zoukankan      html  css  js  c++  java
  • 【Matplotlib】详解图像各个部分

    首先一幅Matplotlib的图像组成部分介绍。

    在matplotlib中,整个图像为一个Figure对象。在Figure对象中可以包含一个或者多个Axes对象。每个Axes(ax)对象都是一个拥有自己坐标系统的绘图区域。所属关系如下:

    下面以一个直线图来详解图像内部各个组件内容:

    其中:title为图像标题,Axis为坐标轴, Label为坐标轴标注,Tick为刻度线,Tick Label为刻度注释。各个对象关系可以梳理成以下内容:

    图像中所有对象均来自于Artist的基类。

    上面基本介绍清楚了图像中各个部分的基本关系,下面着重讲一下几个部分的详细的设置。

    一个"Figure"意味着用户交互的整个窗口。在这个figure中容纳着"subplots"。

    当我们调用plot时,matplotlib会调用gca()获取当前的axes绘图区域,而且gca反过来调用gcf()来获得当前的figure。如果figure为空,它会自动调用figure()生成一个figure, 严格的讲,是生成subplots(111)

    Figures

    Subplots

    plt.subplot(221) # 第一行的左图
    plt.subplot(222) # 第一行的右图
    plt.subplot(212) # 第二整行
    plt.show()
    

    注意:其中各个参数也可以用逗号,分隔开。第一个参数代表子图的行数;第二个参数代表该行图像的列数; 第三个参数代表每行的第几个图像。

    另外:fig, ax = plt.subplots(2,2),其中参数分别代表子图的行数和列数,一共有 2x2 个图像。函数返回一个figure图像和一个子图ax的array列表。

    补充:gridspec命令可以对子图区域划分提供更灵活的配置。

    Tick Locators

    Tick Locators 控制着 ticks 的位置。比如下面:

    ax = plt.gca()
    ax.xaxis.set_major_locator(eval(locator))
    

    一些不同类型的locators:

    代码如下:

    import numpy as np
    import matplotlib.pyplot as plt
    
    
    def tickline():
    plt.xlim(0, 10), plt.ylim(-1, 1), plt.yticks([])
    ax = plt.gca()
    ax.spines['right'].set_color('none')
    ax.spines['left'].set_color('none')
    ax.spines['top'].set_color('none')
    ax.xaxis.set_ticks_position('bottom')
    ax.spines['bottom'].set_position(('data',0))
    ax.yaxis.set_ticks_position('none')
    ax.xaxis.set_minor_locator(plt.MultipleLocator(0.1))
    ax.plot(np.arange(11), np.zeros(11))
    return ax
    
    locators = [
    'plt.NullLocator()',
    'plt.MultipleLocator(1.0)',
    'plt.FixedLocator([0, 2, 8, 9, 10])',
    'plt.IndexLocator(3, 1)',
    'plt.LinearLocator(5)',
    'plt.LogLocator(2, [1.0])',
    'plt.AutoLocator()',
    ]
    
    n_locators = len(locators)
    
    size = 512, 40 * n_locators
    dpi = 72.0
    figsize = size[0] / float(dpi), size[1] / float(dpi)
    fig = plt.figure(figsize=figsize, dpi=dpi)
    fig.patch.set_alpha(0)
    
    
    for i, locator in enumerate(locators):
    plt.subplot(n_locators, 1, i + 1)
    ax = tickline()
    ax.xaxis.set_major_locator(eval(locator))
    plt.text(5, 0.3, locator[3:], ha='center')
    
    plt.subplots_adjust(bottom=.01, top=.99, left=.01, right=.99)
    plt.show()
    

    所有这些locators均来自于基类 matplotlib.ticker.Locator。你可以通过继承该基类创建属于自己的locator样式。同时matplotlib也提供了特殊的日期locator, 位于matplotlib.dates.

  • 相关阅读:
    poj3067 Japan(树状数组)
    Codeforces 482C Game with Strings(dp+概率)
    LeetCode -- 推断链表中是否有环
    螺旋矩阵——正逆序
    POJ 3905 Perfect Election(2-sat)
    设计模式 之 桥接
    Codeforces Round #257 (Div. 2)
    [LeetCode][Java] Minimum Window Substring
    Unity特殊目录和脚本编译顺序
    jQuery插件 -- Cookie插件
  • 原文地址:https://www.cnblogs.com/nju2014/p/5620776.html
Copyright © 2011-2022 走看看