Подтвердить что ты не робот

Расширенный бар-график matplotlib

Мне нужно воссоздать диаграмму, аналогичную диаграмме, приведенной ниже в Excel. Я надеялся использовать matplotlib, но, похоже, не нашел примеров или ссылок на то, как это сделать. Мне нужно, чтобы полосы были окрашены в зависимости от порога производительности, а также отображали порог. Может кто-то указать мне верное направление? Мне нужно иметь возможность сделать это с помощью Python. plot example

4b9b3361

Ответ 1

Мне нужно бежать, но вот что-то, чтобы вы начали:

import numpy as np
import matplotlib
matplotlib.rcParams['text.usetex'] = False
import matplotlib.pyplot as plt
import pandas

df = pandas.DataFrame(np.random.uniform(size=37)*100, columns=['A'])
threshold = 75
fig, ax = plt.subplots(figsize=(8,3))

good = df['A'][df['A'] >= threshold]
bad = df['A'][df['A'] < threshold]

ax.bar(left=good.index, height=good, align='center', color='ForestGreen', zorder=5)
ax.bar(left=bad.index, height=bad, align='center', color='Firebrick', zorder=5)

ax.axhline(y=threshold, linewidth=2, color='ForestGreen', zorder=0)

ax.set_xticks(df.index)
ax.set_xlim(left=df.index[0]-0.75, right=df.index[-1]+0.75)

def annotateBars(row, ax=ax):
    if row['A'] < 20:
        color = 'black'
        vertalign = 'bottom'
        vertpad = 2
    else:
        color = 'white'
        vertalign = 'top'
        vertpad = -2

    ax.text(row.name, row['A'] + vertpad, "{:.1f}%".format(row['A']),
            zorder=10, rotation=90, color=color,
            horizontalalignment='center',
            verticalalignment=vertalign,
            fontsize=8, weight='heavy')

junk = df.apply(annotateBars, ax=ax, axis=1)

И это дает мне: annotated bar plot