做电影小视频在线观看网站,seo实战培训费用,如何进入网站,近几天的新闻大事0 总述
imshow()允许你将图像#xff08;将进行颜色映射——基于norm和cmap——的2D数组或将按原样使用的3D RGB(A)的数组#xff09;渲染到数据空间中的矩形区域。最终渲染中图像的方向由原点和范围关键字参数#xff08;以及生成的AxesImage实例上的属性#xff09;和Ax…0 总述
imshow()允许你将图像将进行颜色映射——基于norm和cmap——的2D数组或将按原样使用的3D RGB(A)的数组渲染到数据空间中的矩形区域。最终渲染中图像的方向由原点和范围关键字参数以及生成的AxesImage实例上的属性和Axes的数据限制控制。
extent关键字参数控制数据坐标中的边界框图像将填充该边界框在数据坐标中指定为左、右、上、下origin关键字参数控制图像填充该边界框的方式最终渲染图像中的方向也受Axes限制的影响。 提示 下面的大部分代码用于向绘图添加标签和信息文本所描述的原点和范围的影响可以在图中看到而无需遵循所有的代码细节。 为了快速理解你可能希望跳过下面的代码详细信息直接继续讨论结果。 import matplotlib.pyplot as plt
import numpy as npfrom 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 righthshift 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\nextentNone, sizelarge,hacenter, vacenter, colorw)def plot_imshow_with_labels(ax, data, extent, origin, xlim, ylim):Actually run imshow() and add extent and index labels.im ax.imshow(data, originorigin, extentextent)# 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, bottomelse:upper_string, lower_string bottom, topif ylim is None or left right:port_string, starboard_string left, rightinverted_xindex Falseelse:port_string, starboard_string right, leftinverted_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),hacenter, vatop, **ann_kwargs)ax.annotate(lower_string, xy(.5, 0), xytext(0, 1),hacenter, vabottom, **ann_kwargs)ax.annotate(port_string, xy(0, .5), xytext(1, 0),haleft, vacenter, rotation90,**ann_kwargs)ax.annotate(starboard_string, xy(1, .5), xytext(-1, 0),haright, vacenter, rotation-90,**ann_kwargs)ax.set_title(forigin: {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, colorwhite, hahalign, vacenter,bbox{boxstyle: square, facecolor: facecolor})if xlim:ax.set_xlim(*xlim)if ylim:ax.set_ylim(*ylim)def generate_imshow_demo_grid(extents, xlimNone, ylimNone):N len(extents)fig plt.figure(tight_layoutTrue)fig.set_size_inches(6, N * (11.25) / 5)gs GridSpec(N, 5, figurefig)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)columns[label][0].set_title(extent)for ax, extent in zip(columns[label], extents):if extent is None:text Noneelse:left, right, bottom, top extenttext (fleft: {left:0.1f}\nright: {right:0.1f}\nfbottom: {bottom:0.1f}\ntop: {top:0.1f}\n)ax.text(1., .5, text, transformax.transAxes, haright, vacenter)ax.axis(off)return columns1 默认范围
首先让我们看一下默认范围 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内的白色区域。
虽然我们保留了前面示例中的范围但坐标00现在显式地放在左下角值向上和向右增加从查看者的角度来看。我们可以看到
坐标左下锚定图像然后填充朝向数据空间中右上点的框第一列始终最靠近“左”原点控制第一行是最接近“顶部”还是“底部”图像可以沿任一方向反转图像的“左-右”和“自上-下”感觉可能与屏幕上的方向无关。