文章目录
- 一、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 pltx = [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 npx = 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可缩写为cplt.show()
(3)调整线条风格
import matplotlib.pyplot as plt
import numpy as npx = 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可缩写为lsplt.show()
(4)调整线宽
import matplotlib.pyplot as plt
import numpy as npx = 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可缩写为lwplt.show()
(5)调整数据点标记
import matplotlib.pyplot as plt
import numpy as npx = 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可缩写为msplt.show()
(6)颜色和风格设置的简写
import matplotlib.pyplot as plt
import numpy as npx = 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 npx = 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 npx = 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 npx = 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 npx = 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 npx = 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 npx = 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 pltx = 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 pltx = ["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 pltx= 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="man")
plt.bar(x,y2,width=0.5,bottom=y1,label="woman")
plt.legend()
plt.show()
(3)并列柱形图
import numpy as np
import matplotlib.pyplot as pltx= np.arange(15)
y1 = x+1
y2 = y1+np.random.random(15)
plt.bar(x,y1,width=0.3,label="man")
plt.bar(x+0.3,y2,width=0.3,label="woman")
plt.legend()
plt.show()
(4)横向柱形图
import numpy as np
import matplotlib.pyplot as pltx = ["G"+str(i) for i in range(1,6)]
y = 2 * np.arange(1,6)
plt.barh(x,y,align="center",height=0.5,alpha=0.8,color="blue",edgecolor="red")
plt.tick_params(axis="both",labelsize=13)
plt.show()
4、多子图
(1)简单多子图
import numpy as np
import matplotlib.pyplot as pltdef f(t):return np.exp(-t)*np.cos(2*np.pi*t)t1 = np.arange(0.0,5.0,0.1)
t2 = np.arange(0.0,5.0,0.2)plt.subplot(211) # 总共2行1列子图网格,当前选中第1个子图
plt.plot(t1,f(t1),"bo-",markerfacecolor="r",markersize=5)
plt.title("A tale of 2 subplots")
plt.ylabel("Damped oscillation")plt.subplot(212) # 总共2行1列子图网格,当前选中第2个子图
plt.plot(t2,np.cos(2*np.pi*t2),"r--")
plt.xlabel("time(s)")
plt.ylabel("Undamped")plt.show()
(2)多行多列子图
import numpy as np
import matplotlib.pyplot as pltx = np.random.random(10)
y = np.random.random(10)plt.subplots_adjust(hspace=0.5,wspace=0.3) # 调整子图之间的水平和垂直间距plt.subplot(321)
plt.scatter(x,y,s=80,c="b",marker=">")plt.subplot(322)
plt.scatter(x,y,s=80,c="g",marker="*")plt.subplot(323)
plt.scatter(x,y,s=80,c="r",marker="s")plt.subplot(324)
plt.scatter(x,y,s=80,c="c",marker="p")plt.subplot(325)
plt.scatter(x,y,s=80,c="m",marker="+")plt.subplot(326)
plt.scatter(x,y,s=80,c="y",marker="H")plt.show()
(3)不规则多子图
import matplotlib.pyplot as plt
import numpy as npdef f(x):return np.exp(-x) * np.cos(2*np.pi*x)x = np.arange(0.0, 3.0, 0.01)
grid = plt.GridSpec(2, 3, wspace=0.4, hspace=0.3)plt.subplot(grid[0, 0])
plt.plot(x, f(x))plt.subplot(grid[0, 1:])
plt.plot(x, f(x), "r--", lw=2)plt.subplot(grid[1, :])
plt.plot(x, f(x), "g-", lw=3)
plt.show()
5、直方图
(1)普通频次直方图
import matplotlib.pyplot as plt
import numpy as npmu, sigma = 100,15
x = mu + sigma * np.random.randn(1000)plt.hist(x, bins=50, facecolor="g", alpha=0.75)plt.show()
(2)概率密度
import matplotlib.pyplot as plt
import numpy as npmu, sigma = 100,15
x = mu + sigma * np.random.randn(1000)plt.hist(x, 50, density=True, color="r")
plt.xlabel("Smarts")
plt.ylabel("Probability")
plt.title("Histogram of IQ")
plt.text(60, .025, r"$\mu=100,\ \sigma=15$")
plt.xlim(40, 160)
plt.ylim(0, 0.03)plt.show()
import matplotlib.pyplot as plt
import numpy as npmu, sigma = 100,15
x = mu + sigma * np.random.randn(1000)plt.hist(x, 50, density=True, color="r", histtype="step")
plt.xlabel("Smarts")
plt.ylabel("Probability")
plt.title("Histogram of IQ")
plt.text(60, .025, r"$\mu=100,\ \sigma=15$")
plt.xlim(40, 160)
plt.ylim(0, 0.03)plt.show()
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import normmu, sigma = 100,15
x = mu + sigma * np.random.randn(10000)_, bins, __=plt.hist(x, 50, density=True)
y = norm.pdf(bins, mu, sigma)
plt.plot(bins, y, "r--", lw=3)
plt.xlabel("Smarts")
plt.ylabel("Probability")
plt.title("Histogram of IQ")
plt.text(60, .025, r"$\mu=100,\ \sigma=15$")
plt.xlim(40, 160)
plt.ylim(0, 0.03)plt.show()
(3)累计概率分布
import matplotlib.pyplot as plt
import numpy as npmu, sigma = 100,15
x = mu + sigma * np.random.randn(10000)plt.hist(x, 50, density=True, cumulative=True, color="r")plt.xlabel("Smarts")
plt.ylabel("Probability")
plt.title("Histogram of IQ")
plt.text(60, 0.8, r"$\mu=100,\ \sigma=15$")
plt.xlim(50, 165)
plt.ylim(0, 1.1)plt.show()
(4)例:模拟两个骰子
import matplotlib.pyplot as plt
import numpy as npclass Die():"模拟一个骰子的类"def __init__(self, num_sides=6):self.num_sides = num_sidesdef roll(self):return np.random.randint(1, self.num_sides+1)# 重复投一个骰子
die = Die()
results = []
for i in range(60000):result = die.roll()results.append(result)plt.hist(results, bins=6, range=(0.75, 6.75), align="mid", width=0.5)
plt.xlim(0, 7)plt.show()
# 重复投两个骰子
die1 = Die()
die2 = Die()
results = []
for i in range(60000):result = die1.roll() + die2.roll()results.append(result)plt.hist(results, bins=11, range=(1.75, 12.75), align="mid", width=0.5)
plt.xlim(0, 13)
plt.xticks(np.arange(1, 14))plt.show()
# 重复投两个骰子
die1 = Die()
die2 = Die()
results = []
for i in range(60000):result = die1.roll() + die2.roll()results.append(result)plt.hist(results, bins=11, range=(1.75, 12.75), density=True, align="mid", width=0.5)
plt.xlim(0, 13)
plt.xticks(np.arange(1, 14))plt.show()
6、误差图
(1)基本误差图
import matplotlib.pyplot as plt
import numpy as npx = np.linspace(0, 10, 50)
dy = 0.5
y = np.sin(x) + dy*np.random.randn(50)plt.errorbar(x, y, yerr=dy, fmt="+b")plt.show()
(2)柱形图误差图
import matplotlib.pyplot as plt
import numpy as npmenMeans = (20, 35, 30, 35, 27)
womenMeans = (25, 32, 34, 20, 25)
menStd = (2, 3, 4, 1, 2)
womenStd = (3, 5, 2, 3, 3)
ind = ["G1", "G2", "G3", "G4", "G5"]
width = 0.35p1 = plt.bar(ind, menMeans, width=width, label="Men", yerr=menStd)
p2 = plt.bar(ind, womenMeans, width=width, bottom=menMeans, label="Women", yerr=womenStd)plt.ylabel("Scores")
plt.title("Scores by group and gender")
plt.yticks(np.arange(0, 81, 10))
plt.legend()
plt.show()
三、面相对象的风格简介
1、普通图
import matplotlib.pyplot as plt
import numpy as npx = np.linspace(0, 5, 10)
y = x ** 2fig = plt.figure(figsize=(8,4), dpi=80) # 设置画布对象
axes = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # 设置轴 [left,bottom,width,height]与画布的比例
axes.plot(x, y, "r")
axes.set_xlabel("x")
axes.set_ylabel("y")
axes.set_title("title")
plt.show()
2、画中画
import matplotlib.pyplot as plt
import numpy as npx = np.linspace(0, 5, 10)
y = x ** 2fig = plt.figure()
ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax2 = fig.add_axes([0.2, 0.5, 0.4, 0.3])ax1.plot(x, y, "r")ax1.set_xlabel("x")
ax1.set_ylabel("y")
ax1.set_title("title")ax2.plot(y, x, "g")
ax2.set_xlabel("y")
ax2.set_ylabel("x")
ax2.set_title("insert title")plt.savefig("1.png")
plt.show()
3、多子图
import matplotlib.pyplot as plt
import numpy as npdef f(t):return np.exp(-t) * np.cos(2*np.pi*t)t1 = np.arange(0.0, 3.0, 0.01)fig = plt.figure()
fig.subplots_adjust(hspace=0.4, wspace=0.4)ax1 = plt.subplot(2, 2, 1)
ax1.plot(t1, f(t1))
ax1.set_title("Upper left")ax2 = plt.subplot(2, 2, 2)
ax2.plot(t1, f(t1))
ax2.set_title("Upper right")ax3 = plt.subplot(2, 1, 2)
ax3.plot(t1, f(t1))
ax3.set_title("Lower")plt.savefig("1.png")
plt.show()
四、三维图形简介
1、三维数据点与线
import matplotlib.pyplot as plt
import numpy as npax = plt.axes(projection="3d")
zline = np.linspace(0, 15, 1000)
xline = np.sin(zline)
yline = np.cos(zline)
ax.plot3D(xline, yline, zline)zdata = 15*np.random.random(100)
xdata = np.sin(zdata)
ydata = np.cos(zdata)ax.scatter3D(xdata, ydata, zdata, c=zdata, cmap="spring")plt.savefig("1.png")
plt.show()
2、三维数据曲面图
import matplotlib.pyplot as plt
import numpy as npdef f(x, y):return np.sin(np.sqrt(x**2 + y**2))x = np.linspace(-6, 6, 30)
y = np.linspace(-6, 6, 30)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)ax = plt.axes(projection="3d")
ax.plot_surface(X, Y, Z, cmap="viridis")plt.savefig("1.png")
plt.show()