Random colors by default in matplotlib

2020-04-30 02:55发布

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:

3条回答
▲ chillily
2楼-- · 2020-04-30 03:01

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.

查看更多
Anthone
3楼-- · 2020-04-30 03:01

The colorful plot has been produced with an earlier version of pandas (<= 0.23). Since then, pandas has decided to make bar plots monochrome, because the color of the bars is pretty meaningless. If you still want to produce a bar chart with the default colors from the "tab10" colormap in pandas >= 0.24, and hence recreate the previous behaviour, it would look like

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

N = 13
df = pd.Series(np.random.randint(10,50,N), index=np.arange(1,N+1))

cmap = plt.cm.tab10
colors = cmap(np.arange(len(df)) % cmap.N)
df.plot.bar(color=colors)

plt.show()

enter image description here

查看更多
贼婆χ
4楼-- · 2020-04-30 03:15

In seaborn is it not problem:

import seaborn as sns

sns.countplot(x='province', data=reviews)

In matplotlib are not spaces, but possible with convert values to one row DataFrame:

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

Or use some qualitative colormap:

import matplotlib.pyplot as plt

N = 10
reviews['province'].value_counts().head(N).plot.bar(color=plt.cm.Paired(np.arange(N)))

reviews['province'].value_counts().head(N).plot.bar(color=plt.cm.Pastel1(np.arange(N)))
查看更多
登录 后发表回答