我有以下数据框:
我想为每一行创建一个饼图,问题是我在图表顺序方面遇到了问题,我希望每个图表的 figsize 可以说是 5,5,并且我的数据框中的每一行都将是一行以索引为标题在我的子图中绘图。
尝试了很多组合并使用 pyploy.subplots 但没有成功。会很高兴得到一些帮助。
谢谢
您可以转置您的数据框并使用 Pandas pie kind 进行绘图,即df.transpose().plot(kind='pie', subplots=True)
或在子绘图时遍历行。
使用子图的示例:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# Recreate a similar dataframe
rows = ['rows {}'.format(i) for i in range(5)]
columns = ['hits', 'misses']
col1 = np.random.random(5)
col2 = 1 - col1
data = zip(col1, col2)
df = pd.DataFrame(data=data, index=rows, columns=columns)
# Plotting
fig = plt.figure(figsize=(15,10))
for i, (name, row) in enumerate(df.iterrows()):
ax = plt.subplot(2,3, i+1)
ax.set_title(row.name)
ax.set_aspect('equal')
ax.pie(row, labels=row.index)
plt.show()
Run Code Online (Sandbox Code Playgroud)