Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为每个地区每年绘制堆叠条形图,以计算销售数量的总和(或平均值)?python_Python_Matplotlib_Seaborn_Bar Chart - Fatal编程技术网

如何为每个地区每年绘制堆叠条形图,以计算销售数量的总和(或平均值)?python

如何为每个地区每年绘制堆叠条形图,以计算销售数量的总和(或平均值)?python,python,matplotlib,seaborn,bar-chart,Python,Matplotlib,Seaborn,Bar Chart,我不确定从哪里开始,我正在尝试得到一个如下所示的图表: y_轴表示图中所示的每年和每个地区的销售数量之和 此数据集可用于: dataset = {'Year': [2019,2020,2020,2019,2019,2020,2017,2017,2018,2020,2018,2016], 'Quantity': [100,50,25,30,40,50,200,600,20,40,100,20], 'Regions': ['Europe','Asia','

我不确定从哪里开始,我正在尝试得到一个如下所示的图表:

y_轴表示图中所示的每年和每个地区的销售数量之和

此数据集可用于:

dataset = {'Year': [2019,2020,2020,2019,2019,2020,2017,2017,2018,2020,2018,2016],
           'Quantity': [100,50,25,30,40,50,200,600,20,40,100,20],
           'Regions': ['Europe','Asia','Africa','Africa','Other','Asia','Africa','Other','America','America','Europe','Europe']}
df = pd.DataFrame(data=dataset)
此外,我不确定在这种情况下使用matplotlib或seaborn是否更好

我尝试过的,但似乎不起作用:

dfc_vol = dfc[['Year','Quantity','Regions']]
plt.rcParams['figure.figsize']=(10,6)
dfc_vol.plot(kind='bar', stacked=True)
plt.legend(bbox_to_anchor=(1.1,0.5))
可能有一个选项首先使用pivot_表重塑数据:

df_pt = df.pivot_table(index='Year', columns='Regions', values='Quantity')

df_pt.plot.bar(stacked=True, figsize=(10, 6))
输出:

可能有一个选项首先使用pivot_表重塑数据:

df_pt = df.pivot_table(index='Year', columns='Regions', values='Quantity')

df_pt.plot.bar(stacked=True, figsize=(10, 6))
输出:


seaborn>=0.11.0时:

sns.histplot(
    data=df,
    x="Year", hue="Regions", weights="Quantity",
    multiple="stack", discrete=True, shrink=.9
)

seaborn>=0.11.0时:

sns.histplot(
    data=df,
    x="Year", hue="Regions", weights="Quantity",
    multiple="stack", discrete=True, shrink=.9
)

谢谢,但由于某些原因,pivot_表无法处理我的数据,它会多次编译每个区域。所以情节看起来不像anything@KilliMandjaro那么你能提供一个实际的数据集吗?我不确定问题出在哪里。我通过添加值=数量修复了pivot问题,但第二行删除时出现错误。你能解释一下它是做什么的以及为什么要添加它吗?@KilliMandjaro-factory values='Quantity'在这里是一个更好的方法,编辑了答案。谢谢,但由于某些原因,pivot_表无法处理我的数据,它会多次编译每个区域。所以情节看起来不像anything@KilliMandjaro那么你能提供一个实际的数据集吗?我不确定问题出在哪里。我通过添加值=数量修复了pivot问题,但第二行删除时出现错误。你能解释一下它是做什么的以及为什么要添加它吗?@KilliMandjaro-factory values='Quantity'在这里是一个更好的方法,编辑了答案。当我试图用plt.legend更改图例的位置时,我收到一条消息说图例中没有手柄。你知道怎么修吗?另外,如果我想使用色调顺序更改顺序,我应该输入什么?当我输入一个特定的列名,即hue_order='Regions'时,当我试图用plt.legend更改图例的位置时,它不起作用。我收到一条消息,说图例中没有要输入的句柄。你知道怎么修吗?另外,如果我想使用色调顺序更改顺序,我应该输入什么?当我输入一个特定的列名,即hue_order='Regions'时,它不起作用