将颜色条添加到极坐标轮廓多点

时间:2014-03-14 22:33:10

标签: python matplotlib contour

作为我之前question的后续内容,我想知道创建多个极contourf子图并向其添加单个颜色条的正确方法是什么。我试过这种方式:

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()

但右边的极坐标图比另一个小:

enter image description here

请注意,如果注释colorbar行,则两个子图具有相同的大小:

enter image description here

如何让它们具有相同的尺寸?另外,如何调整颜色条的大小和极圈一样高?谢谢!

2 个答案:

答案 0 :(得分:2)

为此你需要做一些调整。

您可以更好地定义绘图区域以显示颜色贴图,而不是使用添加新轴来绘制色彩图比例的色彩图。

为了做到这一点,我修改了一些代码。

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

希望有所帮助

答案 1 :(得分:2)

使用subplot2grid并在不同的轴上绘制colorbar

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