简体   繁体   English

相同的颜色条范围适用于不同的图 - Matplotlib

[英]Same color bar range for different plots - Matplotlib

I'm struggling to keep the same color bar range through different plots. 我正在努力通过不同的情节保持相同的颜色条范围。

For example, I have these visualizations: 例如,我有这些可视化:

在此输入图像描述

在此输入图像描述

Which are produced with this code: 使用此代码生成的内容:

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. 我希望能够比较两个字段,所以,我想对它们使用相同的颜色映射。

My first approach was to use the parameters v_min and v_max , using the min/max values of the data. 我的第一种方法是使用参数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

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)

With no success. 没有成功。

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

How could I do that? 我怎么能这样做?

Thank you in advance. 先感谢您。

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: 这是该情节的一个例子:

在此输入图像描述

There are two ways: 1) calculate the levels ahead of time; 有两种方法:1)提前计算水平; 2) use the levels from one plot to set the levels in the other. 2)使用一个图中的水平来设置另一个图中的水平。 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). 我会做第二个,因为从这里应该清楚如何做第一个(例如,使用levels = numpy.linspace(v_min, vmax, 10) ,但是,要清楚,我没有使用这个在这里,但我要让mpl计算水平)。

First, here I'm also using: 首先,我在这里也使用:

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

Then, to plot: 然后,绘制:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM