matplotlib概述
matplotlib是python的一个绘图库。使用它可以很方便的绘制出版质量级别的图形。
matplotlib基本功能
- 基本绘图 (在二维平面坐标系中绘制连续的线)
- 设置线型、线宽和颜色
- 设置坐标轴范围
- 设置坐标刻度
- 设置坐标轴
- 图例
- 特殊点
- 备注
- 图形对象(图形窗口)
- 子图
- 刻度定位器
- 刻度网格线
- 半对数坐标
- 散点图
- 填充
- 条形图
- 饼图
- 等高线图
- 热成像图
- 极坐标系
- 三维曲面
- 简单动画
matplotlib基本功能详解
基本绘图
绘图核心API
案例:绘制一条余弦曲线
import numpy as np
import matplotlib.pyplot as mp
# xarray: <序列> 水平坐标序列
# yarray: <序列> 垂直坐标序列
mp.plot(xarray, yarray)
#显示图表
mp.show()
绘制水平线与垂直线:
import numpy as np
import matplotlib.pyplot as mp
# vertical 绘制垂直线
mp.vlines(vval, ymin, ymax, ...)
# horizotal 绘制水平线
mp.hlines(xval, xmin, xmax, ...)
#显示图表
mp.show()
import numpy as np
import matplotlib.pyplot as mp
x = np.array([1, 2, 3, 4, 5, 6])
y = np.array([12, 39, 36, 25, 4, 41])
# 绘制水平线,垂直线
mp.hlines([10, 20, 30, 40],
[1, 2, 3, 4], [2, 3, 4, 5])
mp.vlines(4, 10, 35)
mp.plot(x, y)
mp.show()
线型、线宽和颜色
案例:绘制一条正弦曲线
#linestyle: 线型 '-' '--' '-.' ':'
#linewidth: 线宽
# 数字
#color: <关键字参数> 颜色
# 英文颜色单词 或 常见颜色英文单词首字母 或 #495434 或 (1,1,1) 或 (1,1,1,1)
#alpha: <关键字参数> 透明度
# 浮点数值
mp.plot(xarray, yarray, linestyle='', linewidth=1, color='', alpha=0.5)
设置坐标轴范围
案例:把坐标轴范围设置为 -π ~ π
#x_limt_min: <float> x轴范围最小值
#x_limit_max: <float> x轴范围最大值
mp.xlim(x_limt_min, x_limit_max)
#y_limt_min: <float> y轴范围最小值
#y_limit_max: <float> y轴范围最大值
mp.ylim(y_limt_min, y_limit_max)
设置坐标刻度
案例:把横坐标的刻度显示为:0, π/2, π, 3π/2, 2π
#x_val_list: x轴刻度值序列
#x_text_list: x轴刻度标签文本序列 [可选]
mp.xticks(x_val_list , x_text_list )
#y_val_list: y轴刻度值序列
#y_text_list: y轴刻度标签文本序列 [可选]
mp.yticks(y_val_list , y_text_list )
刻度文本的特殊语法 – LaTex排版语法字符串
r'$x^n+y^n=z^n$', r'$\int\frac{1}{x} dx = \ln |x| + C$', r'$-\frac{\pi}{2}$'
x 2 + y 2 = z 2 , ∫ 1 x d x = ln ∣ x ∣ + C , − π 2 x^2+y^2=z^2, \int\frac{1}{x} dx = \ln |x| + C, -\frac{\pi}{2} x2+y2=z2,∫x1dx=ln∣x∣+C,−2π
设置坐标轴
坐标轴名:left / right / bottom / top
# 获取当前坐标轴字典,{'left':左轴,'right':右轴,'bottom':下轴,'top':上轴 }
ax = mp.gca()
# 获取其中某个坐标轴
axis = ax.spines['坐标轴名']
# 设置坐标轴的位置。 该方法需要传入2个元素的元组作为参数
# type: <str> 移动坐标轴的参照类型 一般为'data' (以数据的值作为移动参照值)
# val: 参照值
axis.set_position((, val))
# 设置坐标轴的颜色
# color: <str> 颜色值字符串
axis.set_color(color)
案例:设置坐标轴至中心。
#设置坐标轴
ax = mp.gca()
axis_b = ax.spines['bottom']
axis_b.set_position(('data', 0))
axis_l = ax.spines['left']
axis_l.set_position(('data', 0))
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
图例
显示两条曲线的图例,并测试loc属性。
# 再绘制曲线时定义曲线的label
# label: <关键字参数 str> 支持LaTex排版语法字符串
mp.plot(xarray, yarray ... label='', ...)
# 设置图例的位置
# loc: <关键字参数> 制定图例的显示位置 (若不设置loc,则显示默认位置)
# =============== =============
# Location String Location Code
# =============== =============
# 'best' 0
# 'upper right' 1
# 'upper left' 2
# 'lower left' 3
# 'lower right' 4
# 'right' 5
# 'center left' 6
# 'center right' 7
# 'lower center' 8
# 'upper center' 9
# 'center' 10
# =============== =============
mp.legend(loc='')
特殊点
案例:绘制当x=3π/4时两条曲线上的特殊点。
# xarray: <序列> 所有需要标注点的水平坐标组成的序列
# yarray: <序列> 所有需要标注点的垂直坐标组成的序列
mp.scatter(xarray, yarray,
marker='', #点型 ~ matplotlib.markers
s='', #大小
edgecolor='', #边缘色
facecolor='', #填充色
zorder=3 #绘制图层编号 (编号越大,图层越靠上)
)
marker点型可参照:help(matplotlib.markers)
也可参照附录: matplotlib point样式
备注
案例:为在某条曲线上的点添加备注,指明函数方程与值。
# 在图表中为某个点添加备注。包含备注文本,备注箭头等图像的设置。
mp.annotate(
r'$\frac{\pi}{2}$', #备注中显示的文本内容
xycoords='data', #备注目标点所使用的坐标系(data表示数据坐标系)
xy=(x, y), #备注目标点的坐标
textcoords='offset points', #备注文本所使用的坐标系(offset points表示参照点的偏移坐标系)
xytext=(x, y), #备注文本的坐标
fontsize=14, #备注文本的字体大小
arrowprops=dict() #使用字典定义文本指向目标点的箭头样式
)
arrowprops参数使用字典定义指向目标点的箭头样式
#arrowprops字典参数的常用key
arrowprops=dict(
arrowstyle='', #定义箭头样式
connectionstyle='' #定义连接线的样式
)
箭头样式(arrowstyle)字符串如下
============ =============================================
Name Attrs
============ =============================================
'-' None
'->' head_length=0.4,head_width=0.2
'-[' widthB=1.0,lengthB=0.2,angleB=None
'|-|' widthA=1.0,widthB=1.0
'-|>' head_length=0.4,head_width=0.2
'<-' head_length=0.4,head_width=0.2
'<->' head_length=0.4,head_width=0.2
'<|-' head_length=0.4,head_width=0.2
'<|-|>' head_length=0.4,head_width=0.2
'fancy' head_length=0.4,head_width=0.4,tail_width=0.4
'simple' head_length=0.5,head_width=0.5,tail_width=0.2
'wedge' tail_width=0.3,shrink_factor=0.5
============ =============================================
连接线样式(connectionstyle)字符串如下
============ =============================================
Name Attrs
============ =============================================
'angle' angleA=90,angleB=0,rad=0.0
'angle3' angleA=90,angleB=0`
'arc' angleA=0,angleB=0,armA=None,armB=None,rad=0.0
'arc3' rad=0.0
'bar' armA=0.0,armB=0.0,fraction=0.3,angle=None
============ =============================================
import numpy as np
import matplotlib.pyplot as mp
# 线性拆分1000个点
x = np.linspace(-np.pi, np.pi, 1000)
# print(x, x.shape)
sinx = np.sin(x)
# 余弦曲线 cos(x) / 2
cosx = np.cos(x) / 2
# 设置坐标轴的范围
# mp.xlim(0, np.pi)
# mp.ylim(0, 1)
# 修改x轴刻度文本
vals = [-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi]
texts = [r'$-\pi$', r'$-\frac{\pi}{2}$',
'0', r'$\frac{\pi}{2}$', r'$\pi$']
mp.xticks(vals, texts)
# 修改坐标轴
ax = mp.gca()
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.spines['left'].set_position(('data', 0))
ax.spines['bottom'].set_position(('data', 0))
mp.yticks([-1.0, -0.5, 0.5, 1.0])
mp.plot(x, sinx, linestyle='--', linewidth=2,
color='orangered', alpha=0.8,
label=r'$y=sin(x)$')
mp.plot(x, cosx, linestyle='-.', linewidth=2,
color='dodgerblue', alpha=0.9,
label=r'$y=\frac{1}{2}cos(x)$')
# 绘制特殊点
pointx = [np.pi / 2, np.pi / 2]
pointy = [1, 0]
mp.scatter(pointx, pointy,
marker='s', s=70, color='red',
label='smaple points', zorder=3)
# 在图表中为某个点添加备注。包含备注文本,备注箭头等图像的设置。
mp.annotate(
r'$[\frac{\pi}{2}, 1]$', # 备注中显示的文本内容
xycoords='data', # 备注目标点所使用的坐标系(data表示数据坐标系)
xy=(np.pi / 2, 1), # 备注目标点的坐标
textcoords='offset points', # 备注文本所使用的坐标系(offset points表示参照点的偏移坐标系)
xytext=(50, 30), # 备注文本的坐标
fontsize=14, # 备注文本的字体大小
arrowprops=dict(
arrowstyle='-|>',
connectionstyle='angle3') # 使用字典定义文本指向目标点的箭头样式
)
mp.legend()
mp.show()
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9q8vHw3O-1584688843305)(C:\Users\dell\AppData\Roaming\Typora\typora-user-images\1575623929193.png)]
图形对象(图形窗口)
案例:绘制两个窗口,一起显示。
# 手动构建 matplotlib 窗口
mp.figure(
'', #窗口标题栏文本
figsize=(4, 3), #窗口大小 <元组>
dpi=120, #像素密度
facecolor='' #图表背景色
)
mp.show()
mp.figure方法不仅可以构建一个新窗口,如果已经构建过title='xxx’的窗口,又使用figure方法构建了title=‘xxx’ 的窗口的话,mp将不会创建新的窗口,而是把title='xxx’的窗口置为当前操作窗口。
设置当前窗口的参数
案例:测试窗口相关参数
# 设置图表标题 显示在图表上方
mp.title(title, fontsize=12)
# 设置水平轴的文本
mp.xlabel(x_label_str, fontsize=12)
# 设置垂直轴的文本
mp.ylabel(y_label_str, fontsize=12)
# 设置刻度参数 labelsize设置刻度字体大小
mp.tick_params(labelsize=8)
# 设置图表网格线 linestyle设置网格线的样式
# - or solid 粗线
# -- or dashed 虚线
# -. or dashdot 点虚线
# : or dotted 点线
mp.grid(linestyle='')
# 设置紧凑布局,把图表相关参数都显示在窗口中
mp.tight_layout()
子图
矩阵式布局
绘制矩阵式子图布局相关API:
mp.figure('Subplot Layout', facecolor='lightgray')
# 拆分矩阵
# rows: 行数
# cols: 列数
# num: 编号
mp.subplot(rows, cols, num)
# 1 2 3
# 4 5 6
# 7 8 9
mp.subplot(3, 3, 5) #操作3*3的矩阵中编号为5的子图
mp.subplot(335) #简写
案例:绘制9宫格矩阵式子图,每个子图中写一个数字。
mp.figure('Subplot Layout', facecolor='lightgray')
for i in range(9):
mp.subplot(3, 3, i+1)
mp.text(
0.5, 0.5, i+1,
ha='center',
va='center',
size=36,
alpha=0.5,
withdash=False
)
mp.xticks([])
mp.yticks([])
mp.tight_layout()
mp.show()
网格式布局
网格式布局支持单元格的合并。
绘制网格式子图布局相关API:
import matplotlib.gridspec as mg
mp.figure('Grid Layout', facecolor='lightgray')
# 调用GridSpec方法拆分网格式布局
# rows: 行数
# cols: 列数
# gs = mg.GridSpec(rows, cols) 拆分成3行3列
gs = mg.GridSpec(3, 3)
# 合并0行与0、1列为一个子图表
mp.subplot(gs[0, :2])
mp.text(0.5, 0.5, '1', ha='center', va='center', size=36)
mp.show()
案例:绘制一个自定义网格布局。
import matplotlib.gridspec as mg
mp.figure('GridLayout', facecolor='lightgray')
gridsubs = mp.GridSpec(3, 3)
# 合并0行、0/1列为一个子图
mp.subplot(gridsubs[0, :2])
mp.text(0.5, 0.5, 1, ha='center', va='center', size=36)
mp.tight_layout()
mp.xticks([])
mp.yticks([])
自由式布局
自由式布局相关API:
mp.figure('Flow Layout', facecolor='lightgray')
# 设置图标的位置,给出左下角点坐标与宽高即可
# left_bottom_x: 坐下角点x坐标
# left_bottom_x: 坐下角点y坐标
# width: 宽度
# height: 高度
# mp.axes([left_bottom_x, left_bottom_y, width, height])
mp.axes([0.03, 0.03, 0.94, 0.94])
mp.text(0.5, 0.5, '1', ha='center', va='center', size=36)
mp.show()
案例:测试自由式布局,定位子图。
mp.figure('FlowLayout', facecolor='lightgray')
mp.axes([0.1, 0.2, 0.5, 0.3])
mp.text(0.5, 0.5, 1, ha='center', va='center', size=36)
mp.show()
刻度定位器
刻度定位器相关API:
# 获取当前坐标轴
ax = mp.gca()
# 设置水平坐标轴的主刻度定位器
ax.xaxis.set_major_locator(mp.MultipleLocator(1))
# 设置水平坐标轴的次刻度定位器为多点定位器,间隔0.1
ax.xaxis.set_minor_locator(mp.MultipleLocator(0.1))
案例:绘制一个数轴。
mp.figure('Locators', facecolor='lightgray')
# 获取当前坐标轴
ax = mp.gca()
# 隐藏除底轴以外的所有坐标轴
ax.spines['left'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
# 将底坐标轴调整到子图中心位置
ax.spines['bottom'].set_position(('data', 0))
# 设置水平坐标轴的主刻度定位器
ax.xaxis.set_major_locator(mp.NullLocator())
# 设置水平坐标轴的次刻度定位器为多点定位器,间隔0.1
ax.xaxis.set_minor_locator(mp.MultipleLocator(0.1))
# 标记所用刻度定位器类名
mp.text(5, 0.3, 'NullLocator()', ha='center', size=12)
案例:使用for循环测试刻度器样式:
locators = ['mp.NullLocator()', 'mp.MaxNLocator(nbins=4)']
for i, locator in enumerate(locators):
mp.subplot(len(locators), 1, i+1)
mp.xlim(0, 10)
mp.ylim(-1, 1)
mp.yticks([])
# 获取当前坐标轴
ax = mp.gca()
# 隐藏除底轴以外的所有坐标轴
ax.spines['left'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
# 将底坐标轴调整到子图中心位置
ax.spines['bottom'].set_position(('data', 0))
# 设置水平坐标轴的主刻度定位器
ax.xaxis.set_major_locator(eval( ))
# 设置水平坐标轴的次刻度定位器为多点定位器,间隔0.1
ax.xaxis.set_minor_locator(mp.MultipleLocator(0.1))
mp.plot(np.arange(11), np.zeros(11), c='none')
# 标记所用刻度定位器类名
mp.text(5, 0.3, locator, ha='center', size=12)
eval函数就是实现list、dict、tuple与str之间的转化
str函数把list,dict,tuple转为为字符串
一、字符串转换成列表
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CSQe1XtD-1584688843306)(C:\Users\dell\AppData\Roaming\Typora\typora-user-images\1580797047855.png)]
二、字符串转换成字典
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Zmud1Nhu-1584688843306)(C:\Users\dell\AppData\Roaming\Typora\typora-user-images\1580797008584.png)]
三、字符串转换成元组
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ltaTWP0G-1584688843307)(C:\Users\dell\AppData\Roaming\Typora\typora-user-images\1580797074284.png)]
常用刻度器如下
# 空定位器:不绘制刻度
mp.NullLocator()
# 最大值定位器:
# 最多绘制nbins+1个刻度
mp.MaxNLocator(nbins=3)
# 定点定位器:根据locs参数中的位置绘制刻度
mp.FixedLocator(locs=[0, 2.5, 5, 7.5, 10])
# 自动定位器:由系统自动选择刻度的绘制位置
mp.AutoLocator()
# 索引定位器:由offset确定起始刻度,由base确定相邻刻度的间隔
mp.IndexLocator(offset=0.5, base=1.5)
# 多点定位器:从0开始,按照参数指定的间隔(缺省1)绘制刻度
mp.MultipleLocator()
# 线性定位器:等分numticks-1份,绘制numticks个刻度
mp.LinearLocator(numticks=21)
# 对数定位器:以base为底,绘制刻度
mp.LogLocator(base=2)
刻度网格线
绘制刻度网格线的相关API:
ax = mp.gca()
#绘制刻度网格线
ax.grid(
which='', # 'major'/'minor' <-> '主刻度'/'次刻度'
axis='', # 'x'/'y'/'both' <-> 绘制x或y轴
linewidth=1, # 线宽
linestyle='', # 线型
color='', # 颜色
alpha=0.5 # 透明度
)
案例:绘制曲线 [1, 10, 100, 1000, 100, 10, 1],然后设置刻度网格线,测试刻度网格线的参数。
y = np.array([1, 10, 100, 1000, 100, 10, 1])
mp.figure('Normal & Log', facecolor='lightgray')
mp.subplot(211)
mp.title('Normal', fontsize=20)
mp.ylabel('y', fontsize=14)
ax = mp.gca()
ax.xaxis.set_major_locator(mp.MultipleLocator(1.0))
ax.xaxis.set_minor_locator(mp.MultipleLocator(0.1))
ax.yaxis.set_major_locator(mp.MultipleLocator(250))
ax.yaxis.set_minor_locator(mp.MultipleLocator(50))
mp.tick_params(labelsize=10)
ax.grid(which='major', axis='both', linewidth=0.75,
linestyle='-', color='orange')
ax.grid(which='minor', axis='both', linewidth=0.25,
linestyle='-', color='orange')
mp.plot(y, 'o-', c='dodgerblue', label='plot')
mp.legend()
import matplotlib.pyplot as mp
mp.figure('Grid Line', facecolor='lightgray')
ax = mp.gca()
# 修改刻度定位器
ax.xaxis.set_major_locator(mp.MultipleLocator(1))
ax.xaxis.set_minor_locator(mp.MultipleLocator(0.1))
ax.yaxis.set_major_locator(mp.MultipleLocator(250))
ax.yaxis.set_minor_locator(mp.MultipleLocator(50))
ax.grid(which='major', axis='both',
color='orangered', linewidth=0.75)
ax.grid(which='minor', axis='both',
color='orangered', linewidth=0.25)
# 绘制曲线
y = [1, 10, 100, 1000, 100, 10, 1]
mp.plot(y, 'o-', color='dodgerblue')
mp.show()
半对数坐标
y轴将以指数方式递增。 基于半对数坐标绘制第二个子图,表示曲线:[1, 10, 100, 1000, 100, 10, 1]。
mp.figure('Grid', facecolor='lightgray')
y = [1, 10, 100, 1000, 100, 10, 1]
mp.semilogy(y)
mp.show()
散点图
可以通过每个点的坐标、颜色、大小和形状表示不同的特征值。
身高 | 体重 | 性别 | 年龄段 | 种族 |
---|---|---|---|---|
180 | 80 | 男 | 中年 | 亚洲 |
160 | 50 | 女 | 青少 | 美洲 |
绘制散点图的相关API:
mp.scatter(
x, # x轴坐标数组
y, # y轴坐标数组
marker='', # 点型
s=10, # 大小
color='', # 颜色
edgecolor='', # 边缘颜色
facecolor='', # 填充色
zorder='' # 图层序号
)
numpy.random提供了normal函数用于产生符合 正态分布 的随机数
n = 100
# 172: 期望值
# 10: 标准差
# n: 数字生成数量
x = np.random.normal(172, 20, n)
y = np.random.normal(60, 10, n)
案例:绘制平面散点图。
mp.figure('scatter', facecolor='lightgray')
mp.title('scatter')
mp.scatter(x, y)
mp.show()
设置点的颜色
mp.scatter(x, y, c='red') #直接设置颜色
d = (x-172)**2 + (y-60)**2
mp.scatter(x, y, c=d, cmap='jet') #以c作为参数,取cmap颜色映射表中的颜色值
import numpy as np
import matplotlib.pyplot as mp
# 随机生成一组数据
n = 300
height = np.random.normal(175, 5, n)
weight = np.random.normal(70, 7, n)
mp.figure('Persons', facecolor='lightgray')
mp.title('Persons', fontsize=18)
mp.xlabel('height', fontsize=14)
mp.ylabel('weight', fontsize=14)
mp.grid(linestyle=':')
d = (height - 175) ** 2 + (weight - 70)**2
mp.scatter(
height, weight, marker='o', s=70,
label='persons', c=d, cmap='jet')
mp.legend()
mp.show()
cmap颜色映射表参照附件:cmap颜色映射表