使用Matplotlib将最大和最小滑块添加到按钮

时间:2018-11-01 12:32:31

标签: python python-2.7 matplotlib

我尝试添加向上和向下箭头按钮,而不是颜色栏的max和min滑块。单击向上箭头按钮时,颜色条范围值应增加一级;单击单击向下箭头时,颜色条范围值应减小一级。谁能帮我如何向滑块添加按钮以实现以下目的?

当前输出:

enter image description here

预期输出:

enter image description here

以下是我的代码:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button
import matplotlib.colors
ax = plt.subplot(111)
plt.subplots_adjust(left=0.25, bottom=0.25)
img_data = np.random.rand(50,50)

c_max = "%1.2f"
c_min = "%0.2f"
img = ax.imshow(img_data, interpolation='nearest')
cb = plt.colorbar(img)
axcolor = 'lightgoldenrodyellow'
ax_cmax  = plt.axes([0.25, 0.15, 0.65, 0.03])
ax_cmin = plt.axes([0.25, 0.1, 0.65, 0.03])
s_cmax = Slider(ax_cmax, 'max', 0, 50, valfmt=c_max)
s_cmin = Slider(ax_cmin, 'min', 0, 50, valinit=c_min)
def update(val, s=None):
    _cmin = s_cmin.val
    _cmax = s_cmax.val
    img.set_clim(0, _cmax)
    plt.draw()
s_cmax.on_changed(update)
s_cmin.on_changed(update)

plt.show()

0 个答案:

没有答案