Как построить круговую диаграмму в matplotlib с 3 столбцами? - PullRequest
1 голос
/ 09 июля 2019

Мне нужно построить круговую диаграмму, используя matplotlib, но у моего DataFrame есть 3 столбца, а именно gender, segment и total_amount.Я пытался играть с plt.pie() аргументами, но для данных нужны только x и метки.Я попытался установить gender как легенду, но тогда он не выглядит правильно.

import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({'gender': {0: 'Female',
  1: 'Female',
  2: 'Female',
  3: 'Male',
  4: 'Male',
  5: 'Male'},
 'Segment': {0: 'Gold',
  1: 'Platinum',
  2: 'Silver',
  3: 'Gold',
  4: 'Platinum',
  5: 'Silver'},
 'total_amount': {0: 2110045.0,
  1: 2369722.0,
  2: 1897545.0,
  3: 2655970.0,
  4: 2096445.0,
  5: 2347134.0}})

plt.pie(data = df,x="claim_amount",labels="Segment")
plt.legend(d3.gender)
plt.show()

В результате я хочу получить круговую диаграмму total_amount и пометить ее как gender и segment,Если я смогу получить процент, это будет бонус.

1 Ответ

0 голосов
/ 09 июля 2019

Я предлагаю следующее:

# Data to plot
# Take the information from the segment and label columns and join them into one string
labels = df["Segment"]+ " " + df["gender"].map(str)
# Extract the sizes of the segments
sizes = df["total_amount"]
# Plot with labels and percentage
plt.pie(sizes, labels=labels,autopct='%1.1f%%')
plt.show()

Вы должны получить это: enter image description here

...