DBILITY

python matplot pie chart 본문

python

python matplot pie chart

DBILITY 2021. 8. 30. 12:33
반응형

raw_data를 dataframe으로 변환 후 pie chart에 plotting할 데이터는 pandas로 group by를 했다.

별도로 pandas학습이 필요하겠다.

import random

import matplotlib.pyplot as plt
from pandas import DataFrame

raw_data = {
    'observation': range(0, 20),
    'individual_value':
        [0.529, 0.55, 0.555, 0.541, 0.559, 0.543, 0.557, 0.559, 0.581, 0.551,
         0.493, 0.534, 0.527, 0.511, 0.565, 0.559, 0.519, 0.562, 0.551, 0.53
         ]
}
data = DataFrame(raw_data)
# print(data)
colors = []

for i in range(0, len(data['observation'])):
    colors.append((round(random.random(),2), round(random.random(),2), round(random.random(),2)))

plt.figure(figsize=(7, 7))

plt.title('pie chart', fontsize=22)

for i in data['individual_value'].keys():
    # data.at[i,'individual_value'] = round(data['individual_value'][i], 1)
    # data.loc[i, ['individual_value']] = round(data['individual_value'][i], 1)
    data.iloc[i] = [i, round(data['individual_value'][i], 1)]

# print(data['individual_value'])

groupValue = data['individual_value'].groupby(data['individual_value'])
gc = groupValue.count()
print(list(gc.index))
print(list(gc.values))
#
plt.pie(gc.values, autopct='%1.1f%%', colors=colors, labels=gc.index, shadow=False,
        startangle=90, textprops={'fontsize': 18})
plt.legend(gc.index)
plt.show()

반응형

'python' 카테고리의 다른 글

python pandas dataframe  (0) 2021.10.12
python papago translate api example  (0) 2021.10.12
python matplot bar chart example  (0) 2021.08.27
python matplot line chart example  (0) 2021.08.25
python regular expression ( 정규 표현식 )  (0) 2021.08.20
Comments