zoukankan      html  css  js  c++  java
  • matplotlib笔记3

    关于matplotlib的绘制图形的基本代码,我们可以参照下面的连接

    https://matplotlib.org/gallery/index.html

    https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplot.html#matplotlib.pyplot.subplot

    https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplots.html#matplotlib.pyplot.subplots

    在这个官网上选择你想要画的相似的模型图样点击进去就可以看代码了

    下面介绍

    注释

     1 import matplotlib.pyplot as plt
     2 import numpy as np
     3 
     4 x = np.arange(-10,11,1)
     5 
     6 y = x*x
     7 
     8 plt.plot(x, y)
     9 plt.annotate('this is the bottom',xy=(0,5),xytext=(0,20),
    10              arrowprops=dict(facecolor='r',frac=0.3,headwidth=20,width=10))
    11 # xy=(0,5)为箭头下面的点坐标,xytext=(0,20)表示说明文字的起始位置
    12 # arrowprops中的facecolor表示颜色,frac表示箭头占总符号的长度比例,
    13 # headwidth表示箭头的宽度,width表示尾部长方形的宽度
    14 
    15 plt.show()

    文字

     1 import matplotlib.pyplot as plt
     2 import numpy as np
     3 
     4 x = np.arange(-10,11,1)
     5 
     6 y = x*x
     7 
     8 plt.plot(x,y)
     9 # 两组进行对比
    10 plt.text(-2,40,'function:y=x*x',family='serif',
    11          size=20,color='r',style='italic',weight='black')
    12 plt.text(-2,20,'function:y=x*x',family='fantasy',
    13          size=20,color='g',style='oblique',weight='light',
    14          bbox=dict(facecolor='r',alpha=0.2))
    15 # family表示字体,size表示文字大小,color表示字体颜色,style表示斜体
    16 # weight表示字体的粗细,weight也可以用数字表示
    17 # bbox表示为字添加方框,方框里可以有颜色,透明度alpha描述
    18 
    19 plt.show()

    Tex公式

     1 import matplotlib.pyplot as plt
     2 
     3 fig = plt.figure()
     4 
     5 ax = fig.add_subplot(111)
     6 ax.set_xlim([1,7])
     7 ax.set_ylim([1,5])
     8 
     9 # 在每个字符串之前要加\,如alpha,这才表示数学符号,
    10 # 其它的符号在matplotlib官网上均有详细介绍
    11 ax.text(2,4,r'$ alpha_i eta_j pi lambda omega $',size=25)
    12 
    13 ax.text(4,4,r'$ sin(0)=cos(frac{pi}{2}) $',size=25)
    14 
    15 ax.text(2,2,r'$ lim_{(x 
    ightarrow y)} frac{1}{x^3} $',size=25)
    16 
    17 ax.text(4,2,r'$ sqrt[4]{x}=sqrt{y} $',size=25)
    18 
    19 
    20 plt.show()

    区域填充

     1 import matplotlib.pyplot as plt
     2 import numpy as np
     3 
     4 x=np.linspace(0,5*np.pi,100)
     5 y1=np.sin(x)
     6 y2=np.sin(2*x)
     7 
     8 # 填充的区域会有线的痕迹所以可以不画线
     9 # plt.plot(x,y1)
    10 # plt.plot(x,y2)
    11 
    12 # 在函数曲线与x轴所围区域填充颜色
    13 # plt.fill(x,y1,'b',alpha=0.3)
    14 # plt.fill(x,y2,'r',alpha=0.3)
    15 
    16 fig=plt.figure()
    17 ax=plt.gca()
    18 ax.plot(x,y1,color='r')
    19 ax.plot(x,y2,color='b')
    20 
    21 # 将颜色填充到两个函数曲线所围区域
    22 ax.fill_between(x,y1,y2,where=y1>=y2,facecolor='y',interpolate=True)
    23 ax.fill_between(x,y1,y2,where=y1<y2,facecolor='g',interpolate=True)
    24 # where是条件表达式,
    25 # 当曲线不够平滑interpolate=True自动将空白的地方填充完整
    26 
    27 
    28 plt.show()

    形状

     1 import matplotlib.pyplot as plt
     2 import numpy as np
     3 import matplotlib.patches as mpatches
     4 
     5 fig, ax=plt.subplots() 
     6 
     7 # 先确定中心坐标
     8 xy1=np.array([0.2,0.2])
     9 xy2=np.array([0.2,0.8])
    10 xy3=np.array([0.8,0.2])
    11 xy4=np.array([0.8,0.8])
    12 
    13 circle=mpatches.Circle(xy1,0.05)  # 画圆
    14 ax.add_patch(circle)
    15 
    16 rect=mpatches.Rectangle(xy2,0.2,0.2,0.1,color='r')  # 画矩形
    17 ax.add_patch(rect)
    18 
    19 polygon=mpatches.RegularPolygon(xy3,5,0.1,color='g')  # 画多边形
    20 ax.add_patch(polygon)
    21 
    22 ellipse=mpatches.Ellipse(xy4,0.4,0.2,color='y')  # 画椭圆
    23 ax.add_patch(ellipse)
    24 
    25 # matplotlib.patches文档
    26 
    27 plt.axis('equal')  # x轴与y轴比例要相等
    28 plt.grid()
    29 
    30 plt.show()

    样式-美化

     1 import matplotlib.pyplot as plt
     2 import numpy as np
     3 
     4 plt.style.use('ggplot')  # 为所绘制图选择风格
     5 fig, axes = plt.subplots(nrows=2, ncols=2, constrained_layout=True)
     6 ax1,ax2,ax3,ax4=axes.ravel()
     7 x,y=np.random.normal(size=(2,100))
     8 ax1.plot(x,y,'o')
     9 
    10 x=np.arange(0,10)
    11 y=np.arange(0,10)
    12 
    13 ncolors=len(plt.rcParams['axes.prop_cycle'])
    14 # prop_cycle它会有7种颜色可以循环
    15 shift=np.linspace(0,10,ncolors)
    16 
    17 for s in shift:
    18     ax2.plot(x,y+s,'-')
    19 
    20 x=np.arange(5)
    21 y1,y2,y3=np.random.randint(1,25,size=(3,5))
    22 width=0.25
    23 
    24 ax3.bar(x,y1,width)
    25 ax3.bar(x+width,y2,width)
    26 ax3.bar(x+2*width,y2,width)
    27 
    28 colors=plt.rcParams['axes.prop_cycle']
    29 for i, color in enumerate(colors):
    30     xy=np.random.normal(size=2)
    31     ax4.add_patch(plt.Circle(xy,radius=0.3,color=color['color']))
    32 
    33 ax4.axis('equal')
    34 plt.show()

    极坐标

     1 import matplotlib.pyplot as plt
     2 import numpy as np
     3 
     4 # r=np.empty(5)
     5 r=np.empty(9)
     6 r.fill(5)
     7 # r=np.arange(1,6,1)
     8 pi_two=np.pi*2
     9 
    10 # 通过极坐标绘制正8边形
    11 # 通过列表推导式传入多个角
    12 theta=[x*pi_two/8 for x in range(9)]
    13 
    14 # theta=[0,np.pi/2,np.pi,3*np.pi/2,2*np.pi]
    15 
    16 ax=plt.subplot(111,projection='polar')
    17 
    18 ax.plot(theta,r,color='r',linewidth=3)
    19 
    20 ax.grid(True)
    21 
    22 plt.show()

    函数几分图

     1 import matplotlib.pyplot as plt
     2 import numpy as np
     3 from matplotlib.patches import Polygon
     4 
     5 def func(x):
     6     return -(x-2)*(x-8)+40
     7 
     8 x=np.linspace(0,10)
     9 
    10 y=func(x)
    11 
    12 fig,ax=plt.subplots()
    13 
    14 plt.plot(x,y,'r',linewidth=1)
    15 
    16 a=2
    17 b=9
    18 ax.set_xticks([a,b])
    19 ax.set_yticks([])
    20 ax.set_xticklabels(['$a$','$b$'])
    21 
    22 ix=np.linspace(a,b)
    23 iy=func(ix)
    24 
    25 ixy=zip(ix,iy)
    26 verts=[(a,0)]+list(ixy)+[(b,0)]
    27 poly=Polygon(verts,facecolor='0.9',edgecolor='0.5')
    28 ax.add_patch(poly)
    29 
    30 plt.figtext(0.9,0.05,'$x$')
    31 plt.figtext(0.1,0.9,'$y$')
    32 
    33 x_math=(a+b)*0.5
    34 y_math=30
    35 
    36 plt.text(x_math,y_math,r'$int_a^b (-(x-2)*(x-8)+40)$',
    37          fontsize=20,horizontalalignment='center')
    38 # horizontalalignment='center',会让公式自动中心对齐
    39 plt.ylim(ymin=25)
    40 
    41 plt.show()
  • 相关阅读:
    create joint
    delphi 使用parent让进度条上显示文字
    abSymMeshMEL.txt
    ini写配置信息
    CreateBindGroupNode.txt
    CreateaJointCurve.txt
    09 IKFKMatch.txt
    TIF_to_MAP.BAT
    ImportBVHv20.txt
    FormatDateTime 一段以时间为命令的代码
  • 原文地址:https://www.cnblogs.com/yang901112/p/11440614.html
Copyright © 2011-2022 走看看