有下面的代码,我不明白为什么不能正确显示信息(检查C列和F列)bot显示为相同的值,但不同
我需要的是绘制df中的一些列,并在所有子图之间共享图例 (所有列都有相同的值[“SI”,“NO”] (这只是一个示例代码)
colnames=['a','b','c','d','e','f','g']
values=[
['SI','SI','NO','SI','SI','SI','NO'],
['SI','NO','NO','SI','NO','SI','NO'],
['SI','SI','NO','SI','SI','SI','NO'],
['SI','NO','NO','NO','NO','SI','SI'],
['SI','NO','NO','NO','NO','SI','NO']]
df=pd.DataFrame(values, columns=colnames)
def pieplotstest(df):
fig,ax =plt.subplots(2,3,facecolor=(1, 1, 1),figsize=(7.2,4.3))
plt.style.use('fivethirtyeight')
colors=["#172a3d","#e33e31"]
textprops=dict(color="w",weight='bold',size=5)
labels=['NO','SI']
ax[0,0].pie(df['b'].value_counts(),
colors=colors,
autopct = '%1.1f%%',
textprops=textprops,
wedgeprops=dict(width=0.5),
pctdistance=0.7
)
ax[0,1].pie(df['c'].value_counts(),
colors=colors,
autopct = '%1.1f%%',
textprops=textprops,
wedgeprops=dict(width=0.5),
pctdistance=0.75
)
ax[0,2].pie(df['d'].value_counts(),
colors=colors,
autopct = '%1.1f%%',
textprops=textprops,
wedgeprops=dict(width=0.5),
pctdistance=0.75
)
ax[1,0].pie(df['e'].value_counts(),
colors=colors,
autopct = '%1.1f%%',
textprops=textprops,
wedgeprops=dict(width=0.5),
pctdistance=0.75
)
ax[1,1].pie(df['f'].value_counts(),
colors=colors,
autopct = '%1.1f%%',
textprops=textprops,
wedgeprops=dict(width=0.5),
pctdistance=0.75
)
ax[1,2].pie(df['g'].value_counts(),
colors=colors,
autopct = '%1.1f%%',
textprops=textprops,
wedgeprops=dict(width=0.5),
pctdistance=0.75
)
ax[0,0].set_title('b',fontsize=10)
ax[0,1].set_title('c',fontsize=10)
ax[0,2].set_title('d',fontsize=10)
ax[1,0].set_title('e',fontsize=10)
ax[1,1].set_title('f',fontsize=10)
ax[1,2].set_title('g',fontsize=10)
fig.legend(labels,
loc=4,
fontsize=7
)
plt.suptitle('Como estan distribuidas tus ventas')
fig.tight_layout()
plt.savefig(f'orders3.png',dpi=600,transparent=True)
dfc[['b', 'c', 'd', 'e', 'f', 'g']].plot.pie(...)
dfc
显示所有值,即使是0,这与使用^{autopct='%1.1f%%'
或autopct = lambda v: f'{v:.1f}%' if v > 0 else None
格式化楔形标签。第二个选项不会添加标签,除非值为>0
pandas 1.3.2
和matplotlib 3.4.2
因此,注释者是正确的,当只有一个值带有
value_counts()
时,您会遇到问题因此,我将DF转换为:
创建了使代码更清晰所需的子批次索引列表:
并添加了一个循环来遍历每个问题(问题为“a”、“b”等):
输出的完整代码如下所示:
相关问题 更多 >
编程相关推荐