在单个图形上叠加图形

时间:2018-07-17 23:26:04

标签: python matplotlib histogram heatmap imshow

我有两个基于2d直方图的热图,我试图将它们叠加在一张图上。它们的轴的界限(extent_L和extent_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() 

Only most recent heatmap displayed

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

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

enter image description here

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

enter image description here

如果仔细观察最后一个图中的点,例如边缘上的点的聚类,您会发现它们与上图中的不同。

2 个答案:

答案 0 :(得分:1)

您要同时显示两个图,问题是要在另一个图上绘制一个图。要查看实际效果,您可以按照以下方式移动其中一个图:

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 the imshow docs,以获取将图显示在彼此之上的两种选择。

一种转换值的方法是将数据展平,并用所需的颜色对其进行扩充。

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

答案 1 :(得分:0)

您可以致电

newLevel = Level.Fatal;

以使限制根据轴的内容进行调整。

示例:

string currentLevel = ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root.Level.ToString();

enter image description here