x = np.linspace(-1.0,1.0,100)
# 在指定的间隔内返回均匀间隔的数字
y = np.sin(x)
# 在标准正态分布中随机取100个数
y1 = np.random.randn(100)
有四个参数值:'-','--','-.',':'
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-1.0,1.0,1000)
y = np.sin(x)
plt.plot(x,y,ls='-',lw=2,label='sin(x)')
plt.legend()plt.show()
可供选择的颜色有:'blue','green','red','cyan','magenta','yellow','black','white'
import matplotlib.pyplot as pltimport numpy as np
x = np.linspace(-1,1,1000)y = np.random.rand(1000)
plt.scatter(x,y,c='c',label='plot testing')plt.legend()plt.show()
import matplotlib.pyplot as pltimport numpy as np
x = np.linspace(-1,1,1000)y = np.random.rand(1000)
plt.scatter(x,y,c='c',label='plot testing')
plt.xlim(-0.5,0.5)
plt.ylim(0,1)
plt.legend()
plt.show()
import matplotlib.pyplot as pltimport numpy as np
x = np.linspace(-1,1,1000)y = np.random.rand(1000)
plt.scatter(x,y,c='c',label='plot testing')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()
import matplotlib.pyplot as pltimport numpy as np
x = np.linspace(-1,1,1000)y = np.random.rand(1000)
plt.scatter(x,y,c='c',label='plot testing')
plt.grid(ls='--',c='r')
plt.legend()
plt.show()
import matplotlib.pyplot as pltimport numpy as np
x = np.linspace(-1,1,1000)y = np.random.rand(1000)
plt.scatter(x,y,c='c',label='plot testing')# 平行于x轴plt.axhline(y=0,c='r',ls='-',lw=2)# 垂直于x轴plt.axvline(x=0,c='r',ls='-',lw=2)plt.legend()plt.show()
import matplotlib.pyplot as pltimport numpy as np
x = np.linspace(-1,1,1000)y = np.random.rand(1000)
plt.scatter(x,y,c='c',label='plot testing')
plt.axvspan(xmin=-0.2,xmax=0.2,facecolor='c',alpha=0.2)
plt.axhspan(ymin=0.2,ymax=0.4,facecolor='c',alpha=0.2)
plt.legend()
plt.show()
import matplotlib.pyplot as pltimport numpy as np
x = np.linspace(-2.0,2.0,1000)
y = np.sin(x)
plt.plot(x,y,ls='-',lw=2,label='sin(x)')
plt.legend()plt.annotate('max',
xy=(np.pi/2,1),
xytext=((np.pi/2)+1,0.8),
weight='bold',
color='b',
arrowprops=dict(arrowstyle='->',connectionstyle='arc3',color='r'))
plt.show()
import matplotlib.pyplot as pltimport numpy as np
x = np.linspace(-0.1,0.1,1000)
y = np.cos(x)
plt.plot(x,y,ls='-.',lw=2,c='c',label='plot testing')
plt.legend()
plt.text(0,1,'y=cos(x)',weight='bold',color='r')
plt.show()
import matplotlib.pyplot as pltimport numpy as np
x = np.linspace(-0.1,0.1,1000)
y = np.cos(x)
plt.plot(x,y,ls='-.',lw=2,c='c',label='plot testing')
plt.legend()plt.title('y=cos(x)')
plt.show()
import matplotlib.pyplot as pltimport numpy as np
x = np.linspace(-0.1,0.1,1000)
y = np.cos(x)
plt.plot(x,y,ls='-.',lw=2,c='c',label='plot testing')
plt.legend(loc='upper left')
plt.show()