简体   繁体   English

Matplotlib Slider Widget和更改色条阈值

[英]Matplotlib Slider Widget and changing colorbar threshold

I am currently trying to work on a program that will allow the user to display their dataset in the form of a colormap and through the use of sliders, it will also allow the user to adjust the threshold of the colormap and thus update the colormap accordingly. 我目前正在尝试一个程序,该程序将允许用户以色图的形式显示其数据集,并通过使用滑块来显示,它还允许用户调整色图的阈值,从而相应地更新色图。 。 The best to describe this would be through the use of a picture: 最好的描述方法是使用图片: 在此处输入图片说明

This image shows how the colorbar should look before (the image on the left) and after (the image on the right) the adjustment. 此图像显示了调整前(左侧的图像)和之后(右侧的图像)颜色条的外观。 As the threshold values of the colrobar are changed, the colormap would be updated accordingly. 随着corobar阈值的更改,颜色图将相应更新。

Now I am mainly using matplotlib and I found that matplotlib does support some widgets, such as a slider. 现在,我主要使用matplotlib,我发现matplotlib确实支持某些小部件,例如滑块。 However the area I need help in is devising a piece of code which will update the colorbar and colormap (like the way shown in the picture above) when the slider is adjusted. 但是,我需要帮助的地方是设计一段代码,该代码将在调整滑块时更新颜色条和颜色图(如上图所示)。 I was wondering if anyone has done this before and might have a piece of code they would be willing to share and might have pointers as to how this can be achieved. 我想知道是否有人以前做过此事,并且可能会愿意分享一些代码,并且可能有关于如何实现的指针。

This should get you 80% of the way there: 这应该可以让您80%地达到目标:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button

ax = plt.subplot(111)
plt.subplots_adjust(left=0.25, bottom=0.25)
img_data = np.random.rand(50,50)
c_min = 0
c_max = 1

img = ax.imshow(img_data, interpolation='nearest')
cb = plt.colorbar(img)
axcolor = 'lightgoldenrodyellow'


ax_cmin = plt.axes([0.25, 0.1, 0.65, 0.03])
ax_cmax  = plt.axes([0.25, 0.15, 0.65, 0.03])

s_cmin = Slider(ax_cmin, 'min', 0, 1, valinit=c_min)
s_cmax = Slider(ax_cmax, 'max', 0, 1, valinit=c_max)

def update(val, s=None):
    _cmin = s_cmin.val
    _cmax = s_cmax.val
    img.set_clim([_cmin, _cmax])
    plt.draw()

s_cmin.on_changed(update)
s_cmax.on_changed(update)

resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
def reset(event):
    s_cmin.reset()
    s_cmax.reset()
button.on_clicked(reset)

plt.show()

This is a minimally edited version of the official demo . 这是官方演示的最低编辑版本。

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

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