【发布时间】:2019-03-04 21:29:24
【问题描述】:
我想在一组条形图中的每个条形旁边绘制中间位置及其值。
import numpy as np
import matplotlib.pyplot as plt
data = [[ 66386, 174296, 75131, 577908, 32015],
[ 58230, 381139, 78045, 99308, 160454],
[ 89135, 80552, 152558, 497981, 603535],
[ 78415, 81858, 150656, 193263, 69638],
[139361, 331509, 343164, 781380, 52269]]
columns = ('Freeze', 'Wind', 'Flood', 'Quake', 'Hail')
rows = ['%d year' % x for x in (100, 50, 20, 10, 5)]
values = np.arange(0, 2500, 500)
value_increment = 1000
# Get some pastel shades for the colors
colors = plt.cm.BuPu(np.linspace(0, 0.5, len(rows)))
n_rows = len(data)
index = np.arange(len(columns)) + 0.3
bar_width = 0.4
# Initialize the vertical-offset for the stacked bar chart.
y_offset = np.zeros(len(columns))
# Plot bars and create text labels for the table
cell_text = []
for row in range(n_rows):
plt.bar(index, data[row], bar_width, bottom=y_offset, color=colors[row])
y_offset = y_offset + data[row]
cell_text.append(['%1.1f' % (x / 1000.0) for x in y_offset])
# Reverse colors and text labels to display the last value at the top.
colors = colors[::-1]
cell_text.reverse()
# Add a table at the bottom of the axes
the_table = plt.table(cellText=cell_text,
rowLabels=rows,
rowColours=colors,
colLabels=columns,
loc='bottom')
# Adjust layout to make room for the table:
plt.subplots_adjust(left=0.2, bottom=0.2)
plt.ylabel("Loss in ${0}'s".format(value_increment))
plt.yticks(values * value_increment, ['%d' % val for val in values])
plt.xticks([])
plt.title('Loss by Disaster')
plt.show()
我尝试将其插入到创建条形的for 循环中
for row in range(n_rows):
plt.bar(index, data[row], bar_width, bottom=y_offset, color=colors[row])
med = np.median(data[row])
xmin = row
xmax= (row + 1)
label = str(med) + "%"
plt.hlines(med, xmin, xmax, label=label, linestyle="dashed")
y_offset = y_offset + data[row]
cell_text.append(['%1.1f' % (x / 1000.0) for x in y_offset])
然而线条太长:如何获得条形宽度? 加号标签不显示并且条从单元格中心移开......!
【问题讨论】:
-
需要设置
xmin和xmax -
我试过了,但它只给出了一行
-
改用
hlines -
我已更新代码以包含
hline和随之而来的问题 -
有关条形的定位,请参阅我的答案。标签的要点是,当使用
hlines时,您只会得到 1 个标签。如果您将plt.legend()添加到您的代码中,它将绘制图例,您会明白为什么这并不理想。将实际中位数作为annotation放在每个条形旁边可能会更好...
标签: python matplotlib histogram