Python 在单个图形上叠加绘图

Python 在单个图形上叠加绘图,python,matplotlib,histogram,heatmap,imshow,Python,Matplotlib,Histogram,Heatmap,Imshow,我有两个基于二维直方图的热图,我正试图覆盖在一个图表上。它们轴的极限(范围L和范围H)不一定完全一致。如果需要的话,我可以制作出令人满意的各个图,但是当我试图在一张图上很好地显示两个热图时,只会显示最近的一个热图 import numpy as np import numpy.random import matplotlib.pyplot as plt # Generate some test data x_L = np.random.randn(8873) y_L = np.random.r

我有两个基于二维直方图的热图,我正试图覆盖在一个图表上。它们轴的极限(范围L和范围H)不一定完全一致。如果需要的话,我可以制作出令人满意的各个图,但是当我试图在一张图上很好地显示两个热图时,只会显示最近的一个热图

import numpy as np
import numpy.random
import matplotlib.pyplot as plt

# Generate some test data
x_L = np.random.randn(8873)
y_L = np.random.randn(8873)

x_H = np.random.randn(1000)
y_H = np.random.randn(1000)

heatmap_L, xedges_L, yedges_L = np.histogram2d(x_L, y_L, bins=50)
extent_L = [xedges_L[0], xedges_L[-1], yedges_L[0], yedges_L[-1]]

heatmap_H, xedges_H, yedges_H = np.histogram2d(x_H, y_H, bins=50)
extent_H = [xedges_H[0], xedges_H[-1], yedges_H[0], yedges_H[-1]]

plt.clf()
im1 = plt.imshow(heatmap_L.T, extent=extent_L, origin='lower', cmap='Blues')
im2 = plt.imshow(heatmap_H.T, extent=extent_H, origin='lower', cmap='Greens')
plt.show() 

编辑:如果我没弄错的话,所有的点都不在正确的位置

import numpy as np
import numpy.random
import matplotlib.pyplot as plt

# Generate some test data
x_L = np.random.randn(8873)
y_L = np.random.randn(8873)

x_H = np.random.randn(1000)
y_H = np.random.randn(1000)

heatmap_L, xedges_L, yedges_L = np.histogram2d(x_L, y_L, bins=50)
extent_L = np.array([xedges_L[0], xedges_L[-1], yedges_L[0], yedges_L[-1]])

heatmap_H, xedges_H, yedges_H = np.histogram2d(x_H, y_H, bins=50)
extent_H = np.array([xedges_H[0], xedges_H[-1], yedges_H[0], yedges_H[-1]])

plt.clf()
im1 = plt.imshow(heatmap_L.T, extent=extent_L, origin='lower', cmap='Blues')
im2 = plt.imshow(heatmap_H.T, extent=extent_H, origin='lower', cmap='Greens')
plt.autoscale()
plt.show()

如果仔细观察上一个图中的点,例如边缘点的聚集,您会注意到它们与上面的图中的不同

你可以打电话

plt.autoscale()
这样,限制将根据轴的内容进行调整

例如:

import numpy as np
import matplotlib.pyplot as plt

def get(offs=0):
    # Generate some test data
    x = np.random.randn(8873)+offs
    y = np.random.randn(8873)+offs

    heatmap, xedges, yedges = np.histogram2d(x, y, bins=50)
    extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]

    heatmap, xedges, yedges = np.histogram2d(x, y, bins=50)
    extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
    return heatmap, extent

h1,e1  = get(-3)
h2,e2  = get(+3)
plt.imshow(h1, extent=e1, origin='lower', cmap="RdBu")
plt.imshow(h2, extent=e2, origin='lower', cmap="YlGnBu")
plt.autoscale()
plt.show()

如果同时显示两个绘图,问题是一个绘图在另一个绘图之上。要查看此操作,可以移动其中一个图,如中所示:

import numpy as np
import numpy.random
import matplotlib.pyplot as plt

# Generate some test data
x_L = np.random.randn(8873)
y_L = np.random.randn(8873)

x_H = np.random.randn(1000)
y_H = np.random.randn(1000)

heatmap_L, xedges_L, yedges_L = np.histogram2d(x_L, y_L, bins=50)
extent_L = np.array([xedges_L[0], xedges_L[-1], yedges_L[0], yedges_L[-1]])

heatmap_H, xedges_H, yedges_H = np.histogram2d(x_H, y_H, bins=50)
extent_H = np.array([xedges_H[0], xedges_H[-1], yedges_H[0], yedges_H[-1]])

plt.clf()
im1 = plt.imshow(heatmap_L.T, extent=extent_L, origin='lower', cmap='Blues')
im2 = plt.imshow(heatmap_H.T+2, extent=extent_H+2, origin='lower', cmap='Greens')
plt.autoscale()
plt.show() 
您还需要在其中调用
plt.autoscale()
,否则限制无法正确调整

一种将两个绘图相互重叠显示的方法是使用参数
alpha=X
imshow
调用(其中0 转换值的一种方法是将数据展平,并使用所需的颜色对其进行增强

# imports and test data generation as before, removed for clarity...

flatHMH = np.reshape(heatmap_H, 2500)  # flatten the 2D arrays
flatHML = np.reshape(heatmap_L, 2500)
maxHMH = flatHMH.max()  # Find the maximum in each
maxHML = flatHML.max()
# Now for each value in the flat array build an RGBA tuple using 
# 1 for the colour we want - either green or blue, and then scaling
# the value by the maximum, finally reshaping back to a 50x50 array
augHMH = np.array([(0, 1, 0, x/maxHMH) for x in flatHMH]).reshape((50, 50, 4))
augHML = np.array([(0, 0, 1, x/maxHML) for x in flatHML]).reshape((50, 50, 4))

plt.clf()
# Plot without cmap as colours are now part of the data array passed.
im1 = plt.imshow(augHML, extent=extent_L, origin='lower')
im2 = plt.imshow(augHMH, extent=extent_H, origin='lower')
plt.autoscale()
plt.show() 

@重要的是,它是字面上如下,这为一个情节的情况提供了一个工作的例子。我有兴趣把两幅这样的图叠加起来。谢谢。我已经测试过了,但无法覆盖图以确认是否需要按照预期进行调整。是的,alpha有助于混合,但如果它太低,则它看起来完全被涂抹,没有明显的集中点区分。但是如果alpha太高,那么每个叠加图的矩形网格就会变得非常明显。有没有一种最佳方法可以很好地混合重叠图的背景,同时保持集中点的显著性?@Mathews24,请参阅imshow文档中先前暗示的替代解决方案的更新答案。根据我的想象,这些点可能与原始情况不完全一致。当我用RGB绘制上述(第一个示例)和后一个示例时,这些点不一定位于网格上完全相同的位置。我将在编辑原始问题时提供一个示例。
# imports and test data generation as before, removed for clarity...

flatHMH = np.reshape(heatmap_H, 2500)  # flatten the 2D arrays
flatHML = np.reshape(heatmap_L, 2500)
maxHMH = flatHMH.max()  # Find the maximum in each
maxHML = flatHML.max()
# Now for each value in the flat array build an RGBA tuple using 
# 1 for the colour we want - either green or blue, and then scaling
# the value by the maximum, finally reshaping back to a 50x50 array
augHMH = np.array([(0, 1, 0, x/maxHMH) for x in flatHMH]).reshape((50, 50, 4))
augHML = np.array([(0, 0, 1, x/maxHML) for x in flatHML]).reshape((50, 50, 4))

plt.clf()
# Plot without cmap as colours are now part of the data array passed.
im1 = plt.imshow(augHML, extent=extent_L, origin='lower')
im2 = plt.imshow(augHMH, extent=extent_H, origin='lower')
plt.autoscale()
plt.show()