gpt4 book ai didi

Pandas 饼图图删除楔形上的标签文本

转载 作者:行者123 更新时间:2023-12-03 21:16:50 26 4
gpt4 key购买 nike

pandas 绘图教程 http://pandas.pydata.org/pandas-docs/version/0.15.0/visualization.html 上的饼图示例生成下图:

enter image description here

使用此代码:

import matplotlib.pyplot as plt
plt.style.use('ggplot')
import numpy as np
np.random.seed(123456)


import pandas as pd
df = pd.DataFrame(3 * np.random.rand(4, 2), index=['a', 'b', 'c', 'd'], columns=['x', 'y'])

f, axes = plt.subplots(1,2, figsize=(10,5))
for ax, col in zip(axes, df.columns):
df[col].plot(kind='pie', autopct='%.2f', labels=df.index, ax=ax, title=col, fontsize=10)
ax.legend(loc=3)

plt.show()

我想从两个子图中删除文本标签 (a,b,c,d),因为对于我的应用程序,这些标签很长,所以我只想在图例中显示它们。

阅读本文后: How to add a legend to matplotlib pie chart? ,我想出了一种使用 matplotlib.pyplot.pie 的方法,但即使我仍在使用 ggplot,这个数字也不那么花哨。
f, axes = plt.subplots(1,2, figsize=(10,5))
for ax, col in zip(axes, df.columns):
patches, text, _ = ax.pie(df[col].values, autopct='%.2f')
ax.legend(patches, labels=df.index, loc='best')

enter image description here

我的问题是,有没有一种方法可以将我想要的东西从双方结合起来?明确地说,我想要 Pandas 的幻想,但从楔子中删除文本。

谢谢

最佳答案

您可以关闭图表中的标签,然后在对 legend 的调用中定义它们。 :

df[col].plot(kind='pie', autopct='%.2f', labels=['','','',''],  ax=ax, title=col, fontsize=10)
ax.legend(loc=3, labels=df.index)

或者
... labels=None ...

enter image description here

关于 Pandas 饼图图删除楔形上的标签文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30059862/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com