matplotlib 的 imshow 中的颜色值?

发布于 2024-11-04 04:26:21 字数 242 浏览 2 评论 0原文

我想知道在 matplotlib 中使用 imshow() 时单击的点的颜色值。有没有办法通过 matplotlib 中的事件处理程序找到此信息(与单击的 x,y 坐标相同的方式可用)?如果没有,我如何找到这些信息?

具体来说,我正在考虑这样的案例:

imshow(np.random.rand(10,10)*255, interpolation='nearest')

谢谢! ——艾琳

I'd like to know the color value of a point I click on when I use imshow() in matplotlib. Is there a way to find this information through the event handler in matplotlib (the same way as the x,y coordinates of your click are available)? If not, how would I find this information?

Specifically I'm thinking about a case like this:

imshow(np.random.rand(10,10)*255, interpolation='nearest')

Thanks!
--Erin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

默嘫て 2024-11-11 04:26:22

如果“颜色值”指的是图表上单击点处的数组值,那么这很有用。

from matplotlib import pyplot as plt
import numpy as np


class collect_points():
   omega = []
   def __init__(self,array):
       self.array = array
   def onclick(self,event):
       self.omega.append((int(round(event.ydata)),   int(round(event.xdata))))

   def indices(self):
       plot = plt.imshow(self.array, cmap = plt.cm.hot, interpolation =  'nearest', origin= 'upper')
       fig = plt.gcf()
       ax = plt.gca()
       zeta = fig.canvas.mpl_connect('button_press_event', self.onclick)
       plt.colorbar()
       plt.show()
       return self.omega

用法如下:

from collect_points import collect_points
import numpy as np

array = np.random.rand(10,10)*255   
indices = collect_points(array).indices()

应该出现一个绘图窗口,单击点,然后返回 numpy 数组的索引。

If by 'color value' you mean the value of the array at a clicked point on a graph, then this is useful.

from matplotlib import pyplot as plt
import numpy as np


class collect_points():
   omega = []
   def __init__(self,array):
       self.array = array
   def onclick(self,event):
       self.omega.append((int(round(event.ydata)),   int(round(event.xdata))))

   def indices(self):
       plot = plt.imshow(self.array, cmap = plt.cm.hot, interpolation =  'nearest', origin= 'upper')
       fig = plt.gcf()
       ax = plt.gca()
       zeta = fig.canvas.mpl_connect('button_press_event', self.onclick)
       plt.colorbar()
       plt.show()
       return self.omega

Usage would be something like:

from collect_points import collect_points
import numpy as np

array = np.random.rand(10,10)*255   
indices = collect_points(array).indices()

A plotting window should appear, you click on points, and returned are the indices of the numpy array.

紫竹語嫣☆ 2024-11-11 04:26:22

您可以尝试如下操作:

x, y = len(df.columns.values), len(df.index.values)

# subplot etc 

# Set values for x/y ticks/labels
ax.set_xticks(np.linspace(0, x-1, x))
ax.set_xticklabels(ranges_df.columns)
ax.set_yticks(np.linspace(0, y-1, y))
ax.set_yticklabels(ranges_df.index)

 for i, j in product(range(y), range(x)):
    ax.text(j, i, '{0:.0f}'.format(ranges_df.iloc[i, j]),
    size='small', ha='center', va='center')

You could try something like the below:

x, y = len(df.columns.values), len(df.index.values)

# subplot etc 

# Set values for x/y ticks/labels
ax.set_xticks(np.linspace(0, x-1, x))
ax.set_xticklabels(ranges_df.columns)
ax.set_yticks(np.linspace(0, y-1, y))
ax.set_yticklabels(ranges_df.index)

 for i, j in product(range(y), range(x)):
    ax.text(j, i, '{0:.0f}'.format(ranges_df.iloc[i, j]),
    size='small', ha='center', va='center')
梦回旧景 2024-11-11 04:26:21

这是一个可行的解决方案。它仅适用于interpolation = 'nearest'。我仍在寻找一种更清晰的方法来从图像中检索插值(而不是对选取的 x,y 进行四舍五入并从原始数组中进行选择。)无论如何:

from matplotlib import pyplot as plt
import numpy as np

im = plt.imshow(np.random.rand(10,10)*255, interpolation='nearest')
fig = plt.gcf()
ax = plt.gca()

class EventHandler:
    def __init__(self):
        fig.canvas.mpl_connect('button_press_event', self.onpress)

    def onpress(self, event):
        if event.inaxes!=ax:
            return
        xi, yi = (int(round(n)) for n in (event.xdata, event.ydata))
        value = im.get_array()[xi,yi]
        color = im.cmap(im.norm(value))
        print xi,yi,value,color

handler = EventHandler()

plt.show()

Here's a passable solution. It only works for interpolation = 'nearest'. I'm still looking for a cleaner way to retrieve the interpolated value from the image (rather than rounding the picked x,y and selecting from the original array.) Anyway:

from matplotlib import pyplot as plt
import numpy as np

im = plt.imshow(np.random.rand(10,10)*255, interpolation='nearest')
fig = plt.gcf()
ax = plt.gca()

class EventHandler:
    def __init__(self):
        fig.canvas.mpl_connect('button_press_event', self.onpress)

    def onpress(self, event):
        if event.inaxes!=ax:
            return
        xi, yi = (int(round(n)) for n in (event.xdata, event.ydata))
        value = im.get_array()[xi,yi]
        color = im.cmap(im.norm(value))
        print xi,yi,value,color

handler = EventHandler()

plt.show()
北城半夏 2024-11-11 04:26:21

上述解决方案仅适用于单个图像。如果您在同一脚本中绘制两个或多个图像,“inaxes”事件不会在两个轴之间产生差异。您永远不会知道您单击的是哪个轴,因此您不会知道应该显示哪个图像值。

The above solution only works for a single image. If you plot two or more images in the same script, the "inaxes" event will not make a difference between both axis. You will never know in which axis are you clicking, so you won't know which image value should be displayed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文