在 Python 的条形图中,无法在带有 4 个条形图的条形图顶部获得可读文本
Can't get readable text on top of bars with 4 bars in Python's bar plot
我有以下代码:
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('ggplot')
def autolabel(rects):
"""Attach a text label above each bar in *rects*, displaying its height."""
for rect in rects:
height = int(rect.get_height())
ax.annotate('{}'.format(height),
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(0, 3), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom', fontsize=6)
data = [108, 140.9, 187, 237.6, 299.2, 360.9, 413.3, 431.9, 437.2, 441.9]
set1 = [140.973, 161.588, 202.391, 213.57, 408.55, 442.648, 491.883, 517.456, 534.018, 545.594]
set2 = [140.386, 156.932, 200.106, 213.789, 401.426, 440.09, 490.252, 516.478, 533.255, 545.232]
set3 = [141.046, 162.663, 202.05, 213.613, 408.678, 442.685, 491.894, 517.552, 534.028, 545.858]
stage = [1,2,3,4,5,6,7,8,9,10]
x = np.arange(len(stage)) # the label locations
y = [50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600]
width = 0.20 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(x - 3*width/2, data, width, color = "tab:gray", label='Data')
rects2 = ax.bar(x - width/2, set1, width, color = "tab:red", label='Set 1')
rects3 = ax.bar(x + width/2, set2, width, color = "tab:blue", label='Set 2')
rects4 = ax.bar(x + 3*width/2, set3, width, color = "tab:purple", label='Set 3')
ax.set_yticks(y)
ax.set_yticklabels(y, fontsize=10)
ax.set_ylim(0,580)
ax.set_xticks(x)
ax.set_xticklabels(stage, fontsize=10)
ax.legend(fontsize=8)
ax.grid(True)
autolabel(rects1)
autolabel(rects2)
autolabel(rects3)
autolabel(rects4)
fig.savefig("plot.png", dpi=300)
这给了我以下条形图:
Bar Plot
任何人都可以帮助我在所有条形图的顶部用可读的文本绘制它吗?我尝试增加宽度以获得更多文本可见性,但随后 x 轴上第二个点的条等与之前的条重叠。
matplotlib
现在有一种向条形图添加标签的方法,bar_label
。您可以用它替换您的自定义函数并在那里添加您的选项。为了让你的标签适合,而不进一步缩小文本的大小,你可以旋转 90 度,例如
ax.bar_label(rects1, fmt="%d", fontsize=6, rotation=90, padding=3)
我有以下代码:
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('ggplot')
def autolabel(rects):
"""Attach a text label above each bar in *rects*, displaying its height."""
for rect in rects:
height = int(rect.get_height())
ax.annotate('{}'.format(height),
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(0, 3), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom', fontsize=6)
data = [108, 140.9, 187, 237.6, 299.2, 360.9, 413.3, 431.9, 437.2, 441.9]
set1 = [140.973, 161.588, 202.391, 213.57, 408.55, 442.648, 491.883, 517.456, 534.018, 545.594]
set2 = [140.386, 156.932, 200.106, 213.789, 401.426, 440.09, 490.252, 516.478, 533.255, 545.232]
set3 = [141.046, 162.663, 202.05, 213.613, 408.678, 442.685, 491.894, 517.552, 534.028, 545.858]
stage = [1,2,3,4,5,6,7,8,9,10]
x = np.arange(len(stage)) # the label locations
y = [50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600]
width = 0.20 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(x - 3*width/2, data, width, color = "tab:gray", label='Data')
rects2 = ax.bar(x - width/2, set1, width, color = "tab:red", label='Set 1')
rects3 = ax.bar(x + width/2, set2, width, color = "tab:blue", label='Set 2')
rects4 = ax.bar(x + 3*width/2, set3, width, color = "tab:purple", label='Set 3')
ax.set_yticks(y)
ax.set_yticklabels(y, fontsize=10)
ax.set_ylim(0,580)
ax.set_xticks(x)
ax.set_xticklabels(stage, fontsize=10)
ax.legend(fontsize=8)
ax.grid(True)
autolabel(rects1)
autolabel(rects2)
autolabel(rects3)
autolabel(rects4)
fig.savefig("plot.png", dpi=300)
这给了我以下条形图:
Bar Plot
任何人都可以帮助我在所有条形图的顶部用可读的文本绘制它吗?我尝试增加宽度以获得更多文本可见性,但随后 x 轴上第二个点的条等与之前的条重叠。
matplotlib
现在有一种向条形图添加标签的方法,bar_label
。您可以用它替换您的自定义函数并在那里添加您的选项。为了让你的标签适合,而不进一步缩小文本的大小,你可以旋转 90 度,例如
ax.bar_label(rects1, fmt="%d", fontsize=6, rotation=90, padding=3)