matplotlib 基础
一般给它一个别名mpl
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(1, 10, 100)
x
array([ 1. , 1.09090909, 1.18181818, 1.27272727, 1.36363636,
1.45454545, 1.54545455, 1.63636364, 1.72727273, 1.81818182,
1.90909091, 2. , 2.09090909, 2.18181818, 2.27272727,
2.36363636, 2.45454545, 2.54545455, 2.63636364, 2.72727273,
2.81818182, 2.90909091, 3. , 3.09090909, 3.18181818,
3.27272727, 3.36363636, 3.45454545, 3.54545455, 3.63636364,
3.72727273, 3.81818182, 3.90909091, 4. , 4.09090909,
4.18181818, 4.27272727, 4.36363636, 4.45454545, 4.54545455,
4.63636364, 4.72727273, 4.81818182, 4.90909091, 5. ,
5.09090909, 5.18181818, 5.27272727, 5.36363636, 5.45454545,
5.54545455, 5.63636364, 5.72727273, 5.81818182, 5.90909091,
6. , 6.09090909, 6.18181818, 6.27272727, 6.36363636,
6.45454545, 6.54545455, 6.63636364, 6.72727273, 6.81818182,
6.90909091, 7. , 7.09090909, 7.18181818, 7.27272727,
7.36363636, 7.45454545, 7.54545455, 7.63636364, 7.72727273,
7.81818182, 7.90909091, 8. , 8.09090909, 8.18181818,
8.27272727, 8.36363636, 8.45454545, 8.54545455, 8.63636364,
8.72727273, 8.81818182, 8.90909091, 9. , 9.09090909,
9.18181818, 9.27272727, 9.36363636, 9.45454545, 9.54545455,
9.63636364, 9.72727273, 9.81818182, 9.90909091, 10. ])
y = np.sin(x)
y
array([ 0.84147098, 0.88704699, 0.92529707, 0.95590534, 0.978619 ,
0.99325047, 0.99967891, 0.99785123, 0.98778253, 0.96955595,
0.94332203, 0.90929743, 0.86776314, 0.8190622 , 0.76359681,
0.70182505, 0.63425707, 0.56145091, 0.48400786, 0.40256749,
0.31780241, 0.23041267, 0.14112001, 0.05066187, -0.04021468,
-0.1307591 , -0.22022362, -0.30786935, -0.39297247, -0.47483011,
-0.55276624, -0.6261372 , -0.69433703, -0.7568025 , -0.8130177 ,
-0.86251837, -0.9048957 , -0.93979971, -0.96694212, -0.98609877,
-0.99711147, -0.99988924, -0.99440916, -0.98071647, -0.95892427,
-0.92921254, -0.89182665, -0.84707537, -0.79532828, -0.73701276,
-0.67261042, -0.60265314, -0.52771868, -0.44842592, -0.36542971,
-0.2794155 , -0.19109366, -0.10119362, -0.01045784, 0.0803643 ,
0.17052273, 0.25927286, 0.34588171, 0.429634 , 0.50983804,
0.58583144, 0.6569866 , 0.72271585, 0.78247636, 0.83577457,
0.88217031, 0.92128041, 0.95278186, 0.9764145 , 0.99198316,
0.99935926, 0.99848187, 0.98935825, 0.97206374, 0.94674118,
0.9135997 , 0.87291301, 0.82501713, 0.77030762, 0.70923631,
0.64230758, 0.57007418, 0.49313267, 0.41211849, 0.32770071,
0.24057653, 0.15146548, 0.06110351, -0.0297631 , -0.1203839 ,
-0.21001048, -0.29790263, -0.38333447, -0.46560043, -0.54402111])
用plot函数绘制图形,x就是横轴,y就是纵轴 用show展示
plt.plot(x, y)
plt.show()
实际是一个折线图,就是x是100个元素,y是100个元素,把他们连接了起来
cosy = np.cos(x)
cosy.shape
(100,)
siny = y.copy()
可以画两条曲线 只需要再调用一次plot
plt.plot(x,siny)
plt.plot(x,cosy)
plt.show()
可以手动改直线的颜色
plt.plot(x,siny)
plt.plot(x,cosy,color = 'red')
plt.show()
linestyle也可以改变直线的样式
plt.plot(x,siny)
plt.plot(x,cosy,color = 'red', linestyle='--')
plt.show()
xlim()可以调节x轴的范围 ylim()可以调节y轴的范围
plt.plot(x,siny)
plt.plot(x,cosy,color = 'red', linestyle='--')
plt.xlim(-5, 15)
plt.ylim(0,1.5)
plt.show()
可以一起调节x,y轴,前两个数是x轴
plt.plot(x,siny)
plt.plot(x,cosy,color = 'red', linestyle='--')
plt.axis([-1, 11, -2, 2])
plt.show()
可以给x,y加上说明
plt.plot(x,siny)
plt.plot(x,cosy,color = 'red', linestyle='--')
plt.xlabel("x axis")
plt.ylabel("y value")
plt.show()
可以给两个直线加上注释
plt.plot(x,siny, label="sin(x)")
plt.plot(x,cosy,color = 'red', linestyle='--', label="cos(x)")
plt.xlabel("x axis")
plt.ylabel("y value")
plt.legend()
plt.show()
给图加上标题
plt.plot(x,siny, label="sin(x)")
plt.plot(x,cosy,color = 'red', linestyle='--', label="cos(x)")
plt.xlabel("x axis")
plt.ylabel("y value")
plt.legend()
plt.title("Welcome to the Ml World!")
plt.show()
Scatter Plot 绘制散点图
plt.scatter(x, siny)
plt.show()
plt.scatter(x, siny)
plt.scatter(x, cosy, color='red')
plt.show()
散点图的横纵两个轴是特征
alpha是不透明度,0是完全透明,1是完全不透明
x = np.random.normal(0, 1, 10000)
y = np.random.normal(0, 1, 10000)
plt.scatter(x, y, alpha=0.3)
plt.show()