二维
散点图:
import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate
from scipy.interpolate import interp1d
x = np.array([2, 7, 12, 16, 21,26,30,35,40,45,49,54,59,63,68,72,77,82,86,91,96])
y = np.array([565, 983,734, 865, 1234,769,703,571,522,434,418,363,339,327,294,278,259,248,232,217,212])
# 散点图
plt.scatter(x, y, c='black', alpha=0.5) # alpha:透明度) c:颜色
折线图:
import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate
from scipy.interpolate import splint
x = np.array([2, 7, 12, 16, 21,26,30,35,40,45,49,54,59,63,68,72,77,82,86,91,96])
y = np.array([565, 983,734, 865, 1234,769,703,571,522,434,418,363,339,327,294,278,259,248,232,217,212])
# 散点图
plt.scatter(x, y, c='black', alpha=0.5) # alpha:透明度) c:颜色
#折线图
plt.plot(x, y)
平滑折线图:
import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate
from scipy.interpolate import splint
x = np.array([2, 7, 12, 16, 21,26,30,35,40,45,49,54,59,63,68,72,77,82,86,91,96])
y = np.array([565, 983,734, 865, 1234,769,703,571,522,434,418,363,339,327,294,278,259,248,232,217,212])
# 散点图
# plt.scatter(x, y, c='black', alpha=0.5) # alpha:透明度) c:颜色
# plt.plot(x, y)
# 产生从2到95,间隔为1的xnew数组
xnew = np.arange(2, 95, 0.1)
# 实现函数
func = interpolate.interp1d(x, y, kind='cubic')
# 利用xnew和func函数生成ynew,xnew数量等于ynew数量
ynew = func(xnew)
# 平滑后的折线图
plt.plot(xnew, ynew)
图例
# 横纵坐标的名称以及对应字体格式
font2 = {'family': 'Times New Roman',
'weight': 'normal',
'size': 18,
}
#
plt.xlabel("Frequency (Hz)", font2) # X轴标题及字号
plt.ylabel("Amplitude (mm)", font2) # Y轴标题及字号
# 刻度的字体
plt.tick_params(axis='both', labelsize=14) # 刻度大小
三维
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
fig = plt.figure() #定义新的三维坐标轴
ax3 = plt.axes(projection='3d')
#定义三维数据
xx = np.arange(0.1,10,0.2)
yy = np.arange(0.1,10,0.2)
X, Y = np.meshgrid(xx, yy)
#Z = np.sin(X)+np.cos(Y)
Z = np.sqrt(X/Y)
#作图
ax3.plot_surface(X,Y,Z,rstride = 1, cstride = 1,cmap='rainbow')
#ax3.contour(X,Y,Z, zdim='z',offset=-2,cmap='rainbow) #等高线图,要设置offset,为Z的最小值
plt.show()
双坐标:
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
plt.rcParams['font.sans-serif'] = ['Times New Roman']
R = np.array([0.1,0.15,0.2,0.25])
V = np.array([15.04,20.4,21.7,21.97])
P = np.array([897,874,812,795])
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(R, V, label='ww', color='blue')
ax.scatter(R, V, c='blue') # alpha:透明度) c:颜色
ax2 = ax.twinx()
ax2.plot(R, P, label='ww',color='red')
ax2.scatter(R, P, c='red',marker='^') # alpha:透明度) c:颜色
ax.legend(loc=0)
#ax.grid()
ax2.set_ylim(650, 950)
ax.set_ylim(5,30)
ax2.legend(loc=0)
plt.show()
#plt.savefi
当matplotlib等模块下载速度慢:
打开IDE命令行终端,输入python,。例如要下载numpy
pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
参考链接:
https://blog.csdn.net/wxystyle/article/details/79577556
https://blog.csdn.net/LEE18254290736/article/details/88343568