matplotlib 进阶之origin and extent in imshow
文章目录
- 显示的extent
- Explicit extent and axes limits
matplotlib教程学习笔记
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpecdef index_to_coordinate(index, extent, origin):"""Return the pixel center of an index."""left, right, bottom, top = extenthshift = 0.5 * np.sign(right - left)left, right = left + hshift, right - hshiftvshift = 0.5 * np.sign(top - bottom)bottom, top = bottom + vshift, top - vshiftif origin == 'upper':bottom, top = top, bottomreturn {"[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 = extentx, 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, haligndef 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 = Falseelse:port_string, starboard_string = 'right', 'left'inverted_xindex = Truebbox_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 labelsfor 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 + yfor 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 = extenttext = ('left: {left:0.1f}\nright: {right:0.1f}\n' +'bottom: {bottom:0.1f}\ntop: {top:0.1f}\n').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));
matplotlib 进阶之origin and extent in imshow相关推荐
- origin画图_把heatmap翻一转:imshow的origin和extent
背景故事: 之前画热度图用的seaborn,里面好像有一个inversey的选项,可以把热度图直接做垂直翻转.结合numpy的histogram2d来看,这是十分实用的,因为histogram2d得到 ...
- Matplotlib进阶教程:布局讲解
在后台回复[阅读书籍] 即可获取python相关电子书~ Hi,我是山月. 今天来给大家介绍下Matplotlib的布局部分~ 01 自定义图形布局 可以创建axes的网格状组合的方法: 1)subp ...
- Matplotlib进阶教程:颜色讲解
在后台回复[阅读书籍] 即可获取python相关电子书~ Hi,我是山月. 今天来给大家介绍下Matplotlib里的颜色部分. 01 指定颜色 Matplotlib可以通过以下格式来指定颜色: 一个 ...
- Matplotlib进阶:利用rcParams控制图形属性
目录 概要 What is rc setting? What is rcParams? matplotlibrc文件在哪儿 缺省设置的绘图例 利用rcParams修改设置属性 小结 概要 本文简单介绍 ...
- matplotlib 进阶之Tight Layout guide
文章目录 简单的例子 Use with GridSpec Legend and Annotations Use with AxesGrid1 Colorbar 函数链接 matplotlib教程学习笔 ...
- 大数据分析——Matplotlib进阶教程
文章目录 问题区设置坐标轴 1.matplotlib.pyplot总览 (1)总函数 (2)常用函数 常用函数解析 对于figure和axes的理解 2.实战 (1)三维图 3D画图常用函数 np.m ...
- matplotlib 进阶之Artist tutorial(如何操作Atrist和定制)
目录 基本 plt.figure() fig.add_axes() ax.lines set_xlabel 一个完整的例子 定制你的对象 obj.set(alpha=0.5, zorder=2), o ...
- Python的数据科学函数包(三)——matplotlib(plt)
Matplotlib是Python最著名的2D绘图库 c opencv要比PIL, plt的速度更快一些 matplotlib中一张图的具体构造 如果将Matplotlib绘图和我们平常画画相类比,可 ...
- matplotlib 笔记 imshow
1 基本介绍 将数据显示为图像,一般放到在 2D 常规栅格上. 输入可以是实际的 RGB(A) 数据,也可以是 2D 标量数据,它们将被渲染为伪彩色图像. 为了显示灰度图像,使用参数 cmap='gr ...
最新文章
- python写界面输入测试脚本_python+Selenium自动化测试——输入,点击操作
- scrapy详解及主要应用场景
- dev gridcontrol 根据数据获取索引_MySQL 索引分析除了 EXPLAIN 还有什么方法?
- Windows 文件含义大全
- 通信风口下,App 即将消亡?
- 为什么企业宁愿花 15K 重新招人,也不愿意花 10K 留住老测试员?
- svn 仓库 本地 连接_建立Subversion仓库在本地如何操作?
- Shell 脚本进程并发进程数控制
- 在CMD里进行复制粘贴的方法
- VirtualLab专题实验教程-4.基于超表面的闪耀光栅
- 淘宝信用等级|淘宝买家信用等级|淘宝卖家信用等级(图片介绍更清晰)
- 职业学校计算机和机电哪个好,职业学校都有什么专业10大热门专业
- 什么是溢出?补码加法运算如何判断是否溢出?
- P1002 过河卒(dp动态规划,洛谷,java)
- NOI 4.6 贪心 2407:书架
- matlab里面axis auto,Matlab中axis函数用法总结-Go语言中文社区
- MyBatis持久层框架
- Kotlin重载操作符和约定声明规则
- 纯原生JS用面向对象class方法实现简易扫雷小游戏
- token登录验证机制
热门文章
- 华为新款开机+音量上进不去Rec双清模式解决办法
- 吐血推荐那些提升开发人员工作效率的在线工具|文末抽书
- 怎样修复服务器电池,服务器电池故障
- 计算机的缓存目录在哪里,Cbox缓存文件在计算机哪个位置
- c语言培训后工作机会,娄底C语言培训,娄底C++找工作难吗,娄底C++培训完找什么工作...
- C++进阶——内存管理(二)
- DFS和BFS的区别
- 用计算机弹九张机,九张机(中华新韵)【原创】
- 玩转BI数据分析中的指标计算
- 计算机控制音响阵列,同济大学体育馆采用ATEIS的全数字DSP可指向阵列音箱