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.
# 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?
CodePudding user response:
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.
CodePudding user response:
plt.pie
takes a list of wedge sizes, and has an argument labels
, a list ordered the same way as the wedge sizes. For example, wedgeSizes[0]
would be labeled with 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. After you use plt.pie
, use plt.show()
to display your plot (all of this assuming you've imported matplotlib as plt)
CodePudding user response:
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;
pie_labels = [f"{h}, {p}" for (h,p) in zip(hour_of_day, product_name)]
plt.pie(Porcentaje, labels=pie_labels)
plt.show()