0

I am trying to plot a single colorbar (range : 0, max), for all the sublots that I have. I have tried the solutions here Matplotlib 2 Subplots, 1 Colorbar but they require using plt.subplots, which I am not using.

Here is my current code which displays 3 individual colorbars.

fig = plt.figure(figsize=(15,15))
G = gridspec.GridSpec(2, 2)


#Haut
top = plt.subplot(G[0,0], projection='polar')
phi2D_grid, rho2D_grid = np.meshgrid(phi_grid, rho_grid)
plt.pcolormesh(phi2D_grid, rho2D_grid, compteur_top, cmap='jet', vmin=0, vmax=max)
plt.colorbar()

#Côtés
lat = plt.subplot(G[1,:])
phi2D_grid, z2D_grid = np.meshgrid(phi_grid, z_grid)
plt.pcolormesh(phi2D_grid, z2D_grid, compteur_lat, cmap='jet', vmin=0, vmax=max)
 plt.colorbar()


#Bas
bot = plt.subplot(G[0, 1], projection='polar')
phi2D_grid, rho2D_grid = np.meshgrid(phi_grid, rho_grid)
plt.pcolormesh(phi2D_grid, rho2D_grid, compteur_bot, cmap='jet', vmin=0, vmax=max)
plt.colorbar()

I think I do need to use Gridspec here given that I need to display my figures as follow:

3 graphs

Galia
  • 23
  • 4
  • 2
    Well, they don't require `subplots` really, it's just an easy way of creating such examples. Did you try `plt.colorbar(mesh, ax=[top, lat, bot])`? – ImportanceOfBeingErnest Apr 19 '19 at 19:45
  • Am I supposed to replace mesh by a specific word? Python doesn't recognize it – Galia Apr 19 '19 at 22:23
  • The signature of [`colorbar`](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.colorbar.html) is `colorbar(mappable=None, cax=None, ax=None, **kw)`. What I called `mesh` is the `mesh = pcolormesh(...)` you created and for which you want to create the colorbar. – ImportanceOfBeingErnest Apr 19 '19 at 22:36
  • Since you're using `gridspec` already, I'd suggest to directly assign the colorbar to one of the `G[]` subplots; e.g. `plt.colorbar( …, cax=G[1,1],use_gridspec=True,… )`. You'll probably want to play around with the gridspec options `width_ratios` next to format it nicely. Or: e.g. use `cbax =fig.add_axes([lat.get_position().x1+0.02,0.15, 0.11,0.03])` to place a new subplot right next to the `lat` plot, and then hand it over as a `colorbar(...cax=cbax,…)` argument. – Asmus Apr 20 '19 at 10:27

0 Answers0