使用colorbar时,get_position()会做一些奇怪的事情

时间:2012-08-10 11:23:23

标签: python image matplotlib

我有一个数据矩阵,其中x和y轴是对数的。我正在尝试使用imshow来显示矩阵,但由于我想要日志轴,我将imshow轴中的刻度设置为[],然后覆盖另一组轴:

import matplotlib.pyplot as plt
import numpy as np

# the x,y max and min are the log values
array = np.zeros((2,2))
array[1,1] = -1
fig = plt.figure()
ax = plt.imshow(
    array, 
    extent = (0,1, 1, 0), 
    interpolation = 'nearest').get_axes()
ax.invert_yaxis()

# add a colorbar
# cb = plt.colorbar()      # <----- THIS CAUSES TROUBLE
# cb.set_label('zbar')

ax.set_aspect(1)
ax.xaxis.set_ticks([])
ax.yaxis.set_ticks([])
position = ax.get_position()
aspect = ax.get_aspect()

# overlay another set of axes 
ax_log = fig.add_subplot(111, frameon = False)
ax_log.set_xscale('log')
ax_log.set_yscale('log')
ax_log.axis((10**0, 10**1, 10**0, 10**1)) # old min and max but exponentiated  
ax_log.set_position(position)
ax_log.set_aspect(aspect)

plt.savefig('test.png', bbox_inches = 'tight')
plt.close()

没有颜色栏这可以正常工作:

with no colorbar

但是当我取消注释添加颜色条的线条时,我得到了一个奇怪的转变:

with colorbar

看起来颜色条以某种方式将图像略微向左移动,但鉴于我在创建颜色条后调用get_position()这看起来很奇怪。我是否忽视了制作这个情节的简单方法?有一些简单的解决方案吗?

1 个答案:

答案 0 :(得分:1)

搜索一下,我发现了一个解决方法,也许有一个更好的...

问题似乎是plt.colorbar()将从其绘制的情节“窃取”空间。它仍然有点奇怪,因为我仍然期望get_position()返回正确的坐标。但作为一种解决方法,我使用了GridSpec和原始Colorbar构造函数。

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.gridspec import GridSpec
from matplotlib.colorbar import Colorbar

# the x,y max and min are the log values
array = np.zeros((2,2))
array[1,1] = -1
fig = plt.figure()
gs = GridSpec(10,11)            # create a 10 x 11 grid
ax = plt.subplot(gs[:,0:-1])    # make subplot on 10 x 10 part 
im = plt.imshow(
    array, 
    extent = (0,1, 1, 0), 
    interpolation = 'nearest', 
    axes = ax)
ax.invert_yaxis()

# add a colorbar
cb_ax = plt.subplot(gs[:,-1])   # put the colorbar on the last column
cb = Colorbar(ax = cb_ax, mappable = im ) # use the raw colorbar constructor
cb.set_label('zbar')

ax.set_aspect(1)
ax.xaxis.set_ticks([])
ax.yaxis.set_ticks([])
position = ax.get_position()
aspect = ax.get_aspect()

# overlay another set of axes 
ax_log = fig.add_subplot(111, frameon = False) # can't use gridspec?
ax_log.set_xscale('log')
ax_log.set_yscale('log')
ax_log.axis((10**0, 10**1, 10**0, 10**1)) # old min and max but exponentiated  
ax_log.set_position(position)
ax_log.set_aspect(aspect)

plt.savefig('test.pdf', bbox_inches = 'tight')
plt.close()

我也不能使用GridSpec对象初始化第二组轴(这样做会使图像消失)也很奇怪。