I want to create a figure consisting of nine subplots. I really hated the fact that I needed to create ax1 to ax9 separately so I created a for loop to do so. However, when I want to include a colorbar, the colorbar is positioned right of the last subplot. This is also illustrated in the following figure:

enter image description here

What is going wrong and how can I fix this?

The image has been generated with the following code:

import numpy
import layout
import matplotlib.pylab as plt

data = numpy.random.random((10, 10))

test = ["ax1", "ax2", "ax3", "ax4", "ax5", "ax6", "ax7", "ax8", "ax9"]

fig = plt.figure(1)

for idx in range(len(test)):
    vars()[test[idx]] = fig.add_subplot(3, 3, (idx + 1))

im = ax1.imshow(data)
plt.colorbar(im)

im2 = ax3.imshow(data)
plt.colorbar(im2)

plt.show()
有帮助吗?

解决方案 2

I found the answer to my question, resulting in the correct colorbar vs subplot spacing. Notice that if the spacing between subplot and colorbar does not matter, the answer of Molly is correct.

import numpy
import layout
import matplotlib.pylab as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable

data = numpy.random.random((10, 10))

test = ["ax1", "ax2", "ax3", "ax4", "ax5", "ax6", "ax7", "ax8", "ax9"]

fig = plt.figure(1)

for idx in range(len(test)):
  vars()[test[idx]] = fig.add_subplot(3, 3, (idx + 1))
  divider = make_axes_locatable(vars()[test[idx]])
  vars()["c" + test[idx]] = divider.append_axes("right", size = "5%", pad = 0.05)


im1 = ax1.imshow(data)
plt.colorbar(im1, cax = cax1)

im2 = ax2.imshow(data)
plt.colorbar(im2, cax = cax2)

im3 = ax3.imshow(data)
plt.colorbar(im3, cax = cax3)

im4 = ax4.imshow(data)
plt.colorbar(im4, cax = cax4)

im5 = ax5.imshow(data)
plt.colorbar(im5, cax = cax5)

im6 = ax6.imshow(data)
plt.colorbar(im6, cax = cax6)

im7 = ax7.imshow(data)
plt.colorbar(im7, cax = cax7)

im8 = ax8.imshow(data)
plt.colorbar(im8, cax = cax8)

im9 = ax9.imshow(data)
plt.colorbar(im9, cax = cax9)

plt.show()

This results in:

enter image description here

其他提示

colorbar takes an argument ax the "parent axes object(s) from which space for a new colorbar axes will be stolen." In your code you could do something like this to add a color bar next to an an axes:

im = ax1.imshow(data)
plt.colorbar(im, ax = ax1)

Dude's answer is great. However I would prefer avoid copy-paste by using this:

import numpy
import matplotlib.pylab as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable

data = numpy.random.random((10, 10))

test = ["ax1", "ax2", "ax3", "ax4", "ax5", "ax6", "ax7", "ax8", "ax9"]

fig = plt.figure(1)

for idx in range(len(test)):
  vars()[test[idx]] = fig.add_subplot(3, 3, (idx + 1))
  divider = make_axes_locatable(vars()[test[idx]])
  vars()["c" + test[idx]] = divider.append_axes("right", size = "5%", pad = 0.05)

  vars()["im" + str(idx)] = vars()[test[idx]].imshow(data)
  plt.colorbar(vars()["im" + str(idx)], cax = vars()["c" + test[idx]])

plt.show()

The result is the same.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top