文章目录
- 一、matplotlib库的作用与环境配置
- 1、环境配置示例
- 2、改变绘图风格
- 3、保存图片
- 二、绘制二维图形
- 1、折线图
- (1)示例
- (2)调整线条颜色:
- (3)调整线条风格
- (4)调整线宽
- (5)调整数据点标记
- (6)颜色和风格设置的简写
- (7)调整坐标轴
- (8)设置图形标签
- (9)设置图例
- (10)添加文字和箭头
- 2、散点图
- (1)示例
- (2)颜色设置
- (3)根据数据控制点的大小
- (4)透明度
- 3、柱形图
- (1)简单柱形图
- (2)累加柱形图
- (3)并列柱形图
- (4)横向柱形图
- 4、多子图
- (1)简单多子图
- (2)多行多列子图
- (3)不规则多子图
- 5、直方图
- (1)普通频次直方图
- (2)概率密度
- (3)累计概率分布
- (4)例:模拟两个骰子
- 6、误差图
- (1)基本误差图
- (2)柱形图误差图
- 三、面相对象的风格简介
- 1、普通图
- 2、画中画
- 3、多子图
- 四、三维图形简介
- 1、三维数据点与线
- 2、三维数据曲面图
一、matplotlib库的作用与环境配置
matplotlib库是数据分析中,数据可视化的一个重要工具。
1、环境配置示例
import matplotlib.pyplot as plt
x = [1,2,3,4]
y = [1,4,9,16]
plt.plot(x,y) # 绘制折线图
plt.ylabel("squares") # 设置y轴标签
plt.show() # 显示图片
2、改变绘图风格
print(plt.style.available[:]) # 显示可用的绘图风格
with plt.style.context("seaborn-white"): # 临时改变绘图风格
plt.show()
import matplotlib.pyplot as plt
plt.style.use("seaborn-white") # 永久改变绘图风格
x = [1,2,3,4]
y = [1,4,9,16]
plt.plot(x,y) # 绘制折线图
plt.ylabel("squares") # 设置y轴标签
plt.show() # 显示图片
3、保存图片
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,10,100)
plt.plot(x,np.exp(x))
plt.savefig("myfigure.png") # 保存图片
二、绘制二维图形
1、折线图
(1)示例
import matplotlib.pyplot as plt
import numpy as np
plt.style.use("seaborn-white")
x = np.linspace(0,2*np.pi,100)
plt.plot(x,np.sin(x)) # 绘制正弦曲线
plt.plot(x,np.cos(x)) # 绘制余弦曲线
plt.show()
(2)调整线条颜色:
import matplotlib.pyplot as plt
import numpy as np
plt.style.use("seaborn-white")
x = np.linspace(0,2*np.pi,100)
plt.plot(x,np.sin(x)) # 绘制正弦曲线
offsets = np.linspace(0,np.pi,5)
colors = ["blue","g","r","yellow","pink"]
for offset,color in zip(offsets,colors):
plt.plot(x,np.sin(x-offset),color=color) # 关键字color可缩写为c
plt.show()
(3)调整线条风格
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,10,11)
offsets = list(range(8))
linestyles = ["solid","dashed","dashdot","dotted","-","--","-.",":"]
for offset,linestyle in zip(offsets,linestyles):
plt.plot(x,x+offset,linestyle=linestyle) # linestyle可缩写为ls
plt.show()
(4)调整线宽
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,10,11)
offsets = range(0,12,3)
linewidths = (i*2 for i in range(1,5))
for offset,linewidth in zip(offsets,linewidths):
plt.plot(x,x+offset,linewidth=linewidth) # linewidth可缩写为lw
plt.show()
(5)调整数据点标记
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,10,11)
offsets = range(0,12,3)
markers = ["*","+","o","s"]
for offset,marker in zip(offsets,markers):
plt.plot(x,x+offset,marker=marker,markersize=10) # markersize可缩写为ms
plt.show()
(6)颜色和风格设置的简写
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,10,11)
offsets = range(0,8,2)
color_linestyles = ["g-","c--","k-.","r:"]
for offset,color_linestyle in zip(offsets,color_linestyles):
plt.plot(x,x+offset,color_linestyle)
plt.show()
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,10,11)
offsets = range(0,8,2)
cmls = ["g*-","b+--","ko-.","rs:"]
for offset,cml in zip(offsets,cmls):
plt.plot(x,x+offset,cml)
plt.show()
其他用法及颜色缩写、数据点标记缩写等,请查看官方文档,如下:
https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html
(7)调整坐标轴
x轴、y轴的边界
x = np.linspace(0,2*np.pi,100)
plt.plot(x,np.sin(x))
plt.xlim(-1,7)
plt.ylim(-1.5,1.5)
plt.show()
x = np.linspace(0,2*np.pi,100)
plt.plot(x,np.sin(x))
plt.axis([-2,8,-2,2]) #设置x轴的边界为-2到8,y轴为-2到2
plt.show()
x = np.linspace(0,2*np.pi,100)
plt.plot(x,np.sin(x))
plt.axis("tight")
plt.show()
x = np.linspace(0,2*np.pi,100)
plt.plot(x,np.sin(x))
plt.axis("equal")
plt.show()
help(plt.axis) 可以查询 plt.axis() 中还可以设置哪些字符串参数。
对数坐标
import matplotlib.pyplot as plt
import numpy as np
import math
x = np.logspace(0,5,6) #10**0到10**5的等比数列,取6个数
plt.plot(x,np.log(x),marker="o") # np.log(x)表示以e为底,求x的对数
plt.xscale("log") # 表示x轴的刻度设置为对数刻度
plt.show()
调整坐标轴刻度
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,10,100)
plt.plot(x,x**2)
plt.xticks(np.arange(0,12,step=1),fontsize=15)
plt.yticks(np.arange(0,100,step=10))
plt.show()
调整刻度样式
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,10,100)
plt.plot(x,x**2)
plt.tick_params(axis="both",labelsize=15)
plt.show()
(8)设置图形标签
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,2*np.pi,100)
plt.plot(x,np.sin(x))
plt.title("A Sine Curve",fontsize=20)
plt.xlabel("x",fontsize=15)
plt.ylabel("sin(x)",fontsize=15)
plt.show()
(9)设置图例
默认:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,2*np.pi,100)
plt.plot(x,np.sin(x),"b-",label="Sin")
plt.plot(x,np.cos(x),"r--",label="Cos")
plt.legend()
plt.show()
修饰图例:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,2*np.pi,100)
plt.plot(x,np.sin(x),"b-",label="Sin")
plt.plot(x,np.cos(x),"r--",label="Cos")
plt.legend(loc="upper center",frameon=False,fontsize=15)
#loc supported values are 'best', 'upper right', 'upper left', 'lower left', 'lower right', 'right', 'center left', 'center right', 'lower center', 'upper center', 'center'
plt.ylim(-1.5,2)
plt.show()
(10)添加文字和箭头
添加文字
x = np.linspace(0,2*np.pi,100)
plt.plot(x,np.sin(x),"b-")
plt.text(3.5,0.5,"y=sin(x)",fontsize=15) # 3.5,0.5为文字所在坐标
plt.show()
添加箭头
x = np.linspace(0,2*np.pi,100)
plt.plot(x,np.sin(x),"b-")
plt.annotate("local min",xy=(1.5*np.pi,-1),xytext=(4.5,0),arrowprops=dict(facecolor="black",shrink=0.1))
# xy=(1.5*np.pi,-1) 指定了箭头指向的点的坐标。xytext=(4.5,0) 指定了注释文本的位置。arrowprops=dict(facecolor="black",shrink=0.1) 则设置了箭头的样式,包括颜色和箭头的缩放。
plt.show()
2、散点图
(1)示例
x = np.linspace(0,2*np.pi,20)
plt.scatter(x,np.sin(x),marker="s",s=30,c="r") # s:大小 c:颜色
plt.show()
(2)颜色设置
x = np.linspace(0, 10, 10)
y = x ** 2
plt.scatter(x, y, c=y, cmap="Blues")
# c=y:散点的颜色根据y值的大小而变化
plt.colorbar()
plt.show()
颜色配置参考官方文档:
https://matplotlib.org/2.0.2/examples/color/colormaps_reference.html
(3)根据数据控制点的大小
x,y,colors,size = (np.random.rand(100) for i in range(4))
plt.scatter(x,y,c=colors,s=1000*size,cmap="viridis")
plt.colorbar()
plt.show()
(4)透明度
x,y,colors,size = (np.random.rand(100) for i in range(4))
plt.scatter(x,y,c=colors,s=1000*size,cmap="viridis",alpha=0.3)
plt.colorbar()
plt.show()
3、柱形图
(1)简单柱形图
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(1,6)
plt.bar(x,2*x,align="center",width=0.5,alpha=0.5,color="yellow",edgecolor="red")
plt.xticks(x,("G1","G2","G3","G4","G5"))
plt.tick_params(axis="both",labelsize=13)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
x = ["G"+str(i) for i in range(5)]
y = 1/(1+np.exp(-np.arange(5)))
colors = ["red","yellow","blue","green","gray"]
plt.bar(x,y,align="center",width=0.5,alpha=0.5,color=colors)
plt.tick_params(axis="both",labelsize=13)
plt.show()
(2)累加柱形图
import numpy as np
import matplotlib.pyplot as plt
x= np.arange(5)
y1 = np.random.randint(20,30,size=5)
y2 = np.random.randint(20,30,size=5)
plt.bar(x,y1,width=0.5,label