1、简介
由于经常使用matplotlib进行可视化,所以又苦恼对matplotlib内容知识欠缺导致不能更好做出想要的结果。所以写下该blog进行叙述。
2、库函数引入
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
3、数据引入(x,y分别代表横纵坐标)
x = np.array([-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10])
y = np.array(2*(x**4) + x**2 + 9*x + 2) #假设因变量y刚好符合该公式
4、设置字符样式,解决中文标签乱码
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
5、散点图输出
plt.scatter(x, y, color='black',label="散点图")
plt.legend(loc=1) #loc=1,label标签在右上角
plt.show()
image.png
6、输出曲线图
plt.plot(x, y, color='black',label="曲线图")
plt.legend(loc=2) #loc=2,label标签在左上角
plt.show()
曲线图
7、曲线联合输出(将多条曲线一同输出在一个图里)
plt.scatter(x, y, color='black',label="散点图")
plt.plot(x, y, color='black',label="曲线图")
plt.legend(loc=2) #loc=2,label标签在左上角
plt.show()
散点图和曲线图结合
8、多表合一
使用plt.subplot来创建小图.
plt.subplot(2,2,1)表示将整个图像窗口分为2行2列, 当前位置为1.
使用plt.plot([0,1],[0,1])在第1个位置创建一个小图.
#matplotlib 是可以组合许多的小图, 放在一张大图里面显示的. 使用到的方法叫作 subplot.
#使用import导入matplotlib.pyplot模块, 并简写成plt. 使用plt.figure创建一个图像窗口.
import matplotlib.pyplot as plt
plt.figure()
# 使用plt.subplot来创建小图.
# plt.subplot(2,2,1)表示将整个图像窗口分为2行2列, 当前位置为1.
# 使用plt.plot([0,1],[0,1])在第1个位置创建一个小图.
plt.subplot(2,2,1)
plt.plot([0,1],[0,1])
plt.subplot(2,2,2)
plt.plot([0,1],[0,2])
# plt.subplot(2,2,3)可以简写成plt.subplot(223)
plt.subplot(223)
plt.plot([0,1],[0,3])
plt.subplot(224)
plt.plot([0,1],[0,4])
plt.show() # 展示
多图合一
9、常用曲线颜色
标记符 | 颜色 |
---|---|
r | 红 |
g | 绿 |
b | 蓝 |
c | 蓝绿 |
m | 紫红 |
y | 黄 |
k | 黑 |
w | 白 |
plt.figure()
plt.subplot(4,2,1)
plt.plot(x,y,color='r')
plt.subplot(4,2,2)
plt.plot(x,y,color='g')
# plt.subplot(2,2,3)可以简写成plt.subplot(223)
plt.subplot(423)
plt.plot(x,y,color='b')
plt.subplot(424)
plt.plot(x,y,color='c')
plt.subplot(425)
plt.plot(x,y,color='m')
plt.subplot(426)
plt.plot(x,y,color='y')
plt.subplot(427)
plt.plot(x,y,color='k')
plt.subplot(428)
plt.plot(x,y,color='w')
plt.show() # 展示
不同颜色展示
10、常用曲线类型
linestyle可选参数:
标记 | 曲线 |
---|---|
'-' | solid line style |
'--' | dashed line style |
'-.' | dash-dot line style |
':' | dotted line style |
marker可选参数:
标记 | 曲线类型 |
---|---|
'.' | point marker |
',' | pixel marker |
'o' | circle marker |
'v' | triangle_down marker |
'^' | triangle_up marker |
'<' | triangle_left marker |
'>' | triangle_right marker |
'1' | tri_down marker |
'2' | tri_up marker |
'3' | tri_left marker |
'4' | tri_right marker |
's' | square marker |
'p' | pentagon marker |
'*' | star marker |
'h' | hexagon1 marker |
'H' | hexagon2 marker |
'+' | plus marker |
'x' | x marker |
'D' | diamond marker |
'd' | thin_diamond marker |
'|' | vline marker |
'_' | hline marker |
plt.figure()
plt.subplot(2,2,1)
plt.plot(x,y,color='r',linestyle='-')
plt.subplot(2,2,2)
plt.plot(x,y,color='g',linestyle='--')
# plt.subplot(2,2,3)可以简写成plt.subplot(223)
plt.subplot(223)
plt.plot(x,y,color='b',linestyle='-.')
plt.subplot(224)
plt.plot(x,y,color='c',linestyle=':')
plt.show() # 展示
类型
plt.figure()
plt.subplot(2,2,1)
plt.plot(x,y,color='r',marker='*')
plt.subplot(2,2,2)
plt.plot(x,y,color='g',marker='+')
# plt.subplot(2,2,3)可以简写成plt.subplot(223)
plt.subplot(223)
plt.plot(x,y,color='b',marker='*')
plt.subplot(224)
plt.plot(x,y,color='c',marker='x')
plt.show() # 展示
点特征
11、loc特点
loc参数有多种,’best’表示自动分配最佳位置
类型 | 标记 |
---|---|
'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,
12、图片缩放
(1)figsize=(长,宽):会模糊
from matplotlib import pyplot as plt
plt.figure(figsize=(10,5))
x = [1,2,3]
plt.plot(x, x)
plt.show()
长宽缩放
(2)像素缩放dpi=80
plt.figure(dpi=80)
x = [1,2,3]
plt.plot(x, x)
plt.show()
image.png
13、参考资料出处
1、matplotlab 绘图框架详解([淘码小工]):
https://www.jianshu.com/p/ca21fc707e05?utm_campaign=haruki&utm_content=note&utm_medium=reader_share&utm_source=weixin
2、Python:matplotlib绘图时指定图像大小,放大图像([持剑走天涯]):
https://www.cnblogs.com/qbdj/p/11010773.html