不同绘图的相同色条范围-Matplotlib [英] Same color bar range for different plots - Matplotlib

查看:68
本文介绍了不同绘图的相同色条范围-Matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力通过不同的图来保持相同的色条范围.

I'm struggling to keep the same color bar range through different plots.

例如,我具有以下可视化效果:

For example, I have these visualizations:

使用以下代码制作的

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()

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

I want to be able to compare both fields, so, I would like to use the same color mapping for both of them.

我的第一种方法是使用参数 v_min v_max ,并使用数据的最小值/最大值.

My first approach was to use the parameters v_min and v_max, using the min/max values of the data.

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

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

Then I got the same color mapping:

但是我也想在绘图中显示相同的颜色条范围.我尝试使用

But I also would like to have the same color bar range displayed in the plot. I tried to use

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

没有成功.

此完整示例产生了相同的行为:

This complete example produces the same behavior:

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') 

我该怎么办?

谢谢.

推荐答案

如果要使颜色条中的颜色与两个轮廓图中的相同值相对应,则不仅需要控制颜色条,还需要控制颜色条.等高线图中的水平.也就是说,为了比较各图之间的相同级别,这些图应具有相同的轮廓级别.这很容易做到.这是该情节的示例:

If you want the colors in the colorbars to correspond to the same values within two contour plots, then you need to not only control the colorbar, but also control the levels in the contour plot. That is, to compare the same levels between the plots, the plots should have the same contour levels. This is easy to do. Here's an example of that plot:

有两种方法:1)提前计算水平;2)使用一个地块的水平来设置另一个地块的水平.我将进行第二个操作,因为从中应该清楚如何进行第一个操作(例如,使用 levels = numpy.linspace(v_min,vmax,10),我不在这里使用它,而是让mpl计算电平).

There are two ways: 1) calculate the levels ahead of time; 2) use the levels from one plot to set the levels in the other. I'll do the second, since from this it should be clear how to do the first (using, for example, levels = numpy.linspace(v_min, vmax, 10), though, to be clear, I'm not using this here, but am letting mpl calculate the levels).

首先,这里我也在使用:

First, here I'm also using:

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()

这篇关于不同绘图的相同色条范围-Matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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