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

折线图

使用plot()绘制折线图。plot()函数的语法:

plot ( * args , scalex = True , scaley = True , data = None , ** kwargs ) [source]#

常用函数:

X : 表示X轴的数据

Y : 表示Y轴的数据

fmt : 表示快速设置线条样式的格式字符串

label : 表示应用于图例的标签文本

导入所需库 

import numpy as np  #导入numpy库,命名为np
import matplotlib.pyplot as plt #导入matplotlib中的pyplot库,命名为plt

用面向对象的方式绘制折线图

fig = plt.figure( )
ax =fig.add_subplot(111) 

准备数据

x = np.arange(4, 19)
y_max = np.array([32, 33, 34, 34, 33, 31, 30, 29, 30, 29, 26, 23, 21, 25, 31])
y_min = np.array([19, 19, 20, 22, 22, 21, 22, 16, 18, 18, 17, 14, 15, 16, 16]) 

绘制折线图

ax.plot(x,y_max,
         x,y_min)

展示图表

plt.show() 

 完整代码如下:

 柱形图

使用bar()绘制柱形图。bar()函数的语法:

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'两个取值,

           其中'center'表示将柱形与刻度线居中对齐;'edge'表示将柱形的左边与刻度线对齐。

导入所需库

import numpy as np  #导入numpy库,命名为np
import matplotlib.pyplot as plt #导入matplotlib中的pyplot库,命名为plt

用面向对象的方式绘制柱形图

fig = plt.figure( )
ax =fig.add_subplot(111)

准备数据

x = np.arange(1,8)
y = np.array([10770, 16780, 24440, 30920, 37670, 48200, 57270])
y1 = np.array([1770, 16780, 14440, 1920, 17670, 18200, 17270])

设置柱子的间距

bar_width = 0.3

绘制(橙色)柱形图

ax.bar(x, y, tick_label=["FY2013", "FY2014",
                          "FY2015", "FY2016",
                          "FY2017", "FY2018", "FY2019"], width=0.3,color="orange")

绘制(红色)柱形图

ax.bar(x+0.3,y1,width=0.3,color="red")

展示图表

plt.show()

完整代码如下:  

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值