逐行绘制pandas数据帧

时间:2017-12-17 00:00:55

标签: pandas matplotlib

我有以下数据框:

enter image description here

我想为每一行创建一个饼图,问题是我遇到了图表顺序的问题,我希望每个图表都有一个可比的说5,5并且我的数据框中的每一行都是我的子图中的一行图,索引为标题。

尝试了很多组合并使用pyploy.subplots但没有成功。 很高兴得到一些帮助。

由于

1 个答案:

答案 0 :(得分:2)

您可以转置数据框并使用pandas饼类进行绘图,即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()

piecharts

相关问题