向极坐标轮廓线多图添加颜色条 [英] Adding a colorbar to a polar contourf multiplot
问题描述
作为我之前的问题的后续行动,我想知道创建多个极坐标 contourf
子图并向其中添加单个颜色条的正确方法是什么?我这样尝试过:
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:
请注意,如果 colorbar
行被注释,这两个子图大小相同:
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!
推荐答案
为此,您需要进行一些调整。
For that you need to do some tweaks.
最好使用定义一个绘制区域来显示颜色图的方法,而不是使用增加新轴来绘制颜色图比例的colormap。
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.
我修改了代码
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()
希望有帮助
这篇关于向极坐标轮廓线多图添加颜色条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!