matplotlib从起点出发(14)_Tutorial_imshow_origin_extent

news/2024/7/7 21:10:41

0 总述

imshow()允许你将图像(将进行颜色映射——基于norm和cmap——的2D数组或将按原样使用的3D RGB(A)的数组)渲染到数据空间中的矩形区域。最终渲染中图像的方向由原点和范围关键字参数(以及生成的AxesImage实例上的属性)和Axes的数据限制控制。

extent关键字参数控制数据坐标中的边界框,图像将填充该边界框在数据坐标中指定为(左、右、上、下),origin关键字参数控制图像填充该边界框的方式,最终渲染图像中的方向也受Axes限制的影响。

提示
下面的大部分代码用于向绘图添加标签和信息文本,所描述的原点和范围的影响可以在图中看到,而无需遵循所有的代码细节。
为了快速理解,你可能希望跳过下面的代码详细信息,直接继续讨论结果。

import matplotlib.pyplot as plt
import numpy as np

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\nextent=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(f'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)

    columns['label'][0].set_title('extent=')
    for ax, extent in zip(columns['label'], extents):
        if extent is None:
            text = 'None'
        else:
            left, right, bottom, top = extent
            text = (f'left: {left:0.1f}\nright: {right:0.1f}\n'
                    f'bottom: {bottom:0.1f}\ntop: {top:0.1f}\n')
        ax.text(1., .5, text, transform=ax.transAxes, ha='right', va='center')
        ax.axis('off')
    return columns

1 默认范围

首先,让我们看一下默认范围 default extent = None

generate_imshow_demo_grid(extents=[None])

在这里插入图片描述

通常,对于形状(M, N)的数组,第一个索引沿垂直方向运行,第二个索引沿水平方向运行。像素中心位于整数位置,水平范围为0到N'= N - 1,垂直范围为0到M'= M - 1。origin确定如何在边界框中填充边框。

对于origin = 'lower':

  • [0, 0]对应的是(左,下)
  • [M', 0]对应的是(左,上)
  • [0, N']对应的是(右,下)
  • [M', N']对应的是(右,上)

对于origin = 'upper' 将会反转垂直轴的方向和填充:

  • [0, 0]对应的是(左,上)
  • [M', 0]对应的是(左,下)
  • [0, N']对应的是(右,上)
  • [M', N']对应的是(右,下)

总之,[0, 0]的位置和范围受到origin的影响:

origin[0, 0]位置范围
upper上左(-0.5, numcols-0.5, numrows-0.5, -0.5)
lower下左(-0.5, numcols-0.5, -.05, numrows-0.5)

origin的默认值由rcParams["image.origin"](默认值为’upper’)设置,默认为’upper’以匹配数学和计算机图像索引约定中的矩阵索引约定。

2 显式范围

通过设置范围,我们定义图像区域的坐标。对基础图像数据进行插值/重采样以填充该区域。

如果Axes设置为自动缩放,则Axes的视图限制将设置为范围匹配,从而确保(left, bottom)设置的坐标位于Axes的左下角。但是,这可能会反转轴,因此它们不会在“自然”方向上增加:

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])

在这里插入图片描述

3 显式确定范围和Axes极限

如果我们通过显式设置set_xlim/set_ylim来固定Axes极限,我们会强制Axes的一定大小和方向。这可以将图像的“左-右”和“上-下”感觉与屏幕上的方向分离。

在下面的示例中,我们选择了略大于范围的极限(注意Axes内的白色区域)。

虽然我们保留了前面示例中的范围,但坐标(0,0)现在显式地放在左下角,值向上和向右增加(从查看者的角度来看)。我们可以看到:

  • 坐标(左,下)锚定图像,然后填充朝向数据空间中(右,上)点的框;
  • 第一列始终最靠近“左”;
  • 原点控制第一行是最接近“顶部”还是“底部”;
  • 图像可以沿任一方向反转;
  • 图像的“左-右”和“自上-下”感觉可能与屏幕上的方向无关。

在这里插入图片描述

在这里插入图片描述


http://lihuaxi.xjx100.cn/news/2063045.html

相关文章

蓝桥杯复习之二分法与前缀和

题目&#xff1a;最佳牛围栏 题目链接&#xff1a;https://www.acwing.com/problem/content/104/ 题意&#xff1a;农夫约翰的农场由 N 块田地组成&#xff0c;每块地里都有一定数量的牛&#xff0c;其数量不会少于 1 头&#xff0c;也不会超过 2000 头。 约翰希望用围栏将一…

40-组合总和2(回溯算法)

题目 给定一个候选人编号的集合 candidates 和一个目标数 target &#xff0c;找出 candidates 中所有可以使数字和为 target 的组合。 candidates 中的每个数字在每个组合中只能使用 一次 。 注意&#xff1a;解集不能包含重复的组合。 示例 1: 输入: candidates [10,1,2,7,6…

06 OpenCV增加图像的对比度

文章目录 理论API代码 理论 图像变换可以看作如下&#xff1a; 像素变换 – 点操作邻域操作 – 区域 调整图像亮度和对比度属于像素变换-点操作 API saturate_cast(value)确保值大小范围为0~255之间Mat.at(y,x)[index]value 给每个像素点每个通道赋值 代码 #include <…

2024免费mac苹果电脑的清理和维护软件CleanMyMac X

对于 Mac 用户来说&#xff0c;电脑的清理和维护是一件让人头疼的事情。但是&#xff0c;有了 CleanMyMac X&#xff0c;这一切都将变得轻松愉快。CleanMyMac X 是一款专为 Mac 设计的电脑清理软件&#xff0c;它以其强大的功能和简单的操作&#xff0c;让无数用户为之倾倒。 C…

【未完结】nginx+hexo搭建个人网站真实体验!

自从之前学习Vue和SSM的时候写了个博客项目之后&#xff0c;就特别想做一个自己的博客网站。可能是出于拖延心理以及各种任务安排时间繁忙&#xff0c;一直没有抽时间去弄。博客笔记基本上都是直接使用csdn写的&#xff0c;除此之外&#xff0c;还经常在这些网站上写笔记&#…

蓝桥OJ 2942数字王国之军训排队 DFS剪枝

蓝桥OJ 2942数字王国之军训排队 #include<bits/stdc.h> using namespace std;const int N 15;//最多10队 int a[N], n; vector<int>v[N];//二维数组 v[i]记录队伍i中所有人的编号bool dfs(int cnt, int dep) {if (dep n1){//判断合法性for (int i 1; i < n; …

未来技术发展与情绪消除的可能性——探索人工智能对情绪调控的新路径

随着科技的飞速进步&#xff0c;特别是人工智能(AI)和神经科学领域的突破&#xff0c;关于是否存在一种未来机器能够消除人类情绪的问题引发了广泛的社会讨论与伦理思考。本文旨在探讨这一设想的可能性、潜在影响以及相关伦理挑战。 首先&#xff0c;从技术角度看&#xff0c;科…

5.40 BCC工具之ustat.py解读

一,工具简介 ustat是一个类似于“top”的工具,用于监控高级语言中的事件。它为每个使用Java、Node、Perl、PHP、Python、Ruby和Tcl运行时的进程打印关于垃圾收集、方法调用、对象分配以及各种其他事件的统计信息。 二,代码示例 #!/usr/bin/pythonfrom __future__ import …