Pandas stacked bar chart duplicates colors for large legends

老子叫甜甜 提交于 2019-12-05 05:51:43

问题


I need to create a stacked bar chart with a large number (10 or so) categories. The problem is that Pandas only provides colors for up to 7 different categories. It sets the remaining colors as blue. How to I ensure that every category has a unique color?

Example:

df = pd.DataFrame(np.abs(np.random.randn(10,10)),columns=['A','B','C','D','E','F','G','H','I','J'], index=range(10))
df.plot(kind='bar',stacked=True,figsize=(20,10))

Produces a bar chart where H,I, and J are not given unique colors.


回答1:


In [22]: colors = plt.cm.GnBu(np.linspace(0, 1, 10))

In [23]: df.plot(kind='bar', stacked=True, figsize=(20, 10), color=colors)
Out[23]: <matplotlib.axes._subplots.AxesSubplot at 0x10b792d90>

matplotlib has a bunch of different colormaps to choose from. Just don't use jet.



来源:https://stackoverflow.com/questions/21355832/pandas-stacked-bar-chart-duplicates-colors-for-large-legends

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!