本文介绍了如何在条形图上添加多个注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
除了计数之外,我想将百分比值添加到我的熊猫程序中.但是,我无法这样做.我的代码如下所示,到目前为止,我可以显示计数值.有人可以帮我在每个条形显示的计数值旁边/下方添加相对百分比值吗?
import matplotlib导入matplotlib.pyplot作为plt%matplotlib 内联plt.style.use('ggplot')将 seaborn 作为 sns 导入sns.set_style(白色")无花果= plt.figure()fig.set_figheight(5)fig.set_figwidth(10)ax = fig.add_subplot(111)计数 = [29227, 102492, 53269, 504028, 802994]y_ax =('A','B','C','D','E')y_tick = np.arange(len(y_ax))ax.barh(range(len(counts)), counts, align = "center", color = "tab:blue")ax.set_yticks(y_tick)ax.set_yticklabels(y_ax,size = 8)#用值注释条形图对于我在ax.patches中:ax.text(i.get_width()+.09, i.get_y()+.3, str(round((i.get_width()), 1)), fontsize=8)sns.despine()plt.show();我的代码的输出如下所示.如何在显示的每个计数值旁边添加 % 值?
解决方案
使用 pandas
- 使用 pandas v1.2.4 进行测试
使用 3.4.2 版中的 matplotlib 绘图
- 使用注释资源 - 来自 matplotlib v3.4.2
I would like to add percent values - in addition to counts - to my pandas barplot. However, I am not able to do so. My code is shown below and thus far I can get count values to display. Can somebody please help me add relative % values next to/below the count values displayed for each bar?
import matplotlib import matplotlib.pyplot as plt %matplotlib inline plt.style.use('ggplot') import seaborn as sns sns.set_style("white") fig = plt.figure() fig.set_figheight(5) fig.set_figwidth(10) ax = fig.add_subplot(111) counts = [29227, 102492, 53269, 504028, 802994] y_ax = ('A','B','C','D','E') y_tick = np.arange(len(y_ax)) ax.barh(range(len(counts)), counts, align = "center", color = "tab:blue") ax.set_yticks(y_tick) ax.set_yticklabels(y_ax, size = 8) #annotate bar plot with values for i in ax.patches: ax.text(i.get_width()+.09, i.get_y()+.3, str(round((i.get_width()), 1)), fontsize=8) sns.despine() plt.show();The output of my code is shown below. How can one add % values next to each count value displayed?
解决方案With pandas
- Tested with pandas v1.2.4
Plot use matplotlib from version 3.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 using matplotlib as the plot engine.
- Some formatting can be done with the fmt parameter, but more sophisticated formatting should be done with the labels parameter.
- Adding value labels on a matplotlib bar chart
- How to annotate each segment of a stacked 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 plot and annotate a grouped bar chart
Original Answer without pandas
- Tested with matplotlib v3.3.4
- You can play with the positioning.
- Other formatting options mentioned by JohanC
- Print both parts of the text in one string with a \n in between to get a "natural" line spacing:
- str(f'{round((y.get_width()), 1)}\n{round((label_per), 2)}%')
- ax.text(..., va='center') to vertically center and be able to use a slightly larger font.
- ax.set_xlim(0, max(counts) * 1.18) to get a bit more space for the text.
- Start each line of text with a space to get a natural "horizontal" padding.
- str(f' {round((label_per), 2)}%'), note the space before {.
- y.get_width()+.09 is extremely close to y.get_width() when these values are in the tens of thousands.
更多推荐
如何在条形图上添加多个注释
发布评论