基本绘图,设置子图间距,设置图片大小,设置折线类型,标签,设置坐标轴范围、标签,设置子图标题
import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression
x1,y1=[-1,0,1],[0,0,1]
x2,y2=np.arange(5),np.random.rand(5,1)
x3,n_bins=np.random.randn(200,1),50
x4,y4=np.random.randn(200,1)*10,np.random.randn(200,1)*10
#设置图片大小
fig,ax=plt.subplots(2,2,figsize=(20,15))
#设置子图间距
plt.subplots_adjust(wspace=0.5,hspace=0.5)
# 设置折线类型,标签
ax[0,0].plot(x1,y1,'rx--',label='relu')
ax[0,0].legend(loc='best')
#这里的数组只能是1维的。否则报错:float() argument must be a string or a number,
# not 'NoneType'
ax[0,1].bar(x2,y2[:,0])
ax[1,0].hist(x3,n_bins)
ax[1,1].scatter(x4,y4)
# 设置坐标轴范围
ax[1,1].set_xlim([0,20])
# 设置标签,title
ax[1,1].set_xlabel('x')
ax[1,1].set_ylabel('y')
ax[1,1].set_title('散点图')
plt.show()