通过面向对象绘制折线图和柱形图

利用matplotlib库中面向对象API,绘制折线图和柱形图

折线图

#导入模块

import matplotlib.pyplot as plt
import numpy as np

#figsize:画布大小 

fig = plt.figure(figsize=(2, 2))   
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)

#准备数据

x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 4, 6, 8, 5])
x1 = np.array([1, 2, 3])
y1 = np.array([4, 5, 6])

#调用plot函数

ax1.plot(x,y,'go-')
ax2.plot(x1,y1,'r-.')

#展示图表

plt.show()

效果如下

柱形图:

使用bar()函数绘制柱形图或堆积柱形图plot(x,height,width=0.8,bottom=None,align='center',data=None,tick_label=None,xerr=None,yerr=None,error_kw=None,**kwargs)
#导入模块

import matplotlib.pyplot as plt
import numpy as np
 
​

#figsize:画布大小 

fig = plt.figure(figsize=(7,5))
ax = fig.add_subplot(111)

#准备数据

x = np.arange(0,6)
y = np.array([11,9,5,7,6,10])
bar_width = 0.3 

#调用bar()函数

ax.bar(x,y,tick_label=['a','b','c','d','e','f'],width=bar_width,color='orange')

#展示图表

plt.show()

#效果如下

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值