ValueError: Invalid RGBA argument: 'rgbkymc'
The question needs a slight modification as it would first raise the following error:
```AttributeError: 'Series' object has no attribute 'sortlevel'```This is because sortlevel is deprecated since version 0.20.0. You should instead use sort_index in its place.
Plus, the letters symbolising the colors in the color
parameter of the plot
command need to be provided in a list and not in a string. You can read more about it on Specifying Colors on matplotlib.
Hence, you can use this code:
train_class = train_df['Class'].value_counts().sort_index()
my_colors = ['r', 'g', 'b', 'k', 'y', 'm', 'c'] #red, green, blue, black, 'yellow', 'magenta' & 'cyan'
train_class.plot(kind = 'bar', color = my_colors)
plt.grid()
plt.show()
Dataframe.plot()
doesn't actually take a color
argument. You'd have to drive a matplotlib.pyplot.bar()
call directly if you wanted to use a simple sequence of colours (but note that there are better options, listed below).
If you do decide to use matplotlib.pyplot.bar()
directly, then take into account that it's color
argument then only takes either a single valid color value, so 'r'
or 'k'
, or a sequence of such color values (the documentation for bar()
calls it array like). A list of names would work:
my_colors = ['r', 'g', 'b', 'k', 'y', 'm', 'c'] # red, green, blue, black, etc.
plt.bar(len(train_class), train_class, color=my_colors)
The documentation states that the sequence should be equal in length to the number of bars plotted:
The optional arguments color, edgecolor, linewidth, xerr, and yerr can be either scalars or sequences of length equal to the number of bars.
However, it is just easier to pass in a color map to Dataframe.plot()
here. Color maps as a handy and fast path towards distinct bar colors. You can pass one in as the colormap
keyword argument, this can be a named map (as a string):
train_class.plot(kind='bar', colormap='Paired')
or an actual matplotlib colormap object from the matplotlib.cm
module:
from matplotlib import cm
train_class.plot(kind='bar', colormap=cm.Paired)
If you wanted to stick with matplotlib.pyplot.bar()
, but use a colormap, then create your series of colors from a colormap. Pandas uses np.linspace()
for this so here we do too:
import numpy as np
paired_colors = cm.Paired(np.linspace(0, 1, num=len(train_class))
plt.bar(len(train_class), train_class, color=paired_colors)
For bar plots, I'd pick a qualitative colormap; each name is an attribute of the cm
colormap module. In the above, cm.Paired
is a one such color map. Calling the color map with a sequence of floats between 0.0 and 1.0 gives you back colours picked at each 'percentage' of the range. You could also pass in a sequence of integers to index individual colours instead.
Circling back to Pandas, you can create a colormap from a hand-picked sequence of colours too, with a matplotlib.colors.ListedColormap
instance:
from matplotlib.colors import ListedColormap
my_colors = ['r', 'g', 'b', 'k', 'y', 'm', 'c'] # red, green, blue, black, etc.
my_colormap = ListedColormap(my_colors)
and then pass that to your dataframe .plot()
call:
train_class.plot(kind='bar', colormap=my_colormap)