pandas 堆积的条形图复制了大图例的颜色 [英] Pandas stacked bar chart duplicates colors for large legends
本文介绍了 pandas 堆积的条形图复制了大图例的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要创建具有大量(大约10个)类别的堆叠条形图.问题在于,熊猫最多只能提供7种不同类别的颜色.它将剩余的颜色设置为蓝色.如何确保每个类别都有唯一的颜色?
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?
示例:
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))
生成一个条形图,其中H,I和J没有被赋予唯一的颜色.
Produces a bar chart where H,I, and J are not given unique colors.
推荐答案
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有很多不同的颜色图可供选择.只是不要使用jet
.
matplotlib has a bunch of different colormaps to choose from. Just don't use jet
.
这篇关于 pandas 堆积的条形图复制了大图例的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文