如何在网格上进行细化绘制? [英] How to plot on grid with refinements?

查看:102
本文介绍了如何在网格上进行细化绘制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关问题

我的数据以(3×N)数组的形式出现

My data comes in the form of a (3 × N) array

[[x_0, ..., x_N-1],
 [y_0, ..., y_N-1],
 [z_0, ..., z_N-1]]

我要绘制它,以便前两行编码像素的X,Y位置,而第三行设置像素的颜色.

I want to plot it such that the first two lines code the X, Y position of a pixel and the third line sets the pixel's color.

但是,我不想进行任何插值.相反,所有点都位于网格上,从而平铺了空间,而较低的划分是对原始网格的细化.这是一些伪数据

However, I do not want any interpolation to take place. Rather, the space is tiled by the fact that all points lie on a grid, with lower divisions being refinements of the original grid. Here is some dummy data

[[4, 12, 24,  4, 12, 20, 28,  8, 18, 22, 28, 17, 19, 22, 17, 19],  # X
 [4,  4,  8, 12, 12, 20, 20, 24, 26, 26, 28, 29, 29, 30, 31, 31],  # Y
 [1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16]]  # Z (color)

这些像素的大小

D = [8,  8, 16,  8,  8,  8,  8, 16,  4,  4,  8,  2,  2,  4,  2,  2]

此处说明的是与上述虚拟数据相对应的像素所需的位置和空间范围.

Illustrated here is the desired position and spatial extent for the pixels corresponding to the dummy data above.

现在,我可以对数据进行插值以匹配最细的网格点,但这将效率低下,而且不够美观.我的网格中的某些区域可能比其他区域更为精细.

Now, I could interpolate my data to match the finest grid points, but that will be inefficient and not very elegant. Some areas of my grid may be much more refined than others.

有没有办法在matplotlib中进行这种绘图?

编辑 为了澄清,在大小(d×d)的位置(x,y)上细化一个像素会在位置(x-d/4,y-d/4),(x + d/4,y-d/4),(x-d/4,y + d/4),(x + d/4,y + d/4),每个大小为(d/2×d/2).位置始终以像素的中心为准.

EDIT To clarify, refining a pixel in position (x, y) of size (d×d) gives 4 pixels in positions (x - d/4, y - d/4), (x + d/4, y - d/4), (x - d/4, y + d/4),(x + d/4, y + d/4), each of size (d/2 × d/2). Positions always refer to the center of a pixel.

推荐答案

没有内置函数可以绘制问题中指定的不规则网格.解决方案将是定义具有各个边缘的像素"的集合.

There is no inbuilt function that would allow to plot an irregular grid like the one specified in the question. The solution would be to define a Collection of "pixels" with the respective edges.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection
from matplotlib.ticker import MultipleLocator

x =  np.array([4, 12, 24,  4, 12, 20, 28,  8, 18, 22, 28, 17, 19, 22, 17, 19])  # X
y =  np.array([4,  4,  8, 12, 12, 20, 20, 24, 26, 26, 28, 29, 29, 30, 31, 31])  # Y
z =  np.array([1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16])  # Z (color)
D =  np.array([8,  8, 16,  8,  8,  8,  8, 16,  4,  4,  8,  2,  2,  4,  2,  2])


def irregularmesh(x, y, s, c, ax=None, **kwargs):
    xedge = np.c_[-s, s, s, -s]/2. + np.atleast_2d(x).T
    yedge = np.c_[-s, -s, s, s]/2. + np.atleast_2d(y).T
    xy = np.stack((xedge,yedge), axis=2)

    # Create collection of rectangles.
    pc = PolyCollection(xy, closed=True, **kwargs)
    pc.set_array(c)
    ax = ax or plt.gca()
    ax.add_collection(pc)
    return pc

######## Plotting ################
fig, ax = plt.subplots()

pc = irregularmesh(x, y, D, z, ax=ax, linewidth=0, cmap="inferno")
fig.colorbar(pc, ax=ax)

ax.margins(0)
ax.autoscale()

for axis in [ax.xaxis, ax.yaxis]:
    axis.set_major_locator(MultipleLocator(4))
plt.show()

这篇关于如何在网格上进行细化绘制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆