熊猫系列的彩色饼图
Posted
技术标签:
【中文标题】熊猫系列的彩色饼图【英文标题】:Colormapping pie plots of pandas series 【发布时间】:2019-01-22 02:54:28 【问题描述】:我有一个双甜甜圈图,如果我绘制 DF,我知道如何绘制颜色图,但这不适用于系列。怎么放:
colors=outside['group1'].map(colors_outside),
colors=middle['group2'].map(colors_middle),
在plt.pie()
部分?这是示例代码:
df1 = pd.DataFrame('group1': ['bar', 'bar', 'baz', 'baz', 'baz', 'baz'],
'group2': ['one', 'two', 'one', 'two', 'three', 'four'],
'count': np.random.randint(1,5, 6))
# making the colormap
colors_outside = 'bar': '#FF1493', 'baz': '#FFFF00'
colors_middle= 'one': '#FF3F33', 'two': '#8175E1','three':'#0A0096', 'four':'#960028'
outside = df1.groupby('group1')['group1'].count()
middle = df1.groupby(['group1','group2']).count()
# making the donut
plt.pie(outside, startangle=90, labels=outside.index) # here I need: colors=outside['group1'].map(colors_outside),
plt.pie(middle, radius=0.75, labels=labels, startangle=90, labeldistance=0.75 ) # here I need: colors=middle['group2'].map(colors_middle),
centre_circle = plt.Circle((0,0),0.5,color='black', fc='white')
fig = plt.gcf()
fig.gca().add_artist(centre_circle)
plt.axis('equal')
plt.tight_layout()
plt.show()
【问题讨论】:
【参考方案1】:你可以得到如下颜色:
outside_colors = [colors_outside[c] for c in outside.index]
middle_colors = [colors_middle[c] for c in middle.index.get_level_values('group2')]
然后在情节中使用它们:
plt.pie(outside, startangle=90, labels=outside.index, colors=outside_colors)
plt.pie(middle, radius=0.75, labels=labels, startangle=90, colors=middle_colors, labeldistance=0.75)
【讨论】:
以上是关于熊猫系列的彩色饼图的主要内容,如果未能解决你的问题,请参考以下文章