Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random colors by default in matplotlib

I had a look at Kaggle's univariate-plotting-with-pandas. There's this line which generates bar graph.

reviews['province'].value_counts().head(10).plot.bar()

I don't see any color scheme defined specifically. I tried plotting it using jupyter notebook but could see only one color instead of all multiple colors as at Kaggle.

I tried reading the document and online help but couldn't get any method to generate these colors just by the line above.

How do we do that? Is there a config to set this randomness by default?

At Kaggle:

Jupyter Notebook:

like image 369
impossible Avatar asked Apr 19 '26 16:04

impossible


1 Answers

It seems like the multicoloured bars were the default behaviour in one of the former pandas versions and Kaggle must have used that one for their tutorial (you can read more here).

You can easily recreate the plot by defining a list of standard colours and then using it as an argument in bar.

colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd',
          '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf']
reviews['province'].value_counts().head(10).plot.bar(color=colors)

enter image description here

Tested on pandas 0.24.1 and matplotlib 2.2.2.

like image 163
Arienrhod Avatar answered Apr 22 '26 22:04

Arienrhod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!