1

I have a figure with three axes in a row. Each axes in this row shows some data whereby the x-and y-axis ranges are shared among the three subplots.

Colors indicate a certain label which should be displayed in a colorbar to the right of the last subplot.

A minimal example that almost creates what I want looks like this:

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.axes_grid1 import make_axes_locatable

fig = plt.figure(1)
ax1 = fig.add_subplot(131)
ax2 = fig.add_subplot(132, sharex=ax1, sharey=ax1)
ax3 = fig.add_subplot(133, sharex=ax1, sharey=ax1)

#set aspect ratio for three axes
ax1.set_aspect("equal")
ax2.set_aspect("equal")
ax3.set_aspect("equal")

#define colors and plot some dummy data
class_colors = ["#e74c3c", "#3498db", "#34495e", "#9b59b6", "#2ecc71", "#95a5a6"]
ax1.plot([0,1,2,3,4], [4,3,2,1,0], "D", markeredgecolor=class_colors[0])
ax2.plot([0,1,2,3,4], 1.2*np.array([4,3,2,1,0]), "D", markeredgecolor=class_colors[2])
ax3.plot([0,1,2,3,4], 1.3*np.array([4,3,2,1,0]), "D", markeredgecolor=class_colors[3])
ax3.plot(0.75*np.array([0,1,2,3,4]), 0.89*np.array([4,3,2,1,0]), "D", markeredgecolor=class_colors[4])

#define the color bar
cmap = matplotlib.colors.ListedColormap(class_colors)
bounds = np.linspace(0, 6, 6 + 1)
norm = matplotlib.colors.BoundaryNorm(bounds, cmap.N)
divider = make_axes_locatable(ax3)
cax1 = divider.append_axes("right", size="5%", pad=0.05)
cb = matplotlib.colorbar.ColorbarBase(cax1, cmap=cmap, norm=norm)
cb_ticks_loc = np.arange(0.5, 6, 1)
cb.set_ticks(cb_ticks_loc)
cb.set_ticklabels(["Firefly", "Dragon", "Aligator", "Dog", "Pig", "Stat"])

plt.show()

I use the append_axes method as it is suggested for example here.

However, the generated plot looks like this:

enter image description here

Notice how the last plot is slightly smaller than the other plots!

My question is: How do I avoid this? I want all the plots to have the same size and add some extra space at the end for the color bar.

Merlin1896
  • 1,751
  • 24
  • 39
  • take a look at this answer [SO](https://stackoverflow.com/questions/42396927/how-to-adjust-size-of-two-subplots-one-with-colorbar-and-another-without-in-py). When you append an axis you have to set `size='5%'` and this resizes the whole axes – gyx-hh Aug 14 '18 at 12:50
  • This could work, but it also introduces some white space between the plots which seems like a waste of space. – Merlin1896 Aug 14 '18 at 13:10
  • 2
    I would think that [this answer](https://stackoverflow.com/a/45634754/4124317) should have a solution for this problem. – ImportanceOfBeingErnest Aug 14 '18 at 13:57
  • @Merlin1896 you could always adjust the space by doing `fig.subplots_adjust(wspace=0.05)`. But the the post ImportanceOfBeingErnest linked is a much better approach. – gyx-hh Aug 14 '18 at 14:20
  • @ImportanceOfBeingErnest I think this answers my question. Thanks a lot! – Merlin1896 Aug 14 '18 at 14:35
  • 1
    I marked as duplicate. Feel free to edit your question if that does not work out. – ImportanceOfBeingErnest Aug 14 '18 at 15:07

0 Answers0