1 import matplotlib as mpl 2 import matplotlib.pyplot as plt 3 import numpy as np 4 import warnings 5 warnings.filterwarnings('ignore') 6 plt.style.use('classic') 7 x = np.linspace(0,10,100) 8 fig = plt.figure() 9 plt.plot(x,np.sin(x),'-') 10 plt.plot(x,np.cos(x),'--') 11 plt.show()
运行结果:
1 import matplotlib as mpl 2 import matplotlib.pyplot as plt 3 import numpy as np 4 import warnings 5 warnings.filterwarnings('ignore') 6 plt.style.use('classic') 7 x = np.linspace(0,10,100) 8 plt.figure() 9 plt.subplot(2,2,1) 10 plt.plot(x,np.sin(x)) 11 plt.subplot(2,2,2) 12 plt.plot(x,np.cos(x)) 13 plt.show()
运行结果:
1 import matplotlib as mpl 2 import matplotlib.pyplot as plt 3 import numpy as np 4 import warnings 5 warnings.filterwarnings('ignore') 6 plt.style.use('classic') 7 x = np.linspace(0,10,100) 8 fig,ax=plt.subplots(2)#通过轴方式画图 9 ax[0].plot(x,np.sin(x)) 10 ax[1].plot(x,np.cos(x)) 11 plt.show()
运行结果:
1 import matplotlib as mpl 2 import matplotlib.pyplot as plt 3 import numpy as np 4 import warnings 5 warnings.filterwarnings('ignore') 6 plt.style.use('classic') 7 x = np.linspace(0,10,100) 8 #底图风格 9 plt.style.use('seaborn-whitegrid') 10 fig = plt.figure() 11 ax = plt.axes() 12 plt.show()
运行结果:
1 import matplotlib as mpl 2 import matplotlib.pyplot as plt 3 import numpy as np 4 import warnings 5 warnings.filterwarnings('ignore') 6 plt.style.use('seaborn-whitegrid') 7 fig = plt.figure() 8 ax=plt.axes() 9 x = np.linspace(0,10,100) 10 ax.plot(x,np.sin(x)) 11 plt.sho
运行结果:
1 import matplotlib as mpl 2 import matplotlib.pyplot as plt 3 import numpy as np 4 import warnings 5 warnings.filterwarnings('ignore') 6 plt.style.use('seaborn-whitegrid') 7 fig = plt.figure() 8 ax=plt.axes() 9 x = np.linspace(0,10,100) 10 ax.plot(x,np.sin(x)) 11 #颜色调整 12 plt.plot(x,np.sin(x-0),color='blue') 13 plt.plot(x,np.sin(x-1),color='g') 14 plt.plot(x,np.sin(x-2),color='0.75') 15 plt.plot(x,np.sin(x-3),color='#FFC044') 16 plt.plot(x,np.sin(x-4),color=(1.0,0.2,0.3)) 17 plt.plot(x,np.sin(x-5),color='chartreuse') 18 plt.plot(x,np.sin(x-6),color='pink') 19 plt.show()
运行结果:
1 import matplotlib as mpl 2 import matplotlib.pyplot as plt 3 import numpy as np 4 import warnings 5 warnings.filterwarnings('ignore') 6 plt.style.use('seaborn-whitegrid') 7 fig = plt.figure() 8 ax=plt.axes() 9 x = np.linspace(0,10,100)11 #线条样式 12 plt.plot(x,x-0,linestyle='solid') 13 plt.plot(x,x-1,linestyle='dashed') 14 plt.plot(x,x-2,linestyle='dashdot') 15 plt.plot(x,x-3,linestyle='dotted') 16 plt.plot(x,x-4,linestyle='-')#solid 17 plt.plot(x,x-5,linestyle='--')#dashed 18 plt.plot(x,x-6,linestyle='-.')#dashdot 19 plt.plot(x,x-7,linestyle=':')#dotted 20 plt.show()
运行结果:
1 import matplotlib as mpl 2 import matplotlib.pyplot as plt 3 import numpy as np 4 import warnings 5 warnings.filterwarnings('ignore') 6 plt.style.use('seaborn-whitegrid') 7 fig = plt.figure() 8 ax=plt.axes() 9 x = np.linspace(0,10,100) 10 #标记展示 11 rng = np.random.RandomState(0) 12 for marker in ['o',',','.','x','v','^','<','>','s','d']: 13 plt.plot(rng.rand(5),rng.rand(5),marker, 14 label = "marker='{0}'".format(marker)) 15 plt.legend(numpoints=1) 16 plt.xlim(0,1.8) 17 plt.show()
运行结果:
1 import matplotlib as mpl 2 import matplotlib.pyplot as plt 3 import numpy as np 4 import warnings 5 warnings.filterwarnings('ignore') 6 plt.style.use('seaborn-whitegrid') 7 fig = plt.figure() 8 ax=plt.axes() 9 #散点图 10 x = np.linspace(0,10,30) 11 y = np.sin(x) 12 plt.plot(x,y,'o',color='red') 13 plt.show()
运行结果:
1 import matplotlib as mpl 2 import matplotlib.pyplot as plt 3 import numpy as np 4 import warnings 5 warnings.filterwarnings('ignore') 6 plt.style.use('seaborn-whitegrid') 7 fig = plt.figure() 8 ax=plt.axes() 9 #直方图 10 data = np.random.randn(1000) 11 plt.hist(data,color='g') 12 plt.show()
运行结果:
1 import matplotlib as mpl 2 import matplotlib.pyplot as plt 3 import numpy as np 4 import warnings 5 warnings.filterwarnings('ignore') 9 #直方图 10 data = np.random.randn(1000) 11 plt.hist(data,bins=30,normed=True,alpha=0.5, 12 histtype='stepfilled',color='steelblue', 13 edgecolor='none') 14 plt.show()
运行结果:
1 import matplotlib as mpl 2 import matplotlib.pyplot as plt 3 import numpy as np 4 import warnings 5 warnings.filterwarnings('ignore') 6 #直方图 7 x1 = np.random.normal(0,0.8,1000) 8 x2 = np.random.normal(-2,1,1000) 9 x3 = np.random.normal(3,2,1000) 10 kwargs = dict(histtype='stepfilled',alpha=0.3,normed=True,bins=40) 11 plt.hist(x1,**kwargs) 12 plt.hist(x2,**kwargs) 13 plt.hist(x3,**kwargs) 14 plt.show()
运行结果: