如何从Matplotlib中的图检索颜色条实例 [英] How to retrieve colorbar instance from figure in matplotlib

查看:75
本文介绍了如何从Matplotlib中的图检索颜色条实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部.我想在更改图像数据时更新图形的颜色栏.像这样:

all. I want to update the colorbar of a figure when the imagedata is changed. So something like:

img = misc.lena()
fig = plt.figure()
ax = plt.imshow(im)
plt.colorbar(ax)
newimg = img+10*np.randn(512,512)

def update_colorbar(fig,ax,newimg):
    cbar = fig.axes[1]
    ax.set_data(newimg)
    cbar.update_normal(ax)
    plt.draw()

,但似乎fig.axes()返回的结果没有我期望的颜色条实例.我可能可以将colorbar实例作为参数传递给update函数,但是我认为仅传递一个fig参数就足够了.谁能解释一下如何从图中检索颜色条?还是为什么'fig.axes()'不返回AxesImage或Colobar实例,而仅返回Axes或AxesSubplot?我想我只需要对Axes/Figure的东西有更多的了解.谢谢!

but it seems that returned results from fig.axes() does not have the colorbar instance like I expected. I can probably just pass the colorbar instance as an argument to the update function, but I thought just passing one fig parameter may be good enough. Can anyone explain a little bit on how to retrieve the colorbar from the figure? Or why 'fig.axes()' doesn't return the AxesImage or Colobar instance but just the Axes or AxesSubplot? I think I just need more understanding of the Axes/Figure stuff.Thank you!

推荐答案

首先,我认为您在坐标轴(基本上是绘图),图形,标量可映射(图像,这种情况)以及colorbar实例.

First off, I think you're getting a bit confused between the axes (basically, the plot), the figure, the scalar mappable (the image, in this case), and the colorbar instance.

figure是绘图所在的窗口.它是顶层容器.

The figure is the window that the plot is in. It's the top-level container.

每个图形通常具有一个或多个axes.这些是地块/子图.

Each figure usually has one or more axes. These are the plots/subplots.

颜色栏也在图中.添加颜色条会为要显示的颜色条创建一个新轴(除非另行指定).(通常无法在与图像相同的轴上显示它,因为颜色条需要有自己的x和y限制,等)

Colorbars are also inside the figure. Adding a colorbar creates a new axes (unless you specify otherwise) for the colorbar to be displayed in. (It can't normally be displayed in the same axes as the image, because the colorbar needs to have its own x and y limits, etc.)

您之所以有些困惑,是因为您混用了状态机接口和OO接口.这样做很好,但是您需要了解OO界面.

Some of your confusion is due to the fact that you're mixing the state-machine interface and the OO interface. It's fine to do this, but you need to understand the OO interface.

fig.axes[1]不是颜色条实例.它是绘制颜色条的轴.(另外,fig.axes[1]只是图中的第二条轴.它恰好是具有一个子图和一个颜色条的图形中颜色条所在的轴,但不会通常是这样.)

fig.axes[1] isn't the colorbar instance. It's the axes that the colorbar is plotted in. (Also, fig.axes[1] is just the second axes in the figure. It happens to be the axes that the colorbar is in for a figure with one subplot and one colorbar, but that won't generally be the case.)

如果要更新颜色条,则需要保留colorbar返回的颜色条实例.

If you want to update the colorbar, you'll need to hold on to the colorbar instance that colorbar returns.

以下是您通常如何对待事物的示例:

Here's an example of how you'd normally approach things:

import matplotlib.pyplot as plt
import numpy as np

data = np.random.random((10,10)) # Generate some random data to plot

fig, ax = plt.subplots() # Create a figure with a single axes.
im = ax.imshow(data)     # Display the image data
cbar = fig.colorbar(im)  # Add a colorbar to the figure based on the image

如果您要使用update_normal来更新颜色条,它会期望ScalarMappable(例如,由imshow创建的图像,由scatter创建的集合,由ContourSet创建的ContourSet创建等)(也可以通过其他方法来实现.通常,您只想更新限制,而不是整个限制.)在上面的代码中,您可以调用cbar.update_normal(im).

If you're going to use update_normal to update the colorbar, it expects a ScalarMappable (e.g. an image created by imshow, the collection that scatter creates, the ContourSet that contour creates, etc) to be passed in. (There are other ways to do it, as well. Often you just want to update the limits, rather than the whole thing.) In the case of the code above, you'd call cbar.update_normal(im).

但是,您尚未创建新的AxesImage,只是更改了它的数据.因此,您可能只想做:

However, you haven't created a new AxesImage, you've just changed it's data. Therefore, you probably just want to do:

cbar.set_clim(newimg.min(), newimg.max())

这篇关于如何从Matplotlib中的图检索颜色条实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆