matplot绘制bar图

您所在的位置:网站首页 matplot画图 matplot绘制bar图

matplot绘制bar图

2022-06-07 19:36| 来源: 网络整理| 查看: 265

# -*- coding: utf-8 -*- """ Created on Fri Aug 19 15:07:54 2016 @author: aijun """

import matplotlib.pyplot as plt

def autolabel(rects):     for rect in rects:         height = rect.get_height()         plt.text(rect.get_x()+rect.get_width()/2, 1.03*height, '%s'%float(height)) plt.xlabel('性别') plt.ylabel('人数') plt.title('性别比例分析') plt.xticks((0, 1), ('男', '女')) rect = plt.bar(left=(0, 1), height=(1, 0.5), width=0.35, align="center", yerr=0.0001) plt.legend((rect, ), ('图例', )) autolabel(rect) plt.show()

bar(left, height, width, color, align, yerr)函数:绘制柱形图。left为x轴的位置序列,一般采用arange函数产生一个序列;height为y轴的数值序列,也就是柱形图的高度,一般就是我们需要展示的数据;width为柱形图的宽度,一般这是为1即可;color为柱形图填充的颜色;align设置plt.xticks()函数中的标签的位置;yerr让柱形图的顶端空出一部分。

plt.title('图的标题')函数:为图形添加标题。

plt.xticks(*args, **kwargs)函数:设置X轴的值域。

plt.legend(*args, **kwargs)函数:添加图例。参数必须为元组legend((line1, line2, line3), ('label1', 'label2','label3'))

plt.xlim(a,b)函数:设置x轴的范围。

plt.ylim(a,b)函数:设置y周的范围。

Plt.xticks(*args, **kwargs)函数:获取或者设置X轴当前刻度的标签。Tuple1,tuple2长度相等,tuple1为对应的刻度,tuple2为对应刻度的标签。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3