我正在搜索從具有不同軸 X、Y 和資料 Z 的顏色圖或輪廓函式在同一圖中繪制多個圖形(這里是 2 個)。但是,我只想使用所有圖形的單個顏色條顯示每個資料的最大值。
在此示例中,我創建了一個圖形,其中添加了每個圖形,但第二個圖形覆寫了第一個圖形,無論其資料是低還是高。
import matplotlib.pyplot as plt
import numpy as np
a = [1,0.25]
fig = plt.figure(1)
ax = fig.gca()
for i in range(2):
x = np.linspace(-3, 3, 51)
y = np.linspace(-2*a[i], 2*a[i], 41)
X, Y = np.meshgrid(x, y)
if i == 0:
Z = (1 - X/2 X**5 Y**3) * np.exp(-X**2 - Y**2)
else:
Z = 0.5*np.ones((41,51))
graph = ax.contourf(X,Y,Z)
bar = fig.colorbar(graph)
plt.show()
圖1 代碼顯示
這是我想要顯示的內容:
圖 2 所需
非常感謝,特里斯坦
uj5u.com熱心網友回復:
根據我們在您帖子的評論中進行的討論,我認為您可以編輯代碼以實作您想要的功能,如下所示。
首先,作為一般性評論,我建議您將變數移動到腳本的頂部。
其次,這是主要部分,如果您使用比較來測驗要填充 Z 陣列的值,則可以只繪制一張圖。您可以鏈接多個比較np.logical_and,然后使用np.where函式值或常量值填充 Z 陣列,具體取決于您是否位于所需的 x 和 y 值框內以??及函式值還是所需的常量值最大。
fig = plt.figure()
ax = fig.gca()
xmin, xmax, nx = -3, 3, 51
ymin, ymax, ny = -2, 2, 41
# box values
xbmin, xbmax = -3, 3
ybmin, ybmax = -0.5, 0.5
zlevel = 0.5
x = np.linspace(xmin, xmax, nx)
y = np.linspace(ymin, ymax, ny)
X, Y = np.meshgrid(x,y)
Z = (1 - X/2 X**5 Y**3) * np.exp(-X**2 - Y**2)
test1 = Z<zlevel
test2 = np.logical_and(X>=xbmin, X<=xbmax)
test3 = np.logical_and(Y>=ybmin, Y<=ybmax)
mask = np.logical_and(np.logical_and(test1, test2), test3)
Z = np.where(mask, zlevel*np.ones(Z.shape), Z)
graph = ax.contourf(X,Y,Z)
bar = fig.colorbar(graph)
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/376208.html
標籤:Python matplotlib 颜色图 轮廓
