python第二阶段(15)numpy入门基础-可视化之条形图

    科技2022-07-16  147

    numpy入门基础-可视化之条形图

    1、numpy的条形图bar函数2、示例1)简单的例子2)水平条形图3)显示多条并行条形图4)显示多条重叠条形图 演示:

    1、numpy的条形图bar函数

    条形图: ●以长方形的长度为变量的统计图表 ●用来比较多个项目分类的数据大小 ●通常利用于较小的数据集分析 ●例如不同季度的销量,不同国家的人口等 函数:

    pyplot.bar(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)

    x:条形图的x坐标 height:条形图的高 width:条形图的宽,默认0.8 bottom:基座的y坐标 align:对齐,{‘center’,‘edge’},默认值:‘center’

    center:使基准在x位置居中edge:对齐的左边缘条s的的X位置*

    data:None 其他参数

    color:条形图的颜色edgecolor:条形边缘的颜色

    2、示例

    1)简单的例子

    import numpy as np #导入 numpyas import matplotlib.pyplot as plt #导入 matplotlib.pyplot N=5 y=[20,10,30,25,15] index = np.arange(N) pl=plt.bar(index,height=y)

    out:

    2)水平条形图

    in:

    pl=plt.bar(x=0,bottom=index,width=y,color='red',height=0.5,orientation='horizontal')#orientation='horizontal'水平显示 #等同于pl=plt.barh(index,width=y,color='red',height=0.5)(具体可百度)

    out:

    3)显示多条并行条形图

    in:

    index=np.arange(4) sales_BJ=[52,55,63,53] sales_SH=[44,66,55,41] bar_width=0.3 pl=plt.bar(index,height=sales_BJ,width=bar_width,color='b') pl=plt.bar(index+bar_width,height=sales_SH,width=bar_width,color='r')#要点在index+bar_width这个位置,不然两者会重叠

    out:

    4)显示多条重叠条形图

    in:

    index=np.arange(4) sales_BJ=[52,55,63,53]#北京销量 sales_SH=[44,66,55,41]#上海销量 bar_width=0.3 pl=plt.bar(index,height=sales_BJ,width=bar_width,color='b') pl=plt.bar(index,height=sales_SH,width=bar_width,color='r',bottom=sales_BJ)#要点bottom=sales_BJ,从北京的销量开始

    out:

    Processed: 0.010, SQL: 8