matplotlib:颜色栏调整图像大小? [英] matplotlib: colour bar resizing image?

查看:44
本文介绍了matplotlib:颜色栏调整图像大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我假设我有一个非常简单的问题,在过去的一个小时里,这一直使我发疯.因此,我试图生成一个具有以下轴长x = 37,y = 614的轮廓图.我可以生成一个等高线图没问题,但是当我添加一个颜色条时,图像的大小会调整为我假设的颜色条的大小.

I'm assuming I have a really simple question, which has been driving me insane for the past hour. So, I am trying to produce a contour plot with the following axis lengths x=37,y=614. I can produce a contour plot no problem, but when I add a colour bar the image becomes resized to what i'm assuming is the size of the colour bar.

不带颜色条的图像:

带有颜色条的图像:

该图已调整大小,我不知道为什么.如何绘制与第一个图形相似的图形,但要具有第二个图形的配色方案并带有颜色栏?

The figure becomes resized and I do not know why. How can I plot a figure like my first figure but with the colour scheme of the second figure and with a colour bar?

代码:

import matplotlib
import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
from matplotlib import pylab


y = np.arange(1, 615)
x = np.arange(1, 37)

z = np.loadtxt('145_contact_matrix_605.txt')


fig = plt.figure()
ax = plt.subplot(111)
CS = ax.contour(x, y, z)
plt.clabel(CS, inline=1, fontsize=10)


# COLOUR BAR CODE
im_out = ax.imshow(z, cmap=cm.jet)
ax.matshow(z,cmap=plt.cm.jet)
axcolor = fig.add_axes([0.9,0.1,0.02,0.8]) # adjust these vaules to position colour bar
pylab.colorbar(im_out, cax=axcolor)

plt.show()

推荐答案

改变坐标区纵横比的是 imshow 命令,而不是颜色条.

It's the imshow command that's changing the aspect ratio of the axes, not the colorbar.

imshow 假设您希望纵横比为 1.0,以便数据坐标中的正方形显示为正方形(即正方形像素).

imshow assumes you want an aspect ratio of 1.0 so that a square in data coordinates will appear square (i.e. square pixels).

如果您希望它的行为类似于 contour,只需指定 aspect='auto'.

If you want it to behave like contour, the just specify aspect='auto'.

ax.imshow(z, cmap=cm.jet)

您还应该删除 ax.matshow 行(或使用它代替 imshow ).照原样,您将拥有两个部分重叠且彼此隐藏的图像.

You should also remove the ax.matshow line (or use it instead of imshow). As it is, you'll have two images that partially overlap and hide each other.

如果您确实决定使用 matshow 而不是 imshow ,则需要为其指定 aspect ='auto',如下好吧.

If you do decide to use matshow instead of imshow, you'll need to specify aspect='auto' for it, as well.

这篇关于matplotlib:颜色栏调整图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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