matplotlib高级条形图 [英] matplotlib advanced bar plot

查看:420
本文介绍了matplotlib高级条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要重新创建一个类似于在Excel中创建的图表。我希望能够使用matplotlib,但似乎无法找到任何示例或参考如何执行此类图表。我需要根据性能阈值对条形进行着色,并显示阈值。任何人都可以将我指向正确的方向吗?不过,我确实需要使用Python来做到这一点。



 <$ c 

$ c> 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']> =阈值]
bad = df ['A'] [df [ 'A']<阈值]

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 =阈值,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,
horizo​​ntalalignment ='center',
verticalalignment = vertalign,
fontsize = 8,weight ='heavy' )

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

这给了我:


I need to recreate a chart similar to the one below created in Excel. I was hoping to use matplotlib, but can't seem to find any examples or reference for how to do a chart like this. I need to have bars colored based on a performance threshold, and also display the threshold. Can anyone point me in the right direction? I do need to be able to do this with Python, though.

解决方案

I gotta run, but here's something to get you started:

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)

And that gives me:

这篇关于matplotlib高级条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆