zoukankan      html  css  js  c++  java
  • matplotlib 进阶之origin and extent in imshow

    matplotlib教程学习笔记

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.gridspec import GridSpec
    
    
    def index_to_coordinate(index, extent, origin):
        """Return the pixel center of an index."""
        left, right, bottom, top = extent
    
        hshift = 0.5 * np.sign(right - left)
        left, right = left + hshift, right - hshift
        vshift = 0.5 * np.sign(top - bottom)
        bottom, top = bottom + vshift, top - vshift
    
        if origin == 'upper':
            bottom, top = top, bottom
    
        return {
            "[0, 0]": (left, bottom),
            "[M', 0]": (left, top),
            "[0, N']": (right, bottom),
            "[M', N']": (right, top),
        }[index]
    
    
    def get_index_label_pos(index, extent, origin, inverted_xindex):
        """
        Return the desired position and horizontal alignment of an index label.
        """
        if extent is None:
            extent = lookup_extent(origin)
        left, right, bottom, top = extent
        x, y = index_to_coordinate(index, extent, origin)
    
        is_x0 = index[-2:] == "0]"
        halign = 'left' if is_x0 ^ inverted_xindex else 'right'
        hshift = 0.5 * np.sign(left - right)
        x += hshift * (1 if is_x0 else -1)
        return x, y, halign
    
    
    def get_color(index, data, cmap):
        """Return the data color of an index."""
        val = {
            "[0, 0]": data[0, 0],
            "[0, N']": data[0, -1],
            "[M', 0]": data[-1, 0],
            "[M', N']": data[-1, -1],
        }[index]
        return cmap(val / data.max())
    
    
    def lookup_extent(origin):
        """Return extent for label positioning when not given explicitly."""
        if origin == 'lower':
            return (-0.5, 6.5, -0.5, 5.5)
        else:
            return (-0.5, 6.5, 5.5, -0.5)
    
    
    def set_extent_None_text(ax):
        ax.text(3, 2.5, 'equals
    extent=None', size='large',
                ha='center', va='center', color='w')
    
    
    def plot_imshow_with_labels(ax, data, extent, origin, xlim, ylim):
        """Actually run ``imshow()`` and add extent and index labels."""
        im = ax.imshow(data, origin=origin, extent=extent)
    
        # extent labels (left, right, bottom, top)
        left, right, bottom, top = im.get_extent()
        if xlim is None or top > bottom:
            upper_string, lower_string = 'top', 'bottom'
        else:
            upper_string, lower_string = 'bottom', 'top'
        if ylim is None or left < right:
            port_string, starboard_string = 'left', 'right'
            inverted_xindex = False
        else:
            port_string, starboard_string = 'right', 'left'
            inverted_xindex = True
        bbox_kwargs = {'fc': 'w', 'alpha': .75, 'boxstyle': "round4"}
        ann_kwargs = {'xycoords': 'axes fraction',
                      'textcoords': 'offset points',
                      'bbox': bbox_kwargs}
        ax.annotate(upper_string, xy=(.5, 1), xytext=(0, -1),
                    ha='center', va='top', **ann_kwargs)
        ax.annotate(lower_string, xy=(.5, 0), xytext=(0, 1),
                    ha='center', va='bottom', **ann_kwargs)
        ax.annotate(port_string, xy=(0, .5), xytext=(1, 0),
                    ha='left', va='center', rotation=90,
                    **ann_kwargs)
        ax.annotate(starboard_string, xy=(1, .5), xytext=(-1, 0),
                    ha='right', va='center', rotation=-90,
                    **ann_kwargs)
        ax.set_title('origin: {origin}'.format(origin=origin))
    
        # index labels
        for index in ["[0, 0]", "[0, N']", "[M', 0]", "[M', N']"]:
            tx, ty, halign = get_index_label_pos(index, extent, origin,
                                                 inverted_xindex)
            facecolor = get_color(index, data, im.get_cmap())
            ax.text(tx, ty, index, color='white', ha=halign, va='center',
                    bbox={'boxstyle': 'square', 'facecolor': facecolor})
        if xlim:
            ax.set_xlim(*xlim)
        if ylim:
            ax.set_ylim(*ylim)
    
    
    def generate_imshow_demo_grid(extents, xlim=None, ylim=None):
        N = len(extents)
        fig = plt.figure(tight_layout=True)
        fig.set_size_inches(6, N * (11.25) / 5)
        gs = GridSpec(N, 5, figure=fig)
    
        columns = {'label': [fig.add_subplot(gs[j, 0]) for j in range(N)],
                   'upper': [fig.add_subplot(gs[j, 1:3]) for j in range(N)],
                   'lower': [fig.add_subplot(gs[j, 3:5]) for j in range(N)]}
        x, y = np.ogrid[0:6, 0:7]
        data = x + y
    
        for origin in ['upper', 'lower']:
            for ax, extent in zip(columns[origin], extents):
                plot_imshow_with_labels(ax, data, extent, origin, xlim, ylim)
    
        for ax, extent in zip(columns['label'], extents):
            text_kwargs = {'ha': 'right',
                           'va': 'center',
                           'xycoords': 'axes fraction',
                           'xy': (1, .5)}
            if extent is None:
                ax.annotate('None', **text_kwargs)
                ax.set_title('extent=')
            else:
                left, right, bottom, top = extent
                text = ('left: {left:0.1f}
    right: {right:0.1f}
    ' +
                        'bottom: {bottom:0.1f}
    top: {top:0.1f}
    ').format(
                            left=left, right=right, bottom=bottom, top=top)
    
                ax.annotate(text, **text_kwargs)
            ax.axis('off')
        return columns
    
    generate_imshow_demo_grid(extents=[None]);
    

    在这里插入图片描述

    通常来说,对于shape(M, N)来讲,M是沿着竖直方向的,而N是沿着水平方向的。
    origin参数觉得了其实位置:

    对于 origin="lower":

    [0, 0] 在 (left, bottom)位置

    [M, 0] 在 (left, upper)位置

    [0, N] 在 (right, bottom)位置

    [M, N] 在 (right, top) 位置

    实际上就是,从左下角往右上角发展

    而对于orgin="upper",则是从左上角往右下角发展

    显示的extent

    extent是控制图片的坐标轴的工具,为(left, right, bottom, top)
    就是控制x轴为: left -> right
    y轴为: bottom -> top

    extents = [(-0.5, 6.5, -0.5, 5.5),
               (-0.5, 6.5, 5.5, -0.5),
               (6.5, -0.5, -0.5, 5.5),
               (6.5, -0.5, 5.5, -0.5)]
    
    columns = generate_imshow_demo_grid(extents)
    set_extent_None_text(columns['upper'][1])
    set_extent_None_text(columns['lower'][0])
    

    在这里插入图片描述

    Explicit extent and axes limits

    搞不懂了啊,为什么加了limits之后,可以随便转来转去了啊不知道,就这样吧

    generate_imshow_demo_grid(extents=[None] + extents,
                              xlim=(-2, 8), ylim=(-1, 6));
    

    在这里插入图片描述

  • 相关阅读:
    JS之事件及冒泡
    DOM读取和修改内联样式
    dom查询与修改的一些常用方法
    js修改this指向的三种方法(call,bind,apply)
    JS原型概念
    JS创建对象
    JS的this(谁调用就指向谁)
    变量声明提前与函数声明提前
    JS对象创建
    正则应用之数据采集房屋网站信息
  • 原文地址:https://www.cnblogs.com/MTandHJ/p/10890905.html
Copyright © 2011-2022 走看看