I have a dataframe where energylabel is a categorical variable.
>>> df
costs EP2
0 0.0 260
1 5866.0 230
2 5755.0 32
3 722.0 -42
4 6588.0 103
>>> bins_energylabel = [-np.inf, 0, 50, 75, 105, 160, 190, 250, 290, 335, 380, np.inf]
>>> labs_energylabel = ['A ', 'A ', 'A ', 'A ', 'A', 'B', 'C', 'D', 'E', 'F', 'G']
>>> df['energylabel'] = pd.cut(df['EP2'], bins=bins_energylabel, labels=labs_energylabel)
>>> df
costs EP2 energylabel
0 0.0 260 D
1 5866.0 230 C
2 5755.0 32 A
3 722.0 -42 A
4 6588.0 103 A
I want to plot the costs versus EP2 and coloring the energylabels with their corresponding energylabel.
ax1 = df.plot.scatter(x='costs', y='EP2', c='energylabel', colormap='Set3', sharex=False)
Looks something like this:
Now I want to reverse the order of the colormap values: instead of G
on top, I want A
etc..
My original dataframe is longer and contains more labels, the categories are ordered like so (this is done by using pd.cut()
on column EP2
).
Categories (11, object): ['A ' < 'A ' < 'A ' < 'A ' ... 'D' < 'E' < 'F' < 'G']
I think by reversing the order it can solve my problem. But is it also possible to do this directly from the plotting code above?
CodePudding user response:
IIUC, you can reverse the colormap by adding an _r
suffix and invert the colorbar Y-axis with
NB. inverting the colors is optional, if you don't, you will get: