一、准备工作
命令控制行安装matplotlib
pip install matplotlib
代码中引用
import matplotlib.pyplot as plt
import numpy as np
二、代码用例
1、样例数据生成
x = np.linspace(0.05,20,2000) #Get 2000 numbers evenly in the range from 0.05 to 20
x = np.random.randn(200) #Get 200 numbers randomly from the standard normal distribution
y = np.sin(x) #Get the value of the sin funtion based on x
2、趋势图
#x: the data of the x axis
#y: the data of the y axis
#ls:line style,"-"、":"、"-."、"None"
#lw:line width
#label:this line's name
plt.plot(x,y,ls="-",lw=1,label="plot figure1") #set the parameters of the new line
plt.legend() #set the plot location (default)
plt.show() #Show the plot
#趋势图实例:
x = np.linspace(0.05,20,2000) #Get 2000 numbers evenly from range 0.05 to 20
y = np.sin(x)
z = np.cos(x)
plt.plot(x,y,ls="-",lw=1,label="sin(x)")
plt.plot(x,z,ls=":",lw=1,label="cos(x)") #set the parameters of the second line
plt.legend()
plt.show()
图1 双曲线图样例
3、散点图
plt.scatter(x,y,label="scatter figure1")
plt.legend()
plt.show()
#散点图实例:
x1 = np.linspace(5,500,200)
x2 = np.linspace(600,1500,50)
x3 = np.linspace(2000,3000,100)
y1 = np.random.randn(200)
y2 = np.random.randn(50)
y3 = np.random.randn(100)
#c: the color of the point
plt.scatter(x1,y1,c="r",label="scatter figure1")
plt.scatter(x2,y2,c="g",label="scatter figure2")
plt.scatter(x3,y3,c="b",label="scatter figure3")
plt.legend()
plt.show()
图2 三类型散点图样例