matplotlib库pyplot基本用法

一、基本函数

  1. 创建画布与创建子图

  1. 绘图主体

  1. 保存和展示图形

二、pyplot动态rc参数

1、简介

2、绘图参数——颜色控制color

3、绘图参数——线型linestyle

4、绘图参数——点型marker

三、例题:中国历年GDP增长线

from matplotlib import pyplot as plt 

plt.rcParams['font.sans-serif'] = ['FangSong'] # 指定默认字体
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
plt.rcParams['lines.marker']='o'
years = [1950, 1960, 1970, 1980, 1990, 2000, 2010, 2020] 
gdp = [300.2, 597.17, 926.03, 1911.49, 3608.58, 12113.50, 60871.60, 147227.00] 

# 创建一幅线图, x轴是年份, y轴是gdp 
plt.plot(years, gdp, color='green', linestyle='solid') 
plt.title("中国历年GDP增长曲线")       # 给图形添加一个标题 
plt.ylabel("亿美元")   # 给y轴加标题
plt.xlabel("年度")       # 给x轴加标题

plt.show()               #显示图形

四、pyplot的其他对象

1、网格线

2、坐标轴、标题、图标识

3、例题:三角函数绘图

import math
import matplotlib.pyplot as plt 

X = [ i / 100  for i in range(0, 1000) ]
Y = [ math.sin(x) for x in X ]
Z = [ math.cos(x**2) for x in X ]

plt.figure(figsize=(8,4))#创建一个空白画布
plt.plot( X , Y , label="$sin(x)$" , color="red" , linewidth=2 ) 
plt.plot( X , Z , "b--" , label="$cos(x^2)$" ) 
plt.xlabel("Time(s)") 
plt.ylabel("Volt") #y轴名称
plt.title("PyPlot First Example")#标题 
plt.ylim(-1.2,1.2)#y轴范围 
plt.legend() #指定当前图形的图例
plt.show()#展示

展示结果:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西西弗斯推石头

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值