python-matplotlib函数

0. 画散点图

// 绘制散点图
import matplotlib.pyplot as plt
a=[1,2,3]
b=[1,2,3]
plt.scatter(a,b,c='red',marker='o',s=20)
plt.show()

散点图
1. 画直线

// 绘制散点图
import matplotlib.pyplot as plt
a=[1,2,3]
b=[1,2,3]
plt.figure(0)
plt.scatter(a,b,c='red',marker='x',s=20) # 绘制散点图
plt.plot(a,b,'b-',lw=1)  #绘制直线图
plt.xlabel('Population of City in 10,000s',fontsize=10) # X轴
plt.ylabel('Profit of City in $10,000',fontsize=10) # Y轴
plt.legend(['Data Point','Linear Regression']) # 曲线的标签
plt.show()

在这里插入图片描述
2. 画网格图np.meshgird

// 绘制网格图
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,1000,20)  #区间(0,1000)分为20份
y = np.linspace(0,500,20)    #区间(0,500)分为20X,Y = np.meshgrid(x, y)
plt.plot(X, Y,
         color='limegreen',  # 设置颜色为limegreen
         marker='.',  # 设置点类型为圆点
         linestyle='')  # 设置线型为空,也即没有线连接点
plt.grid(True)
plt.show()

在这里插入图片描述
3. 画3D图

// 绘制3D图
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1,1,100) # x轴范围 
y = np.linspace(1,10,100) # y轴范围点
a, b = np.meshgrid(x, y) # 生成X-O-Y平面
J = np.ones(a.shape) # 在每一个网格点上的对应的z轴的取值
figure = plt.figure(1) # 创建一幅图
fp = Axes3D(figure) # 定义为3D图
fp.plot_surface(a,b,J,cmap='rainbow')
plt.show() 

3D图
等高线图绘制

// 绘制等高线图
import matplotlib.pyplot as plt
x = np.linspace(-1,1,3) # x轴范围 
y = np.linspace(1,10,3) # y轴范围点
a, b = np.meshgrid(x, y) # 生成X-O-Y平面
J =  np.random.random(a.shape) # 在每一个网格点上的对应的z轴的取值,也就是高度
a=plt.contourf(a, b, J, 3, cmap=plt.cm.Spectral) # 绘制等高线图
plt.clabel(a, inline=True, fontsize=10) #加上等高线的高度
plt.show() 

等高线图

猜你喜欢

转载自blog.csdn.net/qq_34229228/article/details/88086225
今日推荐