使用相同的颜色条显示子图 [英] Imshow subplots with the same colorbar

查看:80
本文介绍了使用相同的颜色条显示子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作4个imshow子图,但是它们全部共享相同的色图. Matplotlib会根据矩阵的条目自动调整颜色图上的比例.例如,如果我的一个矩阵的所有整数均为10,而另一个矩阵的所有项等于5,并且我使用Greys色图,则我的一个子图应完全为黑色,而另一个子图应完全为灰色.但是他们两个最终都变成了全黑.如何使所有子图在颜色图上共享相同的比例尺?

I want to make 4 imshow subplots but all of them share the same colormap. Matplotlib automatically adjusts the scale on the colormap depending on the entries of the matrices. For example, if one of my matrices has all entires as 10 and the other one has all entries equal to 5 and I use the Greys colormap then one of my subplots should be completely black and the other one should be completely grey. But both of them end up becoming completely black. How to make all the subplots share the same scale on the colormap?

推荐答案

为实现此目的,您需要使所有图像具有相同的强度比例,否则colorbar()颜色是没有意义的.为此,请使用imshow()vminvmax参数,并确保所有图像的参数都相同.

To get this right you need to have all the images with the same intensity scale, otherwise the colorbar() colours are meaningless. To do that, use the vmin and vmax arguments of imshow(), and make sure they are the same for all your images.

例如,如果要显示的值范围从0到10,则可以使用以下内容:

E.g., if the range of values you want to show goes from 0 to 10, you can use the following:

import pylab as plt
import numpy as np
my_image1 = np.linspace(0, 10, 10000).reshape(100,100)
my_image2 = np.sqrt(my_image1.T) + 3
subplot(1, 2, 1)
plt.imshow(my_image1, vmin=0, vmax=10, cmap='jet', aspect='auto')
plt.subplot(1, 2, 2)
plt.imshow(my_image2, vmin=0, vmax=10, cmap='jet', aspect='auto')
plt.colorbar()

这篇关于使用相同的颜色条显示子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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