如何在附带的图像中创建条形图?
我想创建一个条形图的子图,其中“总计”是Y轴,而“植物”是X轴。同样,“品牌”将是传奇人物,因此在这种情况下,三个不同的“品牌”的3个不同图表。每个组%总计高达100%。我从下面的代码开始,但被卡住了。请参阅下面的数据示例和下图;
import pandas as pd
import numpy as np
df = pd.DataFrame({
'brand':['A','A', 'A', 'B','B', 'B' ,'C','C', 'C'],
'plants':[0, 1, 2, 0,1,2,0,1,2],
'% of total':[80, 12, 8, 67, 18, 5,35, 40,25],
})
plt.figure(figsize=(10, 10))
for i, brand in enumerate(['A', 'B', 'C']):
I would like to create a subplot of bar chart where '% of total' is the y-axis and 'plants' is the x-axis. Also 'brand' will be legend, so in this case 3 different charts for the 3 different 'brands'. Each groups % adds up to 100%. I started with the code below, but got stuck. Please see a sample of the data below and image below;
import pandas as pd
import numpy as np
df = pd.DataFrame({
'brand':['A','A', 'A', 'B','B', 'B' ,'C','C', 'C'],
'plants':[0, 1, 2, 0,1,2,0,1,2],
'% of total':[80, 12, 8, 67, 18, 5,35, 40,25],
})
plt.figure(figsize=(10, 10))
for i, brand in enumerate(['A', 'B', 'C']):
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
seaborn
和 :输出:
You can use
seaborn
andcatplot
:Output:
这需要在循环中吗?您可以简单地使用Pandas抓住相关行。
例如:
这将用于为每个生成一个小号。不确定这是否在问题的范围内,但很乐意在必要时进行编辑。
Does this need to be in a for loop? You could simply grab the relevant rows using pandas.
For example:
This will work for generating a barplot for each. Not sure if this is within the bounds of your problem but happy to edit if necessary.