2

我有一个数据矩阵,其中 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()

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

没有彩条

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

带彩条

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

4

1 回答 1

2

Searching around a bit, I found a workaround, maybe there's a better one...

The issue seems to be that plt.colorbar() will 'steal' space from the plot it's drawn on. It's still a bit strange, because I'd still expect get_position() to return the proper coordinates. But as a workaround I used GridSpec and the raw Colorbar constructor.

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()

It's also quite strange that I can't use the GridSpec object to initialize the second set of axes (doing so makes the image disappear).

于 2012-08-10T13:16:34.207 回答