2014-09-26 128 views
3

我努力通過不同的地塊保持相同的酒吧範圍。不同地塊的相同顏色的酒吧範圍 - Matplotlib

例如,我有這些可視化:

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

我怎麼能這樣做呢?

預先感謝您。

+0

放在兩個呼叫的'vmin'和'vmax'?你在做什麼應該工作。您能否以完整的可調用示例(包括合成數據)重現此問題? – tacaswell 2014-09-26 18:46:25

+0

是的,我把這個參數放在這兩個調用中,功能都是一樣的。我去做。 – pceccon 2014-09-26 18:49:11

+0

,我認爲'set_clim'應該放在'cs'對象上,而不是顏色條上。 – tacaswell 2014-09-26 18:50:10

回答

1

如果您想讓顏色條中的顏色與兩個輪廓圖內的相同值相對應,那麼您不僅需要控制顏色條,還需要控制等高線圖中的水平。也就是說,爲了比較圖之間的相同水平,圖應該具有相同的等值水平。這很容易做到。下面是情節的例子:

enter image description here

有兩種方式:1)計算領先水平的時間; 2)使用一個圖中的水平來設置另一個水平。我會做第二次,因爲從這個角度應該清楚如何做第一個(例如,使用,但是,清楚起見,我沒有在這裏使用這個,但是讓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() 
+0

所以在這個例子中它不可能是5?一個小區的最小,最大值應該理解另一個小區的極限?我對這些數據沒有這樣的控制,它們來自於預測,如果我提到的條件是必要的,這通過我的合奏是不會成立的。 :/ – pceccon 2014-10-03 14:44:12

+1

不,這只是爲兩者使用一組等級的示例。您可以根據需要指定級別,例如,您可以將'levels = np.arange(-5,6)'傳遞給兩者。主要的一點是,如果你想有兩個匹配關係的離散化顏色條(當然這些關卡也與輪廓匹配),那麼這些關卡本身必須匹配。 – tom10 2014-10-03 14:54:56

+0

好的。我會在這裏嘗試並接受你的回答。謝謝@ tom10。 – pceccon 2014-10-03 14:56:54

相關問題