Отрисовываю барплоты циклом, хочу, чтоб автоматически в каждом рисунке определялся самый высокий бар и расскрашивался, например, в красный (чтоб прям сразу в глаза бросалось, какой из выборов был наиболее предпочитаемым). Скажите, пожалуйста, есть ли кратчайшие способы?
def autolabel(rects, labels=None, height_factor=1.01):
for i, rect in enumerate(rects):
height = rect.get_height()
if labels is not None:
try:
label = labels[i]
except (TypeError, KeyError):
label = ' '
else:
label = '%d' % int(height)
ax.text(rect.get_x() + rect.get_width()/2., height_factor*height,
'{}'.format(label),
ha='center', va='bottom')
for index, column in enumerate(df.columns):
array_counts = Counter(df[column])
x, y = [], []
metki = ('', 'очень \nнеэффективно', 'достаточно \nнеэффективно', 'нейтрально', 'достаточно \nэффективно', 'очень \nэффективно', '')
for key, value in array_counts.items():
x.append(key);
y.append(value)
plt.figure(figsize=(8, 5))
ax = plt.gca()
patches = plt.bar(x, y, edgecolor='black', width=1.0)
autolabel(ax.patches, height_factor=1.01)
jet = plt.get_cmap('GnBu', len(patches)) # увета
N = 7
ind = np.arange(N)
for i in range(len(patches)):
patches[i].set_facecolor(jet(i))
# plt.title("{}".format(str(column)))
plt.title('\n'.join(wrap("{}".format(str(column)),60)))
plt.xlim(0.1, 6)
plt.rcParams.update({'font.size': 10})
plt.rc('axes', titlesize=10)
plt.xticks(ind, metki, rotation=60)
plt.tight_layout()
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.spines["bottom"].set_visible(True)
ax.spines["left"].set_visible(True)
# plt.axis('off')
Counter
от самых больших значения перечисляет, можно в цикле раскраски на первой итерации цикла красный цвет забабахать попробовать.