Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python matplotlib高级条形图_Python_Graph_Matplotlib_Plot - Fatal编程技术网

Python matplotlib高级条形图

Python matplotlib高级条形图,python,graph,matplotlib,plot,Python,Graph,Matplotlib,Plot,我需要重新创建一个类似下面在Excel中创建的图表。我希望使用matplotlib,但似乎找不到任何示例或参考来说明如何制作这样的图表。我需要根据性能阈值对条形图着色,并显示阈值。谁能给我指出正确的方向吗?不过,我确实需要能够用Python实现这一点。我必须运行,但这里有一些东西可以让您开始: import numpy as np import matplotlib matplotlib.rcParams['text.usetex'] = False import matplotlib.pypl

我需要重新创建一个类似下面在Excel中创建的图表。我希望使用matplotlib,但似乎找不到任何示例或参考来说明如何制作这样的图表。我需要根据性能阈值对条形图着色,并显示阈值。谁能给我指出正确的方向吗?不过,我确实需要能够用Python实现这一点。

我必须运行,但这里有一些东西可以让您开始:

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)
将numpy导入为np
导入matplotlib
matplotlib.rcParams['text.usetex']=False
将matplotlib.pyplot作为plt导入
进口大熊猫
df=pandas.DataFrame(np.random.uniform(大小=37)*100,列=['A']))
阈值=75
图,ax=plt.子批次(图尺寸=(8,3))
良好=df['A'][df['A']>=阈值]
坏=df['A'][df['A']<阈值]
最大条(左=良好索引,高度=良好,对齐=中心,颜色=绿色,左=5)
ax.bar(左=bad.index,高=bad,align='center',color='Firebrick',zorder=5)
ax.axhline(y=threshold,linewidth=2,color='ForestGreen',zorder=0)
ax.set\u xticks(测向索引)
ax.set_xlim(左=测向指数[0]-0.75,右=测向指数[-1]+0.75)
def注释条(行,ax=ax):
如果第['A']<20行:
颜色=‘黑色’
vertalign='底部'
vertpad=2
其他:
颜色=‘白色’
vertalign=‘顶部’
vertpad=-2
text(row.name,row['A']+vertpad,“{.1f}%.”格式(row['A']),
zorder=10,旋转=90,颜色=color,
水平对齐='中心',
垂直对齐=垂直对齐,
fontsize=8,weight='high')
垃圾=df.apply(注释条,ax=ax,ax=1)
这给了我: