颜色条 matplotlib python 上的 onclick 方法 [英] onclick method on a colorbar matplotlib python
问题描述
我有一个颜色条和一个图表.我想知道您是否可以在颜色条上使用 onclick 方法,然后在图表上执行某些操作.因此,单击着色器的特定颜色部分,然后会发生某些情况我知道如何做我想要发生的事情.我只想知道如何设置颜色条以允许点击
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
推荐答案
好的!只需执行类似 cbar.ax.set_picker(tolerance)
之类的操作.
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()
这篇关于颜色条 matplotlib python 上的 onclick 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!