如何注释堆积条形图的每一段 [英] How to annotate each segment of a stacked bar chart
本文介绍了如何注释堆积条形图的每一段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直试图用其值来注释堆积条形图的每个子数量,如上图所示(值不准确,只是一个示例).
df.iloc [1:].T.plot(种类='bar',堆叠= True)plt.show()
我使用的数据框:
链接的帖子有点类似于我的问题,但是我不理解该答案中给出的代码,也没有给出任何解释.
注释资源 - 来自 matplotlib v3.4.2
-
I've been trying to annotate each sub-amount of a stacked bar chart with its values like the picture shown above (values not accurate, just an example).
df.iloc[1:].T.plot(kind='bar', stacked=True) plt.show()
Data frame that I used:
The linked post is somewhat similar to my question but I do not understand the code given in that answer nor were there any explanations given.
Annotating Values in Stacked Bar Chart Matplotlib
解决方案Imports and DataFrame
import pandas as pd import matplotlib.pyplot as plt data = {'var': ['TR', 'AC', 'F&B'], '2019 1Q': [6600, 1256, 588], '2019 2Q': [6566, 1309, 586], '2019 3Q': [7383, 1525, 673]} df = pd.DataFrame(data) df.set_index('var', inplace=True) # display(df) 2019 1Q 2019 2Q 2019 3Q var TR 6600 6566 7383 AC 1256 1309 1525 F&B 588 586 673
Update as of
matplotlib v3.4.2
- Use
matplotlib.pyplot.bar_label
- See the matplotlib: Bar Label Demo page for additional formatting options.
- Tested with
pandas v1.2.4
, which is usingmatplotlib
as the plot engine.
ax = df.T.plot.bar(stacked=True, figsize=(6, 5), rot=0) for c in ax.containers: ax.bar_label(c, label_type='center') ax.legend(title='Categories', bbox_to_anchor=(1.05, 1), loc='upper left')
Annotation Resources - from
matplotlib v3.4.2
- Adding value labels on a matplotlib bar chart
- Stacked Bar Chart with Centered Labels
- How to plot and annotate multiple data columns in a seaborn barplot
- How to annotate a seaborn barplot with the aggregated value
- stack bar plot in matplotlib and add label to each section
- How to add multiple annotations to a barplot
- How to plot and annotate a grouped bar chart
Original Answer - prior to
matplotlib v3.4.2
- Transpose the dataframe and then use
pandas.DataFrame.plot.bar
withstacked=True
. - An
ndarray
is returned with onematplotlib.axes.Axes
per column withsubplots=True
.- In the case of this figure,
ax.patches
contains 9matplotlib.patches.Rectangle
objects, one for each segment of each bar.- By using the associated methods for this object, the
height
,width
,x
, andy
locations can be extracted, and used to annotate the rectangles.
- By using the associated methods for this object, the
- In the case of this figure,
- The difference this question has from How to annotate a stacked bar chart with word count and column name? is the other question needs to extract and use alternate text for labels, and this dataframe needed to be transposed.
ax = df.T.plot.bar(stacked=True) plt.legend(title='Categories', bbox_to_anchor=(1.05, 1), loc='upper left') for i, rect in enumerate(ax.patches): # Find where everything is located height = rect.get_height() width = rect.get_width() x = rect.get_x() y = rect.get_y() # The height of the bar is the count value and can used as the label label_text = f'{height:.0f}' label_x = x + width / 2 label_y = y + height / 2 # don't include label if it's equivalently 0 if height > 0.001: ax.text(label_x, label_y, label_text, ha='center', va='center', fontsize=8)
这篇关于如何注释堆积条形图的每一段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Use
查看全文