在每个条形图中显示数值的barh图可以通过在每个条形上添加文本标签来实现。以下是一种常见的实现方法:
import matplotlib.pyplot as plt
import numpy as np
categories = ['A', 'B', 'C', 'D', 'E']
values = [10, 20, 15, 25, 30]
fig, ax = plt.subplots()
bars = ax.barh(categories, values)
for bar in bars:
width = bar.get_width()
ax.text(width, bar.get_y() + bar.get_height() / 2, str(width), ha='left', va='center')
ax.set_title('Barh Chart with Values')
ax.set_xlabel('Value')
ax.set_ylabel('Category')
plt.show()
这样,每个条形图上方就会显示相应的数值。
推荐的腾讯云相关产品:腾讯云云服务器(ECS)和腾讯云弹性容器实例(Elastic Container Instance)。
领取专属 10元无门槛券
手把手带您无忧上云