问题
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:
Notice that if the colorbar
line is commented the two subplots have the same size:
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!
回答1:
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()
Hope it helps
回答2:
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()
来源:https://stackoverflow.com/questions/22416965/adding-a-colorbar-to-a-polar-contourf-multiplot