zoukankan      html  css  js  c++  java
  • matplotlib画图

    1)plt.subplots()

    fig,ax = plt.subplots()的意思是,建立一个fig对象,建立一个axis对象。

    pyplot有当前的图形(figure)和当前的轴(axes)的概念,所有的作图命令都是对当前的对象作用。

    2)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列表。

    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.

  • 相关阅读:
    zookeeper ACL(access control lists)权限控制
    zookeeper伪分布式集群搭建
    云服务器离线安装MariaDB安装步骤和解决办法
    云服务器——之Linux下安装tomcat
    云服务器之——Linux下配置JDK环境
    nginx安装与fastdfs配置--阿里云
    fastDFS 一二事
    云服务器 ECS--查找公网ip使用终端连接云服务
    springboot oauth 鉴权之——password、authorization_code鉴权
    springboot oauth 鉴权之——授权码authorization_code鉴权
  • 原文地址:https://www.cnblogs.com/djx571/p/9512675.html
Copyright © 2011-2022 走看看