onclick method on a colorbar matplotlib python

穿精又带淫゛_ 提交于 2019-12-21 21:58:53

问题


I have a colorbar and a graph. I was wondering if you can use the onclick method on a colorbar that will then do something on the graph. So click on a particular color portion of the colorer then something happens I know how to do the bit that i want to happen. I just want to know how to set the colorbar to allow on click


回答1:


Sure! Just do something like cbar.ax.set_picker(tolerance).

As a quick example, this will highlight values in the image near the value you click on the colorbar:

import numpy as np
import matplotlib.pyplot as plt

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

fig, ax = plt.subplots()
im = ax.imshow(data)
cbar = fig.colorbar(im)
ax.set_title('Click on the colorbar')

highlight = ax.imshow(np.ma.masked_all_like(data), interpolation='nearest', 
                      vmin=data.min(), vmax=data.max())

def on_pick(event):
    val = event.mouseevent.ydata
    selection = np.ma.masked_outside(data, val - 0.05, val + 0.05)
    highlight.set_data(selection)
    fig.canvas.draw()

cbar.ax.set_picker(5)
fig.canvas.mpl_connect('pick_event', on_pick)

plt.show()



来源:https://stackoverflow.com/questions/14910963/onclick-method-on-a-colorbar-matplotlib-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!