您的位置:首页 > 其它

pyplot绘制条形图函数bar与barh使用实例源码

2017-08-21 16:33 471 查看
#coding=utf8
'''
matplotlib.pyplot.bar(left, height, width=0.8, bottom=None, hold=None, data=None, **kwargs)
绘制条形图。绘制带有矩形边界的条形图通过如下设置:
left, left + width, bottom, bottom + height
(left, right, bottom and top edges)

输入参数:
left:标量序列。表示条形图左边x坐标。
height:标量或者标量序列。条形图的高度。
width:标量或者数组,可选参数。条形图宽度默认为:0.8。
bottom:标量或者数组,可选参数。条形图y坐标,默认值为None。
color:标量或者数组,可选参数。条形图前景色。
edgecolor:标量或者数组,可选参数。条形图边界颜色。
linewidth:标量或者数组,可选参数。条形图边界宽度。
如果为None,使用默认linewidth;如果为0,不画边界。默认为None。
tick_label:字符串或者数组,可选参数。条形图的tick标记,默认为None。
xerr:标量或者数组,可选参数。如果不是None,将把生成的errorbars用在条形图上,默认为None。
yerr:标量或者数组,可选参数。如果不是None,将把生成的errorbars用在条形图上,默认为None。

ecolor:标量或者数组,可选参数。指定errorbars的颜色,默认为None。
capsize:标量,可选参数。确定errorbars上限的长度,默认为None,从errorbar.capsize rcParam获取到值。

error_kw:字典类型,可选参数。kwags参数被传给errorbar方法。
ecolor和capsize可能在这被指定而不是作为一个单独的kwargs。

align:{'center','edge'},可选参数,默认:'center'。
如果是'edge',通过左边界(条形图垂直)和底边界(条形图水平)来使条形图对齐。
如果是'center',将left参数解释为条形图中心坐标。
通过传递一个给width设置复数,来使条形图以右边界进行对齐。

orientation:{'vertical','horizontal'},可选参数。设置条形图方向。
log:布尔类型,可选参数。如果为true,设置轴到log scale。默认为False。

返回值:
bars:matplotlib.container.BarContainer。带有所有bar与errorbar的容器。

-------------------------------------------------------------------------------
matplotlib.pyplot.barh(bottom, width, height=0.8, left=None, hold=None, **kwargs)
创建一个水平条形图。创建一个带有矩形边界的水平条,设置如下:
left, left + width, bottom, bottom + height
(left, right, bottom and top edges)
输入参数:
bottom:标量或者数组。条形图的y坐标。
width:标量或者数组。条形图宽度。
height:标量序列,可选参数,默认值为:0.8。条形图的高度。
left:标量序列。条形图左边的X坐标

返回值:
matplotlib.patches.Rectangle实例。

'''
import matplotlib as mpl
import matplotlib.pyplot as plt
import sys

#提供汉字支持
mpl.rcParams["font.family"]="sans-serif"
mpl.rcParams["font.sans-serif"]=u'SimHei'

def Vbar():
#绘制垂直条形图
rect=plt.bar(left=(0,1,2),height=(40,30,50),width=0.35,align="center")
#显示汉字,前面加u,代表使用unicode
#y轴标记
plt.ylabel(u'班级')

#x轴标记
plt.xlabel(u'人数')

#设置x轴可读显示
plt.xticks((0,1,2),(u'一年级',u'二年级',u'三年级'))

#显示柱状图信息
plt.legend((rect,),(u"图例",))
#显示绘制图片
plt.show()

def Hbar():

#绘制水平条形图
rect=plt.barh(bottom=(0,1,2),height=0.35,width=(40,30,50),align="center")
#显示汉字,前面加u,代表使用unicode
#y轴标记
plt.ylabel(u'类别')

#x轴标记
plt.xlabel(u'数量')

#设置x轴可读显示
plt.yticks((0,1,2),(u'文学类',u'历史类',u'哲学类'))

#显示柱状图信息
plt.legend((rect,),(u"图例",))
#显示绘制图片
plt.show()

if __name__=="__main__":
while True:
choice=raw_input(u"输入你的选择(V,H):")
if choice in ["v","V"]:
Vbar()
elif choice in ["h","H"]:
Hbar()
else:
print "Exit..."
sys.exit()






内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: