Я хочу нарисовать срединное расположение и его значение рядом с каждым столбцом в наборе столбцов.
Учитывая пример matplotlib
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])
Однако строки слишком длинные: как получить ширину бара? Метки Плюс не отображаются а также столбцы смещены от центров ячеек...!
Я пробовал, но выдает одну строку
Вместо этого используйте hlines
Я обновил код, чтобы включить hline и последующие проблемы.
Чтобы узнать о расположении баров, см. Мой ответ. Суть с метками в том, что при использовании hlines вы получаете всего 1 метку. Если вы добавите plt.legend() к своему коду, он нарисует легенду, и вы поймете, почему это не идеально. Возможно, было бы лучше поставить фактическую медиану как annotation рядом с каждым столбцом...
К моему ответу добавлен простой пример с использованием annotations. Надеюсь, это поможет вам получить то, что вы хотите!






Для позиционирования:
Я немного адаптировал ваш код.
Дело в том, что bar отрисовывает столбцы по центру по умолчанию (см. документы, который всегда помогает), поэтому вам нужно установить hlines от центра - 0,5 ширины столбца до центра + 0,5 ширины столбца:
# Plot bars and create text labels for the table
cell_text = []
medians = []
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])
medians.append(np.median(data[row]))
# Reverse colors and text labels to display the last value at the top.
colors = colors[::-1]
cell_text.reverse()
# Draw the medians
h_width = 0.5 * bar_width
plt.hlines(medians, xmin=index-0.5*bar_width, xmax=index+0.5*bar_width, color = "k", linestyle = "dashed", linewidth=1)
Вот что вы получите тогда:
Теперь о этикетках:
Если вы используете hlines с label=label, вам все равно нужно нарисовать легенду с plt.legend().
Проблема, однако, в том, что все значения будут в легенде, и будет сложно, если не невозможно, связать каждое значение с соответствующей строкой, особенно если все они имеют одинаковый цвет. Лучшим решением может быть размещение значений рядом с каждой пунктирной линией. Для этого вы можете использовать аннотации:
# add the annotations (just after the part from above)
for i, med in enumerate(medians):
plt.annotate(str(med) + "%", xy=(index[i], med),
ha='center', va='bottom'
)
Таким образом, вы получаете:
Вам нужно установить
xminиxmax