简体   繁体   English

set_data之后在matplotlib中动态重新规范化

[英]Dynamic renormalize in matplotlib after set_data

I am making an interactive display of 3d data in 2d via .imshow() method. 我正在通过.imshow()方法以2d形式交互式显示3d数据。 I let the user to change the mode between viewing a single 2d layer and viewing the sum along all 2d layers. 我让用户在查看单个2d图层和查看所有2d图层的总和之间更改模式。 This results in large changes of range of the displayed values. 这会导致显示值范围的较大变化。 For this reason keeping the same color mapping all the time results in the image becoming oversaturated and unreadable. 因此,始终保持相同的颜色映射会导致图像变得过饱和并且无法读取。 I use .set_data() method of AxesImage class for changing the displayed data and I need a way of recalculating the color mapping at the same time. 我使用AxesImage类的.set_data()方法来更改显示的数据,并且需要一种同时重新计算颜色映射的方法。 The closest I got to this goal is this function: 我最接近此目标的是此函数:

def blit_data(self, data):
    c_norm  = cs.Normalize(vmin=np.nanmin(data), vmax=np.nanmax(data))
    cmap = plt.get_cmap('viridis')
    scalar_map = cmx.ScalarMappable(norm=c_norm, cmap=cmap)
    cmapped = scalar_map.to_rgba(data)
    self.display.set_data(cmapped)

(cmx = matplotlib.cm, cs = matplotlib.colors, plt = matplotlib.pyplot)

However this has an unwanted side effect: mousing over a pixel in the displayed image now displays [rgb] tuple as tooltip, instead of the original float64 value, which hinders exploration of this data. 但是,这有一个不良的副作用:将鼠标悬停在显示的图像中的像素上现在会显示[rgb]元组作为工具提示,而不是原始的float64值,这会妨碍对此数据的探究。 For this reason I am looking for another method to achieve the same effect. 因此,我正在寻找另一种方法来达到相同的效果。 A follow up question will be how to communicate this renormalization to a colorbar, so it stays relevant. 后续问题将是如何将此重归一化传达给颜色栏,使其保持相关性。

import matplotlib.pyplot as plt
import numpy as np

fig, (ax1, ax2) = plt.subplots(1, 2)

data = np.random.rand(10, 10)

im1 = ax1.imshow(data, interpolation='none', cmap='viridis')
im2 = ax2.imshow(data, interpolation='none', cmap='viridis')
im2.set_clim(0, .5)

示例输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM