【问题标题】:plotting a pandas dataframe row by row逐行绘制熊猫数据框
【发布时间】:2017-12-17 00:00:55
【问题描述】:

我有以下数据框:

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

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

谢谢

【问题讨论】:

    标签: pandas matplotlib


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-08
      • 2016-01-07
      • 2016-09-16
      • 2019-03-17
      • 2017-10-10
      • 2020-01-14
      • 2017-05-28
      • 2017-06-09
      相关资源
      最近更新 更多