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:
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.