您可以做的是用这种方式在绘图上注释一些文本
for x,y,tex in zip(x_axis,abs_value,perc_value):
t = ax.text(x,f"{tex:.2f} %",horizontalalignment='center',verticalalignment='center',size = 11.5,bbox = dict(boxstyle="round",fc="w",ec='#414141',linewidth=1.4))
其中x_axis
是带有列所在位置的列表。 abs_value
是带有列高的列表,而perc_value
是带有百分比的列表。
我在文本中添加了其他元素,例如bbox
将创建一个带有百分比的圆形白框。试着使用参数以获得最佳效果。
如您所见,我将文本f"{tex:.2f} %"
放置在坐标(x,y)
上。
希望对您有所帮助。
,
要注释条形图,您可以在此处参考matplotlib文档中的示例。
https://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/barchart.html#sphx-glr-gallery-lines-bars-and-markers-barchart-py
fig = plt.figure(figsize=(15,10))
ax = plt.gca()
width = 0.35
rects1 = ax.bar(df.index-width/2,df.A,width)
rects2 = ax.bar(df.index+width/2,df.B,width)
for r1,r2 in zip(rects1,rects2):
h1 = r1.get_height()
h2 = r2.get_height()
percent = int(h1 * 100 / h2)
ax.annotate('{}%'.format(percent),xy=(r1.get_x() + r1.get_width() / 2,h1),xytext=(0,3),# 3 points vertical offset
textcoords="offset points",ha='center',va='bottom')
ax.annotate('100%',xy=(r2.get_x() + r2.get_width() / 2,h2),va='bottom')
plt.show()
和
fig = plt.figure(figsize=(15,10))
ax = plt.gca()
width = 0.35
rects1 = ax.bar(df.index,width)
rects2 = ax.bar(df.index,width,bottom=df.A)
for r1,h1/2),0),textcoords="offset points",h1+h2/2),va='bottom')
plt.show()
本文链接:https://www.f2er.com/3168229.html