简体   繁体   English

如何使用 matplotlib 绘制饼图

[英]How can I graph a pie chart with matplotlib

Im having trouble graphing a pie chart I cant do it right, Im only able to do a bar graph but a pie chart its a little more complicated.我在绘制饼图时遇到问题,我做不对,我只能绘制条形图,但饼图有点复杂。 Im a newby with matplotlib, so I have this table and this code.我是 matplotlib 的新手,所以我有这张表和这段代码。

# Ordenamos y tenemos los productos mas comprados por hora. 
orders_hour=products_and_orders.groupby(["order_hour_of_day", "product_name"])["order_id"].count().reset_index()
orders_hour["Porcentaje"]=orders_hour["order_id"]/orders_hour["order_id"].sum()

#Sacamos porcentaje para poder graficar 
orders_hour=orders_hour.nlargest(20, "Porcentaje")
orders_hour
    order_hour_of_day   product_name    order_id    Porcentaje
233586  10  Banana  40731   0.001256
275816  11  Banana  38455   0.001186
403103  14  Banana  38218   0.001178
445632  15  Banana  38181   0.001177
192707  9   Banana  37980   0.001171
360618  13  Banana  36992   0.001141
487902  16  Banana  36883   0.001137
318182  12  Banana  36206   0.001116
233406  10  Bag of Organic Bananas  31842   0.000982
445451  15  Bag of Organic Bananas  30919   0.000953
275633  11  Bag of Organic Bananas  30914   0.000953
402921  14  Bag of Organic Bananas  30808   0.000950
529804  17  Banana  30701   0.000947
360445  13  Bag of Organic Bananas  30296   0.000934
192528  9   Bag of Organic Bananas  30268   0.000933
318011  12  Bag of Organic Bananas  29443   0.000908
487722  16  Bag of Organic Bananas  29222   0.000901
154712  8   Banana  27420   0.000845
570586  18  Banana  24571   0.000758
529626  17  Bag of Organic Bananas  23820   0.000734

How can I do the pie chart the hour_of_day with the product_name?如何使用 product_name 制作 hour_of_day 饼图?

Well, how would you expect it to look like?那么,你希望它看起来像什么? Pie charts are (at least the simple ones) 1-D charts.饼图是(至少是简单的)一维图表。 You could show a pie chart for the product names, and another pie chart for the hour of the day.您可以为产品名称显示一个饼图,并为一天中的小时显示另一个饼图。

plt.pie takes a list of wedge sizes, and has an argument labels , a list ordered the same way as the wedge sizes. plt.pie接受一个楔形大小列表,并有一个参数labels ,一个列表以与楔形大小相同的方式排序。 For example, wedgeSizes[0] would be labeled with labels[0] .例如, wedgeSizes[0]将被标记为labels[0] If I understand you correctly, you can make wedgesizes how many orders were done at each hour, and labels be a list of the hours.如果我理解正确的话,您可以将wedgesizes完成的订单数量设为楔形,并将labels设为小时列表。 After you use plt.pie , use plt.show() to display your plot (all of this assuming you've imported matplotlib as plt)使用plt.pie后,使用plt.show()显示您的 plot(假设您已将 matplotlib 导入为 plt)

If you're trying to use the Porcentaje data as the basis for your pie chart, then you just need to create the corresponding labels;如果您尝试使用 Porcentaje 数据作为饼图的基础,那么您只需要创建相应的标签;

pie_labels = [f"{h}, {p}" for (h,p) in zip(hour_of_day, product_name)]

plt.pie(Porcentaje, labels=pie_labels)

plt.show()

在此处输入图像描述

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

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