سؤال

As a follow-up to my previous question, I was wondering what is the proper way of creating multiple polar contourf subplots and add a single color bar to them. I tried this way:

import numpy as np
import matplotlib.pyplot as plt

#-- Generate Data -----------------------------------------
# Using linspace so that the endpoint of 360 is included...
azimuths = np.radians(np.linspace(0, 360, 100))
zeniths = np.arange(0, 70, 10)
r, theta = np.meshgrid(zeniths, azimuths)
values1 = np.random.random((azimuths.size, zeniths.size))
values2 = np.random.random((azimuths.size, zeniths.size))

#-- Plot... ------------------------------------------------
fig, axs = plt.subplots(1, 2, subplot_kw=dict(projection='polar'))
p1 = axs[0].contourf(theta, r, values1, 100)
p2 = axs[1].contourf(theta, r, values2, 100)
cbar = plt.colorbar(p2, ax=axs[1])

plt.show()

But the polar plot on the right is smaller than the other one:

enter image description here

Notice that if the colorbar line is commented the two subplots have the same size:

enter image description here

How do I get them to have the same size? Also, how can I resize the color bar to be as tall as the polar circles? Thanks!

هل كانت مفيدة؟

المحلول

For that you need to do some tweaks.

Instead of using colormap, which adds a new axes to plot the colormap scale, you would be better defining a plotting area to show the color map.

I modified you code a little bit in order to do so.

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

#-- Generate Data -----------------------------------------
# Using linspace so that the endpoint of 360 is included...
azimuths = np.radians(np.linspace(0, 360, 100))
zeniths = np.arange(0, 70, 10)
r, theta = np.meshgrid(zeniths, azimuths)
values1 = np.random.random((azimuths.size, zeniths.size))
values2 = np.random.random((azimuths.size, zeniths.size))

#-- Plot... ------------------------------------------------
fig, axs = plt.subplots(1, 2, figsize=(12,5),subplot_kw=dict(projection='polar'))
p1 = axs[0].contourf(theta, r, values1, 100)
p2 = axs[1].contourf(theta, r, values2, 100)

#-- obtaining the colormap limits
vmin,vmax = p2.get_clim()
#-- Defining a normalised scale
cNorm = mpl.colors.Normalize(vmin=vmin, vmax=vmax)
#-- Creating a new axes at the right side
ax3 = fig.add_axes([0.9, 0.1, 0.03, 0.8])
#-- Plotting the colormap in the created axes
cb1 = mpl.colorbar.ColorbarBase(ax3, norm=cNorm)
fig.subplots_adjust(left=0.05,right=0.85)
plt.show()

enter image description here

Hope it helps

نصائح أخرى

Use subplot2grid and plot the colorbar in a different axis:

azimuths = np.radians(np.linspace(0, 360, 100))
zeniths = np.arange(0, 70, 10)
r, theta = np.meshgrid(zeniths, azimuths)
values1 = np.random.random((azimuths.size, zeniths.size))
values2 = np.random.random((azimuths.size, zeniths.size))

#-- Plot... ------------------------------------------------
#fig, axs = plt.subplots(1, 2, subplot_kw=dict(projection='polar'))
fig = plt.figure()
ax1=plt.subplot2grid((1,10), (0, 0), colspan=4, projection='polar')
ax2=plt.subplot2grid((1,10), (0, 4), colspan=4, projection='polar')
ax3=plt.subplot2grid((1,15), (0, 14), colspan=1)
p1 = ax1.contourf(theta, r, values1, 100)
p2 = ax2.contourf(theta, r, values2, 100)
cbar = plt.colorbar(p2, cax=ax3)

plt.show()

enter image description here

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top