gpt4 book ai didi

python - 不同图的相同颜色条范围 - Matplotlib

转载 作者:行者123 更新时间:2023-11-28 18:40:58 24 4
gpt4 key购买 nike

我正在努力通过不同的地 block 保持相同的颜色条范围。

例如,我有这些可视化效果:

enter image description here

enter image description here

哪些是用这段代码产生的:

def plot_contour(x_dim, y_dim, x_steps, y_steps, scalar_field, file_path):
plt.figure()

x, y = numpy.mgrid[-x_dim:x_dim/:x_steps*1j, -y_dim:y_dim:y_steps*1j]
cs = plt.contourf(x, y, scalar_field, zorder=1, extent=[-x_dim, x_dim, -y_dim, y_dim])
plt.colorbar(cs)

plt.savefig(file_path + '.png', dpi=Vc.dpi)
plt.close()

我希望能够比较这两个字段,因此,我想对它们使用相同的颜色映射。

我的第一种方法是使用参数 v_minv_max,使用数据的最小/最大值。

cs = plt.contourf(x, y, scalar_field, zorder=1, extent=[-x_dim, x_dim, -y_dim, y_dim], vmin=-1.00, vmax=1.05) # Manual setting to test

然后我得到了相同的颜色映射:

enter image description here enter image description here

但我也希望在绘图中显示相同的颜色条范围。我试着用

cb = plt.colorbar(cs)
cb.set_clim(vmin=-1.00, vmax=1.05)

没有成功。

这个完整的例子产生相同的行为:

import matplotlib
import numpy as numpy
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

matplotlib.rcParams['xtick.direction'] = 'out'
matplotlib.rcParams['ytick.direction'] = 'out'

delta = 0.025
x = numpy.arange(-3.0, 3.0, delta)
y = numpy.arange(-2.0, 2.0, delta)
X, Y = numpy.meshgrid(x, y)

Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians

Za = 10.0 * (Z2 - Z1)
Zb = 5.0 * (Z2 - Z1)

def bounds(scalar_fields):
"""
Get the bounds of a set of scalar_fields
:param scalar_fields : the scalar field set
:return: a set of normalized vector field components
"""
max_bound = -numpy.inf
min_bound = numpy.inf

for scalar_field in scalar_fields:
max_lim = numpy.max(scalar_field)
min_lim = numpy.min(scalar_field)
if max_lim > max_bound:
max_bound = max_lim
if min_lim < min_bound:
min_bound = min_lim

return min_bound, max_bound

def plot_contour(x_dim, y_dim, x_steps, y_steps, scalar_field, v_min, v_max, file_path):
plt.figure()

x, y = numpy.mgrid[-x_dim/2:x_dim/2:x_steps*1j, -y_dim/2:y_dim/2:y_steps*1j]

cs = plt.contourf(x, y, scalar_field, zorder=1, extent=[-x_dim/2.0, x_dim/2.0, -y_dim/2.0, y_dim/2.0],
vmin=v_min, vmax=v_max)
cb = plt.colorbar(cs)

plt.savefig(file_path + '.png')
plt.close()

v_min, v_max = bounds([Za, Zb])
x_dim = y_dim = 6

y_steps = x.shape[0]
x_steps = y.shape[0]

plot_contour(x_dim, y_dim, x_steps, y_steps, Za, v_min, v_max, 'Za')
plot_contour(x_dim, y_dim, x_steps, y_steps, Zb, v_min, v_max, 'Zb')

我该怎么做?

提前谢谢你。

最佳答案

如果您希望颜色条中的颜色对应两个等高线图中的相同值,那么您不仅需要控制颜色条,还需要控制等高线图中的水平。也就是说,要比较地 block 之间的相同水平,地 block 应具有相同的等高线水平。这很容易做到。这是该图的示例:

enter image description here

有两种方法:1)提前计算等级; 2)使用一个图中的水平来设置另一个图中的水平。我会做第二个,因为从这里应该清楚如何做第一个(使用,例如,levels = numpy.linspace(v_min, vmax, 10),虽然,要清楚,我在这里没有使用它,而是让 mpl 计算级别)。

首先,这里我还使用了:

Za = 10.0 * (Z2 - Z1)
Zb = 6.0 * (Z2 - Z1) # 6, rather than 5

然后,绘制:

def plot_contour(x_dim, y_dim, x_steps, y_steps, scalar_field, file_path, v_min, v_max, levels=None):
x, y = numpy.mgrid[-x_dim/2:x_dim/2:x_steps*1j, -y_dim/2:y_dim/2:y_steps*1j]
cs = plt.contourf(x, y, scalar_field, zorder=1, cmap=cm.jet, extent=[-x_dim/2.0, x_dim/2.0, -y_dim/2.0, y_dim/2.0], vmin=v_min, vmax=v_max, levels=levels)
plt.colorbar(cs)
return cs.levels

v_min, v_max = bounds([Za, Zb])

plt.figure()
plt.subplot(121)
levels = plot_contour(x_dim, y_dim, x_steps, y_steps, Za, 'Za', v_min, v_max)
plt.subplot(122)
plot_contour(x_dim, y_dim, x_steps, y_steps, Zb, 'Zb', v_min, v_max, levels=levels)
plt.show()

关于python - 不同图的相同颜色条范围 - Matplotlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26065811/

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