簡體   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