Matplotlib条形图

目录

一、什么是条形图

二、简单条形图

垂直条形图:

 水平条形图:

三、 复式条形图

多组条形图:

 堆积条形图:

 有for循环的堆积条形图:

 对称条形图

一、什么是条形图

条形图(bar chart)是用宽度相同的条形的高度或长短来表示数据多少的图形。条形图可以横置或纵置,纵置时也称为柱形图(column chart)。此外,条形图有简单条形图、复式条形 图等形式。

二、简单条形图

垂直条形图:

bar(x, height, width=0.8, bottom=None, *, align=‘center’, data=None, **kwargs)

x:浮点型或类数组对象;柱形的的x轴坐标.。

height:浮点型或类数组对象;柱形的高度。

width:浮点型或类数组对象;柱形的宽度,默认值为0.8。

bottom:浮点型或类数组对象; 指定y坐标的起始高度,默认0。

align:指定对齐方式,可选‘center’、 ‘edge’。

import matplotlib.pyplot as plt
import numpy as np

# 获得八个等差小数 # arange:(起始值,结束值,步长)
x=np.arange(0,20,2.5)
height=2*x+1.5

plt.bar(x,height,width=1.5,align="center")

plt.show()

 水平条形图:

barh(y, width, height=0.8, left=None, *, align=‘center’, **kwargs)

y:浮点型或类数组对象;条形的y轴坐标。

width:浮点型或类数组对象;条形的宽度。(width、height与垂直条形图的意义相反)

height:浮点型或类数组对象;柱形的高度。

left:浮点型或类数组对象;条块左侧的x坐标。

align:指定对齐方式,可选‘center’、 ‘edge’。

y=np.arange(0,20,2.5)
width=2*y+1.5

plt.barh(y,width,height=1.5,align="center")

plt.show()

三、 复式条形图

多组条形图:

height=[[10., 20., 30., 20.],[40., 25., 53., 18.],[6., 22., 52., 19.]]
x=np.arange(4)

# x+0.25:因为width=0.25。(width尽量不要太大)
plt.bar(x+0.00,height[0],width=0.25,align="center",color="r")
plt.bar(x+0.25,height[1],width=0.25,align="center",color="b")
plt.bar(x+0.50,height[2],width=0.25,align="center",color="m")

plt.show()

 堆积条形图:

fig=plt.figure(figsize=(5,5))
ax=plt.axes()

height_1=[10., 20., 30., 20.]
height_2=[40., 25., 53., 18.]
x=np.arange(4)

# bottom=height_1:以height_1的各个最高点为起始点。
plt.bar(x,height_1,width=0.5,align="center",color="r")
plt.bar(x,height_2,width=0.5,align="center",color="b",bottom=height_1)

# bottom=height_2:以height_2的各个最高点为起始点。
# plt.bar(x,height_1,width=0.5,align="center",color="r",bottom=height_2)
# plt.bar(x,height_2,width=0.5,align="center",color="b")

plt.show()

 有for循环的堆积条形图:

plf=plt.figure(figsize=(9,5))
ax=plt.axes()

plt.rcParams['font.sans-serif'] = ['KaiTi']  # 指定默认字体,解决中文不显示的问题
# plt.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题

height = np.array([[5., 30., 45., 22.], [5., 25., 50., 20.], [10., 20., 10., 10.],[5.,4.,9.,8.]])
x = np.arange(data.shape[1])
for i in range(height.shape[0]):
    plt.bar(x, height[i], bottom = np.sum(height[:i], axis = 0))
#     print(i) 

plt.xticks(size=15) # x轴刻度值
plt.yticks(size=15)
plt.xlabel("x轴",size=20) # x轴标签
plt.ylabel("y轴",size=20)

plt.show() 

 

 对称条形图:

plf=plt.figure(figsize=(9,5))
ax=plt.axes()


width_1=np.array([5., 30., 45., 22.])
width_2=np.array([5., 25., 50., 20.])
y=np.arange(4)

ax.barh(y,width_1,height=0.5,align="center",color="r",label="图例1")
ax.barh(y,-width_2,height=0.5,align="center",color="b",label="图例2")

plt.xticks(size=15)
plt.yticks(size=15)
plt.xlabel("x轴",size=20)
plt.ylabel("y轴",size=20)

# 设置图例的样式 #loc=1:放在右上(loc=1,2,3,4对应坐标轴的一,二,三,四象限)
plt.legend(loc=1,fontsize=15)
# plt.legend(loc=2)

plt.show()

 

 

  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值