本文介绍了如何在条形图(Python)上的条形上方书写文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这张图:我想在每列上方写下计数.这些值在第一个和第二个列表中.你能帮我解决这个问题吗?我尝试了一些没有成功的东西.
I have this graph:I want to write the count above each column. These values are in the first and second lists. Can you help me solve this problem? I tried something without success.
这是图表的代码:
countListFast = [1492.0, 497.0, 441.0, 218.0, 101.0, 78.0, 103.0]
countListSlow = [1718.0, 806.0, 850.0, 397.0, 182.0, 125.0, 106.0]
errorRateListOfFast = ['9.09', '9.09', '9.38', '9.40', '7.89', '8.02', '10.00']
errorRateListOfSlow = ['10.00', '13.04', '14.29', '12.50', '14.29', '14.53', '11.11']
opacity = 0.4
bar_width = 0.35
plt.xlabel('Tasks')
plt.ylabel('Error Rate')
plt.xticks(range(len(errorRateListOfFast)),('[10-20)', '[20-30)', '[30-50)', '[50-70)','[70-90)', '[90-120)', ' [120 < )'), rotation=30)
plt.bar(np.arange(len(errorRateListOfFast))+ bar_width, errorRateListOfFast, bar_width, align='center', alpha=opacity, color='b', label='Fast <= 6 sec.')
plt.bar(range(len(errorRateListOfSlow)), errorRateListOfSlow, bar_width, align='center', alpha=opacity, color='r', label='Slower > 6 sec.')
plt.legend()
plt.tight_layout()
plt.show()
推荐答案
plt.bar()
返回一个矩形列表,可用于在每个条形上方放置合适的文本,如下所示:
plt.bar()
returns a list of rectangles that can be used to position suitable text above each of the bars as follows:
import matplotlib.pyplot as plt
import numpy as np
errorRateListOfFast = ['9.09', '9.09', '9.38', '9.40', '7.89', '8.02', '10.00']
errorRateListOfSlow = ['10.00', '13.04', '14.29', '12.50', '14.29', '14.53', '11.11']
# Convert to floats
errorRateListOfFast = [float(x) for x in errorRateListOfFast]
errorRateListOfSlow = [float(x) for x in errorRateListOfSlow]
opacity = 0.4
bar_width = 0.35
plt.xlabel('Tasks')
plt.ylabel('Error Rate')
plt.xticks(range(len(errorRateListOfFast)),('[10-20)', '[20-30)', '[30-50)', '[50-70)','[70-90)', '[90-120)', ' [120 < )'), rotation=30)
bar1 = plt.bar(np.arange(len(errorRateListOfFast)) + bar_width, errorRateListOfFast, bar_width, align='center', alpha=opacity, color='b', label='Fast <= 6 sec.')
bar2 = plt.bar(range(len(errorRateListOfSlow)), errorRateListOfSlow, bar_width, align='center', alpha=opacity, color='r', label='Slower > 6 sec.')
# Add counts above the two bar graphs
for rect in bar1 + bar2:
height = rect.get_height()
plt.text(rect.get_x() + rect.get_width() / 2.0, height, f'{height:.0f}', ha='center', va='bottom')
plt.legend()
plt.tight_layout()
plt.show()
给你:
ha='center'
和 va='bottom'
指的是文本如何相对于 x
和 y
坐标,即水平和垂直对齐.
ha='center'
and va='bottom'
refer to how the text is aligned in relation to the x
and y
co-ordinates, i.e. horizontal and vertical alignment.
这篇关于如何在条形图(Python)上的条形上方书写文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!