Matplotlib:颤抖和imshow叠加,如何设置两个颜色条? [英] Matplotlib : quiver and imshow superimposed, how can I set two colorbars?

查看:164
本文介绍了Matplotlib:颤抖和imshow叠加,如何设置两个颜色条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图形,它由 imshow()显示的图像,轮廓和 quiver()设置的矢量场组成。我已经根据另一个标量对向量场进行了着色。在我图形的右边,我做了一个 colorbar()。此 colorbar()表示 imshow()显示的值(在我的情况下可以为正,也可以为负)。我想知道如何设置另一个颜色条,该颜色条将基于矢量颜色的标量值。有人知道怎么做吗?

I have a figure that consists of an image displayed by imshow(), a contour and a vector field set by quiver(). I have colored the vector field based on another scalar quantity. On the right of my figure, I have made a colorbar(). This colorbar() represents the values displayed by imshow() (which can be positive and negative in my case). I'd like to know how I could setup another colorbar which would be based on the values of the scalar quantity upon which the color of the vectors is based. Does anyone know how to do that?

这里是我能够制作图像的一个示例。请注意,向量的颜色从蓝色变为红色。根据当前的颜色条,蓝色表示负数。但是我知道用向量的颜色表示的数量始终为正。

Here is an example of the image I've been able to make. Notice that the colors of the vectors go from blue to red. According to the current colorbar, blue means negative. However I know that the quantity represented by the color of the vector is always positive.

推荐答案

只需调用 colorbar 两次,在每次绘图调用之后。 Pylab将创建一个与最新图匹配的新颜色条。请注意,如您的示例所示,颤振值的范围是0.1,而imshow取的是负值。为了清楚起见(在此示例中未显示),我将使用不同的颜色图来区分两种类型的图。

Simply call colorbar twice, right after each plotting call. Pylab will create a new colorbar matching to the latest plot. Note that, as in your example, the quiver values range from 0,1 while the imshow takes negative values. For clarity (not shown in this example), I would use different colormaps to distinguish the two types of plots.

import numpy as np
import pylab as plt

# Create some sample data
dx = np.linspace(0,1,20)
X,Y = np.meshgrid(dx,dx)
Z  = X**2 - Y
Z2 = X

plt.imshow(Z)
plt.colorbar()

plt.quiver(X,Y,Z2,width=.01,linewidth=1)
plt.colorbar() 

plt.show()

这篇关于Matplotlib:颤抖和imshow叠加,如何设置两个颜色条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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