简体   繁体   English

如何将 label 添加到水平分组条形图中?

[英]How to add label into a horizontal grouped bar graph?

I have some inquires about how to create a labelled horizontal grouped bar graph.我对如何创建带标签的水平分组条形图有一些疑问。 I am attempting to use a DataFrame to make the graph as shown below.我正在尝试使用 DataFrame 来制作如下图所示的图表。 (The image came from an online source that I found earlier on but did not show code on how to do it) (图片来自我之前找到的在线资源,但没有显示如何操作的代码)

我想要实现的预期图表。

However, the problem is that there is no code showing how to do it and most of the codes have been made outdated after new updates to the Python system and the associated libraries.但是,问题在于没有代码显示如何执行此操作,并且在对 Python 系统和相关库进行新更新后,大多数代码已过时。 What is the code I need to obtain such a graph with labels displayed at each bar graphs?我需要什么代码才能获得这样的图表,每个条形图都显示标签? Thanks!谢谢!

See:看:

The result of combining these references is as follows:合并这些参考的结果如下:

import matplotlib.pyplot as plt
import numpy as np

labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 34, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]

y = np.arange(len(labels))  # the label locations
width = 0.35  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.barh(y - width/2, men_means, width, label='Men')
rects2 = ax.barh(y + width/2, women_means, width, label='Women')

ax.set_xlabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_yticks(y)
ax.set_yticklabels(labels)
ax.legend()

ax.bar_label(rects1, label_type='center')
ax.bar_label(rects2, label_type='center')

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM