在 matplotlib 中分页/滚动一组 2D 热图 [英] Paging/scrolling through set of 2D heat maps in matplotlib
问题描述
我正在生成一组 3D 数据的 2D 热图.我希望能够有一种机制来交互地翻阅每个窗格.下面是一个简单的示例代码,我希望能够通过滑块(或其他方式)交互式地查看两个窗格(即 z = [0,1]).这是否可以通过 matplotlib 实现,还是我需要在生成图像文件后进行后期处理?
将 numpy 导入为 np从 matplotlib 导入 pyplot 作为 plt数据 = np.random.randint(10, size=(5, 5, 2))data_slice = np.zeros((5,5))对于范围内的 i (0, 5):对于范围内的 j (0, 5):数据切片[i][j] = 数据[i][j][0]plt.imshow(data_slice, cmap='hot', 插值='最近')plt.show()
我希望能够以交互方式执行此操作,并且
I am generating 2D heat map plots of a set of 3D data. I would like to be able to have a mechanism to interactively page through each pane. Below is a simple sample code, I would like to be able to interactively view both panes (ie, z = [0,1]) via a slider bar (or some other means). Is this possible with matplotlib or is this something I'll need to do post processing after generating the image files?
import numpy as np
from matplotlib import pyplot as plt
data = np.random.randint(10, size=(5, 5, 2))
data_slice = np.zeros((5,5))
for i in range(0, 5):
for j in range(0, 5):
data_slice[i][j] = data[i][j][0]
plt.imshow(data_slice, cmap='hot', interpolation='nearest')
plt.show()
Edit : I want to be able to do this interactively and it appears that the possible duplicate is trying to do this automatically.
You can either animate the layers as suggested by Andrew's comment or you can manually walk through the the layers using a slider as follow:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.widgets import Slider
# generate a five layer data
data = np.random.randint(10, size=(5, 5, 5))
# current layer index start with the first layer
idx = 0
# figure axis setup
fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.15)
# display initial image
im_h = ax.imshow(data[:, :, idx], cmap='hot', interpolation='nearest')
# setup a slider axis and the Slider
ax_depth = plt.axes([0.23, 0.02, 0.56, 0.04])
slider_depth = Slider(ax_depth, 'depth', 0, data.shape[2]-1, valinit=idx)
# update the figure with a change on the slider
def update_depth(val):
idx = int(round(slider_depth.val))
im_h.set_data(data[:, :, idx])
slider_depth.on_changed(update_depth)
plt.show()
The slider is continues while the layer index is discrete integer, I hope that is not a problem. Here is the resulting figure,
这篇关于在 matplotlib 中分页/滚动一组 2D 热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!