在pandas绘图教程的饼状图示例http://pandas.pydata.org/pandas-docs/version/0.15.0/visualization.html,生成以下图表:
使用以下代码:
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),因为对于我的应用程序,这些标签太长了,所以我只想在图例中显示它们。
阅读了这篇文章:如何为 matplotlib 饼图添加图例?,我发现可以使用matplotlib.pyplot.pie
,但即使我仍然使用 ggplot,绘图效果也不如 fancy。
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')
我的问题是,是否有一种方法可以同时满足我想要从两个方面得到的东西?明确地说,我想要 Pandas 的花哨效果,但删除楔形图中的文本。
谢谢