gpt4 book ai didi

python - 对数刻度 Matplotlib PatchCollection 颜色

转载 作者:太空宇宙 更新时间:2023-11-04 03:26:08 26 4
gpt4 key购买 nike

我有一个生成异构网格然后绘制补丁的函数。它为每个 bin 指定上下 xy 边缘。例如,单个 bin 由向量 [x0, x1, y0, y1] 定义。这些坐标转换为 bin:

    y1|---------|   
| |
| bin |
| |
y0|---------|
x0 x1

我有一个 (Nx4) 网格,其中包含 N 个带有 [x0, x1, y0, y1] 列的容器。为了绘制数据,我执行以下操作:

z_plot  = z_stat / (dx * dy)     # ``z_stat`` is a calculated z-value 
z_plot = z_plot / z_plot.max() # for any given bin.

colors = mpl.cm.jet(z_plot) # Let fill data be white.
colors[z_stat == fill] = (1.0, 1.0, 1.0, 1.0) # fill=-9999.0, typically.

dx = mesh[:, 1] - mesh[:, 0] # x1-x0
dy = mesh[:, 3] - mesh[:, 2] # y1-y0.

xy = zip(mesh[:, 0], mesh[:, 2]) # (x,y) coordinates of each
# bin's lower left corner.

patches = [mpl.patches.Rectangle(xy[i], dx[i], dy[i], # I dont want
ec=None, lw=0, fc=colors[i]) # visible edges.
for i in range(mesh.shape[0])
]

patches = mpl.collections.PatchCollection(patches, match_original=True)
ax.add_collection(patches)

if z_stat is not None:

kwargs = {'orientation': 'vertical'}
cax, kw = _mpl.colorbar.make_axes_gridspec(plot_ax, **kwargs)

cbar = mpl.colorbar.ColorbarBase(cax, cmap=_mpl.cm.jet)

这是结果:

The <code>x</code> and <code>y</code> data is converted to a standard 2D histogram in <code>y vs. x</code>. The spiral mesh is generated. <code>Binned Data</code> is the number of data points in each bin. <code>Bin Mean</code> is the mean value in the bin. It would be very useful to log scale this.

This question does something similar, but without the logscale colors .我不知道如何让颜色符合记录比例。简单地将类似 mpl.colors.LogNorm() 的东西传递给 mpl.colorbar.ColorbarBase() 对我来说不起作用。

编辑 1:生成网格。

我有一个生成异构网格然后绘制补丁的函数。它以二维数组开头:

mesh = [[x00, x10, y00, y01], 
[x10, x11, y10, y11],
...,
[xN0, xN1, yN0, yN1]]

我通读网格并将每个箱子分成四个:

#    y1|----|----|          x0, x1, y0, y1 = mesh[i, :]
# | p4 | p3 | xh = [x0 + .5*(x1-x0)]
# |----|----| <- yh yh = [y0 + .5 *(y1-y0)]
# | p1 | p2 |
# y0|----|----|
# x0 ^-xh x1

如果每个 [p1, p2, p3, p4] 的数据点数超过最小数量(例如 50),我将替换行 [x0, x1, y0, y1 ] 这个数组:

        new_mesh = _np.array([[x0, xh, xh, x0],  # Define the 16 edges of  
[xh, x1, x1, xh], # the 4 new bins that are
[y0, y0, yh, yh], # going to replace the bin
[yh, yh, y1, y1]] # originally defined by
).T # [x0, x1, y0, y1].

if i == 0: # 0th edge is a special case for indexing.

mesh_h = _np.concatenate([new_mesh, mesh[1:]])

else:

mesh_h = _np.concatenate([mesh[:i], new_mesh, mesh[i+1:]])


mesh = mesh_h # Set the new edges.

最佳答案

虽然我无法测试您的确切案例,因为您没有提供一个独立可运行的示例,但您应该(如果我对您所需行为的理解是正确的)能够按如下方式完成您想要的。

首先编辑这一行去掉手动设置的颜色和边缘信息:

patches = [mpl.patches.Rectangle(xy[i], dx[i], dy[i],         # I dont want
ec=None, lw=0, fc=colors[i]) # visible edges.
for i in range(mesh.shape[0])
]

它应该看起来像这样:

patches = [mpl.patches.Rectangle(xy[i], dx[i], dy[i]) for i in range(mesh.shape[0])]

然后将 LogNormjet 和您的边缘参数传递给 PatchCollection。这是因为我们希望 matplotlib 自己处理尽可能多的事情,以便它可以为您整理颜色。

patch_collection = mpl.collections.PatchCollection(patches,cmap=matplotlib.cm.jet, norm=matplotlib.colors.LogNorm(), lw=0)

然后使用 set_array 为 PatchCollection 提供 z 信息:

patch_collection.set_array(z_plot)

最后将集合添加到图中,创建颜色条并显示图形:

ax.add_collection(patch_collection)
plt.colorbar(patch_collection)

plt.show()

这个答案主要基于给定的例子 here这可能会有用。

关于python - 对数刻度 Matplotlib PatchCollection 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32930872/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com