2x2 contourf plots sharing the same colorbar

  • Last Update :
  • Techknowledgy :

IIUC, you can just pass axs into colorbar:

plt.tight_layout()
plt.colorbar(cs4, ax = axs)
plt.show()

Suggestion : 2

I would like to add a single color bar on anycodings_python the right side of the following 2x2 plot.,I have been reviewing the solutions of anycodings_python previous question Matplotlib 2 Subplots, 1 anycodings_python Colorbar . However, I have four contourf() anycodings_python subplots, whereas the posted solutions use anycodings_python imshow() plot.,I do not know how to apply the previous anycodings_python solutions with the ax and cs object of my anycodings_python plot.,IIUC, you can just pass axs into anycodings_contourf colorbar:

I would like to add a single color bar on anycodings_python the right side of the following 2x2 plot.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-np.pi * 2, np.pi * 2, num = 50)
y = np.linspace(-np.pi * 2, np.pi * 2, num = 50)
def fun(x, y, pw1, pw2):
   X, Y = np.meshgrid(x, y)
return (X, Y, np.cos(X * Y) ** pw1 + np.sin(X + Y) ** pw2)

X, Y, f01 = fun(x, y, 0, 1)
X, Y, f10 = fun(x, y, 1, 0)
X, Y, f11 = fun(x, y, 1, 1)
X, Y, f12 = fun(x, y, 1, 2)

fig1, axs = plt.subplots(nrows = 2, ncols = 2, figsize = (8, 8), sharex = 'col', sharey = 'row')
   (ax1, ax2), (ax3, ax4) = axs

levels = np.linspace(-2, 2, 10)
cs1 = ax1.contourf(X, Y, f01, levels = levels, cmap = cmap)
ax1.set_ylabel('angle y', fontsize = 16)
ax1.tick_params(labelsize = 14)
ax1.set_title('Interference level 1', fontsize = 14)
ax1.set_aspect('equal')

cs2 = ax2.contourf(X, Y, f10, levels = levels, cmap = cmap)
ax2.set_title('Interference level 2', fontsize = 14)
ax2.set_aspect('equal')

cs3 = ax3.contourf(X, Y, f11, levels = levels, cmap = cmap)
ax3.set_ylabel('angle y', fontsize = 16)
ax3.set_xlabel('angle x', fontsize = 16)
ax3.tick_params(labelsize = 14)
ax3.set_title('Interference level 3', fontsize = 14)
ax3.set_aspect('equal')

cs4 = ax4.contourf(X, Y, f12, levels = levels, cmap = cmap)
ax4.set_xlabel('angle x', fontsize = 16)
ax4.tick_params(labelsize = 14)
ax4.set_title('Interference level 4', fontsize = 14)
ax4.set_aspect('equal')

plt.tight_layout()
plt.show()

I have been reviewing the solutions of anycodings_python previous question Matplotlib 2 Subplots, 1 anycodings_python Colorbar . However, I have four contourf() anycodings_python subplots, whereas the posted solutions use anycodings_python imshow() plot.

# im = imshow
print(im)
AxesImage(223.004,36;98.8364x98.8364)

# Axes
print(ax1)
AxesSubplot(0.0896322,0.541995;0.436434x0.408005)

# Contourf
print(cs1)
<matplotlib.contour.QuadContourSet object at 0x17d398940>

IIUC, you can just pass axs into anycodings_contourf colorbar:

plt.tight_layout()
plt.colorbar(cs4, ax = axs)
plt.show()

Suggestion : 3

Is there any way to ultimately plot the 2x2 seasons contourf subplots and only one color bar on the left (as would be the case just for one contourf plot)?,I am trying to make some seasonal plots of the trend from 13 years worth of data over the Eastern US and North Atlantic. I have the data trended and split into the 4 seasons. I want to display the seasons using a 2x2 subplot and contourf. So far I am able to make the 2x2 contourf subplots and using caxis make the limits in the contourf the same. However, when I try and display the colorbar, it will display the color bar for each 2x2 subplot and in doing so makes the area of the subplots smaller to make space for the colorbar.,From an old mfile, where I did a 3x3 plot in a loop. Should be usable to you. The trick is to give the colorbar a handle, and then position it manually. Remember to to set the clim to the same for each subplot.,This was amazing! I just got around to doing this today (I know, I said I would a month ago..). I had to fiddle with your positioning in order to a) get everything sized right and b) get the right plots in the right "subplot indices".

M = [0.05 0.3 0.55];
N = [0.7 0.45 0.2];
plotw = 0.25;
ploth = 0.25;
for m = 1: 1: 3
for n = 1: 1: 3
subplot('Position', [M(m) N(n) plotw ploth])
imagesc('something(:,:,m,n)')
caxis([-1 1]);
end
end
hcb = colorbar;
set(hcb, 'Position', [M(3) + plotw + 0.01 N(3) 0.02 3 * ploth])

Suggestion : 4

Setting sharex or sharey to True enables global sharing across the whole grid, i.e. also the y-axes of vertically stacked subplots have the same scale when using sharey=True.,For subplots that are sharing axes one set of tick labels is enough. Tick labels of inner Axes are automatically removed by sharex and sharey. Still there remains an unused empty space between the subplots.,By default, each Axes is scaled individually. Thus, if the ranges are different the tick values of the subplots do not align.,If you want a more complex sharing structure, you can first create the grid of axes with no sharing, and then call axes.Axes.sharex or axes.Axes.sharey to add sharing info a posteriori.

import matplotlib.pyplot as plt
import numpy as np

# Some example data to display
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('A single plot')
Text(0.5, 1.0, 'A single plot')
fig, axs = plt.subplots(2)
fig.suptitle('Vertically stacked subplots')
axs[0].plot(x, y)
axs[1].plot(x, -y)
[<matplotlib.lines.Line2D object at 0x7f71779dee00>]
fig, (ax1, ax2) = plt.subplots(2)
fig.suptitle('Vertically stacked subplots')
ax1.plot(x, y)
ax2.plot(x, -y)

Suggestion : 5

Remember to manually set the contour levels so that each plot has the same interval. The common label bar is draw from the first plot, and assumes all are the same. , The important thing to remember here, is to create two separate plot arrays to hold the individual plots for both panels. This is required b/c the label bar is taken from the last plot drawn. , To add a title, it is necessary to create a set of resources that are passed to only the panel template. It is best to give them a different name than the resources used for the individual plots. (e.g. resP = True) , Note: see panel_15.ncl above for a function that makes it easier to panel two sets of contour plots, each with their own labelbar. This example is an older version that is being kept for historical purposes.

gsn_attach_plots is the plot interface that will attach the a group plots along the y-axis (default).

     _________________________________
        |
        | | | |
        |
        plot1 | plot2 | plot3 | plot4 |
        |
        | | | |
        _________________________________

     _________________________________
        |
        |
        |
        plot1 plot2 plot3 plot4 |
        |
        |
        _________________________________

gsnAttachBorderOn gsnAttachBorderOn
   = True(
      default) = False
_________ _________
   |
   | | |
   |
   plot1 | | plot1 |
   |
   | | |
   |
   -- -- -- - | | |
   |
   | | |
   |
   plot2 | | plot2 |
   |
   | | |
   |
   -- -- -- - | | |
   |
   | | |
   |
   plot3 | | plot3 |
   |
   | | |
   |
   -- -- -- - | | |
   |
   | | |
   |
   plot4 | | plot4 |
   |
   | | |
   -- -- -- -- - -- -- -- -- -

  plot1 = gsn_csm_hov(wks, chi(: , {
     0: 100
  }), hres)
  xyplt1 = gsn_csm_xy(wks, x, y, xyres)
  plot2 = gsn_csm_hov(wks, chi(: , {
     220: 320
  }), hres)
  xyplt2 = gsn_csm_xy(wks, x, y, xyres)

  attachres1 = True
  attachres1 @gsnAttachPlotsXAxis = True;
  attaches along x - axis
  attachres2 = True
  attachid1 =
     gsn_attach_plots(plot1, xyplt1, attachres1, attachres2)
  attachid2 =
     gsn_attach_plots(plot2, xyplt2, attachres1, attachres2)

 gsn_panel(wks, (/plot1,plot2/), (/2,1/), False)
 NhlRemoveAnnotation(plot1, attachid1)

The open source tool gdal_translate was used to convert the jpeg file to a NetCDF file:

  gdal_translate - ot Int16 - of netCDF EarthMap_2500x1250.jpg EarthMap_2500x1250.nc