0

My goal is to plot three Graphs as subplots and use just one colorbar for the three of them, I have tryed that by making a figure with 4 subplots like shown in the following Code:

fig=plt.figure()
ax1=plt.subplot(1,3,1)
im=ax1.contourf(  MC, 50,vmax=Max_abs,vmin=Min_abs)
x0,x1 = ax1.get_xlim()
y0,y1 = ax1.get_ylim()
ax1.set_aspect((x1-x0)/(y1-y0))
ax2=plt.subplot(1,3,2,aspect=1)
im2=ax2.contourf(  averagedM, 50,vmax=Max_abs,vmin=Min_abs)
ax3=plt.subplot(1,3,3,aspect=1)
im3=ax3.contourf( residualM, 50,vmax=Max_abs,vmin=Min_abs)


#cax = divider.append_axes("right", size="5%", pad=0.05)
im4 = plt.colorbar(im3, ax=[ax1, ax2, ax3])

#cb.ax.set_visible(True)
plt.show()

The Matrices M1, M2 and M3 are previously computed in the Code, but I guess for my Problem that is not very important. The Problem is that the colorbar doesnt fit the size of the rest of the plots, there are some similar Questions already asked About this Topic but None of them were useful to my Code, so I would like to know how to adjust the colorbar in this specific case.

Thank youenter image description here

Lpng
  • 109
  • 1
  • 8
  • Is this what you are searching for? https://matplotlib.org/tutorials/intermediate/constrainedlayout_guide.html#colorbars – GittingGud Apr 26 '19 at 08:30
  • 2
    Possible duplicate of [Set Matplotlib colorbar size to match graph](https://stackoverflow.com/questions/18195758/set-matplotlib-colorbar-size-to-match-graph) and/or [Matplotlib 2 Subplots, 1 Colorbar](https://stackoverflow.com/questions/13784201/matplotlib-2-subplots-1-colorbar/13784887) – DavidG Apr 26 '19 at 08:34

1 Answers1

1

Because you fixed the aspect ratio of the subplots to be 1, the subplots don't adjust to the figure's size, but the colorbar does. If you would not do that, the subplots would be as tall as the colorbar (but no more quadratic, of course...).

In turn you could simply define a figure size, which fits to the needed aspect ratio of three quadratic plots plus one colorbar, e.g.:

fig = plt.figure(figsize=(12, 3))
SpghttCd
  • 10,510
  • 2
  • 20
  • 25