繁体   English   中英

如何绘制饼图?

[英]how to plot a pie chart?

我有类似的数据:

Machine_id自行车空闲
81091001 41000000000 19000000000
81091001 40000000000 19000000000
81091001 41000000000 19000000000
81091001 41000000000 20000000000
81091001 41000000000 19000000000

绘制饼图的代码:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(palette='Paired')

df = pd.read_csv('sample1.csv')

df = df.set_index('Machine_id')

for ind in df.index:
     fig, ax = plt.subplots(1,1)
     fig.set_size_inches(5,5)
     df.iloc[ind].plot(kind='pie', ax=ax, autopct='%1.1f%%')
     ax.set_ylabel('')
     ax.set_xlabel('')

我在这里遇到错误:

IndexError: single positional indexer is out-of-bounds

那么,如何在每个Machine_id明智的情况下形成饼状图以在熊猫中Cycling v / s Idle

这是您解决的问题:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

sns.set(palette='Paired')

df = pd.read_csv('sample1.csv')

#df = df.set_index('Machine_id')  comment this

for ind in df.index:
     fig, ax = plt.subplots(1,1)
     fig.set_size_inches(5,5)
     df.iloc[ind].plot(kind='pie', ax=ax, autopct='%1.1f%%')
     ax.set_ylabel('')
     ax.set_xlabel('')
     fig.show()   #plot/show final results

另一种方法是考虑具有每行循环和空闲时间的单个图表。 每行的饼图。 (也许饼图不是说明这一点的最佳方法,而是任何方法)

参考 https://matplotlib.org/api/pyplot_api.html

import csv as csv
import matplotlib.pyplot as plt

colors = ['r', 'g']

with open('sample1.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')

    i = 0

    for row in readCSV:
        if i == 0:
            activities = [row[1], row[2]]
            title = row[0]
        else:
            slices = [row[1], row[2]]
            plt.title("Machine ID: " + row[0])  #title is here UPDATED
            plt.pie(slices, labels=activities, colors=colors, startangle=90, autopct='%.1f%%')
            plt.show()

        i += 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM