在matplotlib中通过2D热图集进行分页/滚动 [英] Paging/scrolling through set of 2D heat maps in matplotlib

查看:91
本文介绍了在matplotlib中通过2D热图集进行分页/滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在生成一组3D数据的2D热图图.我希望能够有一种机制来交互地浏览每个窗格.下面是一个简单的示例代码,我希望能够通过滑块(或其他某种方式)交互式地查看两个窗格(即z = [0,1]). matplotlib是否可以实现此功能,还是在生成图像文件后需要做后期处理?

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

我希望能够交互地进行 ,看来 解决方案

您可以按照安德鲁(Andrew)的注释建议为图层设置动画,也可以使用滑块手动遍历图层,如下所示:

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

当层索引是离散整数时,滑块仍在继续,我希望这不是问题.这是结果图,

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆