目录html
Matplotlib是一个强大的可视化工具,是Python的绘图库,可与NumPy一块儿使用,提供了一种有效的MatLab开源替代方案,用来画图真的不要太香!python
下面总结出经常使用的操做以及技巧,保证每一个例子的代码均可以直接拿来运行。更多内容请查看官网api
import numpy as np import matplotlib.pyplot as plt x=np.arange(0,10) plt.title('chenqionghe') plt.plot(x,x*x) plt.show()
官方文档
设置坐标和文字便可dom
import numpy as np import matplotlib.pyplot as plt x=np.arange(-10,11,1) y=x*x plt.plot(x,y) plt.title('chenqionghe') plt.text(-2.5,30,'function y=x*x') plt.show()
官方文档函数
import numpy as np import matplotlib.pyplot as plt x=np.arange(-10,11,1) y=x*x plt.title('chenqionghe') plt.plot(x,y) plt.annotate('chenqionghe is a kind man',xy=(0,1),xytext=(-4,20),arrowprops={'headwidth':10,'facecolor':'r'}) plt.show()
import numpy as np import matplotlib.pyplot as plt x=np.arange(1,20) plt.xlabel('chenqionghe') plt.ylabel('muscle') plt.plot(x,x*x) plt.show()
官方文档工具
import numpy as np import matplotlib.pyplot as plt plt.plot(x,x) plt.plot(x,x*2) plt.plot(x,x*3) plt.plot(x,x*4) # 直接传入legend plt.legend(['chenqionghe','light','weight','baby']) plt.show()
传颜色参数,支持如下几种方式post
import numpy as np import matplotlib.pyplot as plt x=np.arange(1,5) #颜色的几种方式 plt.plot(x,color='g') plt.plot(x+1,color='0.5') plt.plot(x+2,color='#FF00FF') plt.plot(x+3,color=(0.1,0.2,0.3)) plt.show()
更多样式查看官方文档spa
import numpy as np import matplotlib.pyplot as plt x=np.arange(1,5) plt.plot(x,marker='o') plt.plot(x+1,marker='>') plt.plot(x+2,marker='s') plt.show()
全部公式符号
格式以下:
$做为开始和结束符,如$ \omega $,中间的将解析出公式中的符号3d
import numpy as np import matplotlib.pyplot as plt plt.title('chenqionghe') plt.xlim([1,8]) plt.ylim([1,5]) plt.text(2,4,r'$ \alpha \beta \pi \lambda \omega $',size=25) plt.text(4,4,r'$ \sin(0)=\cos(\frac{\pi}{2}) $',size=25) plt.text(2,2,r'$ \lim_{x \rightarrow y} \frac{1}{x^3} $',size=25) plt.text(4,2,r'$ \sqrt[4]{x}=\sqrt{y} $',size=25) plt.show()
import numpy as np import matplotlib.pyplot as plt x='chenqionghe','light','weigtht','baby' y=[15,30,45,10] plt.grid() # 也能够设置颜色、线条宽度、线条样式 # plt.grid(color='g',linewidth='1',linestyle='-.') plt.plot(x,y) plt.show()
同时调整x轴和y轴:plt.locator_params(nbins=20)
只调整x轴:plt.locator_params(‘'x',nbins=20)
只调整y轴:plt.locator_params(‘'y',nbins=20)code
示例代码
import numpy as np import matplotlib.pyplot as plt x=np.arange(0,30,1) plt.plot(x,x) # x轴和y轴分别显示20个 plt.locator_params(nbins=20) plt.show()
示例代码
import numpy as np import matplotlib.pyplot as plt x=np.arange(0,30,1) plt.plot(x,x*x) #显示坐标轴,plt.axis(),4个数字分别表明x轴和y轴的最小坐标,最大坐标 #调整x为10到25 plt.xlim(xmin=10,xmax=25) plt.plot(x,x*x) plt.show()
有时候显示日期会重叠在一块儿,很是不友好,调用plt.gcf().autofmt_xdate(),将自动调整角度
import numpy as np import pandas as pd import matplotlib.pyplot as plt x=pd.date_range('2020/01/01',periods=30) y=np.arange(0,30,1) plt.plot(x,y) plt.gcf().autofmt_xdate() plt.show()
import numpy as np import matplotlib.pyplot as plt x=np.arange(1,20) y1=x*x y2=np.log(x) plt.plot(x,y1) # 添加一个坐标轴,默认0到1 plt.twinx() plt.plot(x,y2,'r') plt.show()
fill填充函数区域
import numpy as np import matplotlib.pyplot as plt x=np.linspace(0,5*np.pi,1000) y1=np.sin(x) y2=np.sin(2*x) plt.plot(x,y1) plt.plot(x,y2) plt.fill(x,y1,'g') plt.fill(x,y2,'r') plt.title('chenqionghe') plt.show()
fill_beween填充函数交叉区域
import numpy as np import matplotlib.pyplot as plt plt.title('chenqionghe') x=np.linspace(0,5*np.pi,1000) y1=np.sin(x) y2=np.sin(2*x) plt.plot(x,y1) plt.plot(x,y2) plt.fill_between(x,y1,y2,where=y1>y2,interpolate=True) plt.show()
各类形状参考官方文档
import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as mptaches xy1=np.array([0.2,0.2]) xy2=np.array([0.2,0.8]) xy3=np.array([0.8,0.2]) xy4=np.array([0.8,0.8]) fig,ax=plt.subplots() #圆形,指定坐标和半径 circle=mptaches.Circle(xy1,0.15) ax.add_patch(circle) #长方形 rect=mptaches.Rectangle(xy2,0.2,0.1,color='r') ax.add_patch(rect) #多边形 polygon=mptaches.RegularPolygon(xy3,6,0.1,color='g') ax.add_patch(polygon) # 椭圆 ellipse=mptaches.Ellipse(xy4,0.4,0.2,color='c') ax.add_patch(ellipse) ax.axis('equal') plt.show()
matplotlib支持多种样式,能够经过plt.style.use切换样式,例如:
plt.style.use('ggplot')
输入plt.style.available
能够查看全部的样式
plt.style.available ['seaborn-dark', 'seaborn-darkgrid', 'seaborn-ticks', 'fivethirtyeight', 'seaborn-whitegrid', 'classic', '_classic_test', 'fast', 'seaborn-talk', 'seaborn-dark-palette', 'seaborn-bright', 'seaborn-pastel', 'grayscale', 'seaborn-notebook', 'ggplot', 'seaborn-colorblind', 'seaborn-muted', 'seaborn', 'Solarize_Light2', 'seaborn-paper', 'bmh', 'tableau-colorblind10', 'seaborn-white', 'dark_background', 'seaborn-poster', 'seaborn-deep']
示例代码
import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as mptaches plt.style.use('ggplot') # 新建4个子图 fig,axes=plt.subplots(2,2) ax1,ax2,ax3,ax4=axes.ravel() # 第一个图 x,y=np.random.normal(size=(2,100)) ax1.plot(x,y,'o') # 第二个图 x=np.arange(0,10) y=np.arange(0,10) colors=plt.rcParams['axes.prop_cycle'] length=np.linspace(0,10,len(colors)) for s in length: ax2.plot(x,y+s,'-') # 第三个图 x=np.arange(5) y1,y2,y3=np.random.randint(1,25,size=(3,5)) width=0.25 ax3.bar(x,y1,width) ax3.bar(x+width,y2,width) ax3.bar(x+2*width,y3,width) # 第四个图 for i,color in enumerate(colors): xy=np.random.normal(size=2) ax4.add_patch(plt.Circle(xy,radius=0.3,color=color['color'])) ax4.axis('equal') plt.show()
默认样式
切换成ggplot样式后
到这里,经常使用的技巧已经差很少,建议最好本身运行一下加深印象,更多技巧能够查看下面的文章