Using matplotlib colormap with pandas dataframe.plot function

I am trying to use the matplotlib.colormap object in conjunction with the pandas.plot function:

import pandas as pd import matplotlib.pyplot as plt import matplotlib.cm as cm df = pd.DataFrame({'days':[172, 200, 400, 600]}) cmap = cm.get_cmap('RdYlGn') df['days'].plot(kind='barh', colormap=cmap) plt.show() 

I know that I have to somehow tell colormap the range of values ​​that it feeds, but I cannot figure out how to do this when using the pandas.plot () function, as this graph () does not accept vmin / vmax parameters, for example .

+6
source share
1 answer

Pandas applies a color palette for each row, which means that you get the same color for a frame with one column.

To apply different colors to each row of your data frame, you must generate a list of colors from the selected color map:

 import pandas as pd import matplotlib.pyplot as plt import matplotlib.cm as cm import numpy as np df = pd.DataFrame({'days':[172, 200, 400, 600]}) colors = cm.RdYlGn(np.linspace(0,1,len(df))) df['days'].plot(kind='barh', color=colors) plt.show() 

enter image description here

Another method is to use matplotlib directly.

+6
source

Source: https://habr.com/ru/post/1013859/


All Articles