py - 绘图(2)

目灵

  • pyplot API
  • 面向对象的API
    • figure
    • axes
    • subplots


import matplotlib.pyplot as plt
plt.close('all')
  • pyplot API
    pyplot主要用于交互式绘图和程序化绘图生成的简单案例。每个pyplot函数都会对图形做一定的修改。
    pyplot案例。

  • 面向对象的API
    matplotlib的核心是面向对象的,建议直接使用对象
    一般创建一个Figure和一个或多个Axes

    • Figure
      注:如果要创建多个图形,需要显示调用plt.close(),使其正确清理内存。

    matplotlib.pyplot.figure(num = None,figsize = None,dpi = None,facecolor = None,edgecolor = None,frameon = True,FigureClass = <class’matplotlib.figure.Figure’>,clear = False,** kwargs )
    – num:如未提供,创建新的图形,且图形编号递增;如已存在,则激活;
    – figsize:(float, float),宽高,默认(6.4, 4.8);
    – dpi:图的分辨率;
    – facecolor:背景颜色;
    – edgecolor:边框颜色;
    – frameon:默认True,如为False则禁止绘制图形框;
    – FigureClass:可选自定义实例;
    – clear:默认False,如为True且num数字存在则删除;
    – **kwargs:其它。

    1. fig.add_axes()
      在图中增加轴。

      add_axes(rect, projection=None, polar=False, sharex=False, sharey=False, label=None, **kwargs)
      – rect: sequence(left, bottom, width, heigh)
      – projection: 默认None,{None, ‘ploar’, ‘aitoff’, str, …}
      add_axes(axes)

      x = np.arange(100)
      y = np.sin(x)
      
      fig = plt.figure()
      ax = fig.add_axes([1, 1, 1, 1])
      ax.axes.plot(x, y)
      

      在这里插入图片描述

    2. fig.add_gridsped()
      返回GridSpec父类。指定可以放置子图的网格的几何形状。

      add_gridspec(nrows, ncols, **kwargs)

      x = np.arange(10)
      y = np.sin(x)
      
      fig = plt.figure()
      
      gs = fig.add_gridspec(1, 2)
      
      ax1 = fig.add_subplot(gs[0])
      ax2 = fig.add_subplot(gs[1])
      
      ax1.axes.plot(x)
      ax2.axes.plot(y)
      
      fig.show()
      

      在这里插入图片描述

    3. fig.add_subplot()
      增加一个子图。

      add_subplot(self, *args, **kwargs)
      add_subplot(nrows, ncols, index, **kwargs)
      add_subplot(pos, **kwargs)
      add_subplot(ax)
      add_subplot()

    • Axes
      在当前图上添加轴,并使其成为当前轴。
      不建议:如果图中已存在一个带有键的轴(args,kwargs),那么它将简单地成为当前轴并返回它。如果要修改轴,必须使用一组唯一的(args, kwargs)。

    matplotlib.pyplot.axes(arg = None,polar=None, ** kwargs )
    arg: 1)None,添加一个全窗口轴。2)[ left, bottom, width, height ];
    polar: True,极坐标;
    sharex, sharey: 共享x轴、或y轴,该轴具有相同的限制;
    axes: Axes,或子类Axes.
    **kwargs 提供更多关键字参数。

    # 感受axes的用法
    np.random.seed(100)
    x = np.random.randn(100)
    y = np.random.randn(100)
    
    fig = plt.figure
    ax1 = plt.axes()
    ax2 = plt.axes()
    
    ax1.plot(x)
    ax2.plot(y)
    
    # 图1
    plt.show()
    
    # 图2
    # 修改
    ax1 = plt.axes((1, 1, 1, 1))
    ax2 = plt.axes((2, 2, 2, 2))
    

    在这里插入图片描述
    在这里插入图片描述

    • plt.subplots()参数详解
      创建一个Figure对象和一组子图。

    matplotlib.pyplot.subplots(nrows = 1,ncols = 1,sharex = False,sharey = False,squeeze = True,subplot_kw = None,gridspec_kw = None,** fig_kw )


x = np.linspace(0, 2*np.pi, 400)
y = np.sin(x**2)

fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('Simple plot')
Out[7]: Text(0.5, 1.0, 'Simple plot')

在这里插入图片描述


fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x, y)
ax1.set_title('Sharing Y axis')
ax2.scatter(x, y)
Out[8]: <matplotlib.collections.PathCollection at 0x7753630>

在这里插入图片描述


fig, ax = plt.subplots(2, 2, subplot_kw=dict(polar=True))
ax[0, 0].plot(x, y)
ax[1, 1].scatter(x, y)
Out[9]: <matplotlib.collections.PathCollection at 0xa6cf9b0>

在这里插入图片描述


plt.subplots(2, 2, sharex='col')

plt.subplots(2, 2, sharey='row')

plt.subplots(2, 2, sharex='all', sharey='all')

plt.subplots(2, 2, sharex=True, sharey=True)

在这里插入图片描述


# No. 10 with a single subplot
# clear=True, if exists delete it.
fig, ax = plt.subplots(num=10, clear=True)

在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值