所以,据我所知,你想在同一个极坐标图上显示不同大小的数据。基本上你问的是如何为极坐标轴做类似twinx
的操作。
举个例子来说明这个问题,这将是很好,以显示在不同的规模比蓝色系列下面的情节绿色系列,同时保持它们对便于比较:
相同极轴
import numpy as np
import matplotlib.pyplot as plt
numpoints = 30
theta = np.linspace(0, 2*np.pi, numpoints)
r1 = np.random.random(numpoints)
r2 = 5 * np.random.random(numpoints)
params = dict(projection='polar', theta_direction=-1, theta_offset=np.pi/2)
fig, ax = plt.subplots(subplot_kw=params)
ax.fill_between(theta, r2, color='blue', alpha=0.5)
ax.fill_between(theta, r1, color='green', alpha=0.5)
plt.show()
然而,ax.twinx()
不适用于极坐标图工作。
可以解决这个问题,但这不是非常简单。这里有一个例子:
import numpy as np
import matplotlib.pyplot as plt
def main():
numpoints = 30
theta = np.linspace(0, 2*np.pi, numpoints)
r1 = np.random.random(numpoints)
r2 = 5 * np.random.random(numpoints)
params = dict(projection='polar', theta_direction=-1, theta_offset=np.pi/2)
fig, ax = plt.subplots(subplot_kw=params)
ax2 = polar_twin(ax)
ax.fill_between(theta, r2, color='blue', alpha=0.5)
ax2.fill_between(theta, r1, color='green', alpha=0.5)
plt.show()
def polar_twin(ax):
ax2 = ax.figure.add_axes(ax.get_position(), projection='polar',
label='twin', frameon=False,
theta_direction=ax.get_theta_direction(),
theta_offset=ax.get_theta_offset())
ax2.xaxis.set_visible(False)
# There should be a method for this, but there isn't... Pull request?
ax2._r_label_position._t = (22.5 + 180, 0.0)
ax2._r_label_position.invalidate()
# Ensure that original axes tick labels are on top of plots in twinned axes
for label in ax.get_yticklabels():
ax.figure.texts.append(label)
return ax2
main()
就是我们想要的东西,但它看起来相当不错,在第一。一个改进是将刻度标记对应于我们正在密谋什么:
plt.setp(ax2.get_yticklabels(), color='darkgreen')
plt.setp(ax.get_yticklabels(), color='darkblue')
但是,我们仍然有双网格,这是相当混乱。一个简单的方法是手动设置r-限制(和/或r-ticks),使得网格彼此重叠。或者,您可以编写自定义定位器来自动执行此操作。让我们坚持使用简单的方法在这里:
ax.set_rlim([0, 5])
ax2.set_rlim([0, 1])
警告:因为共享轴不为极坐标图工作,implmentation我上面将有任何改变的位置问题原始的轴。例如,向图中添加一个颜色条会导致各种问题。有可能解决这个问题,但我已经放弃了这部分。如果你需要它,让我知道,我会添加一个例子。
无论如何,这里是完整的,独立的代码来生成最终的数字:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1977)
def main():
numpoints = 30
theta = np.linspace(0, 2*np.pi, numpoints)
r1 = np.random.random(numpoints)
r2 = 5 * np.random.random(numpoints)
params = dict(projection='polar', theta_direction=-1, theta_offset=np.pi/2)
fig, ax = plt.subplots(subplot_kw=params)
ax2 = polar_twin(ax)
ax.fill_between(theta, r2, color='blue', alpha=0.5)
ax2.fill_between(theta, r1, color='green', alpha=0.5)
plt.setp(ax2.get_yticklabels(), color='darkgreen')
plt.setp(ax.get_yticklabels(), color='darkblue')
ax.set_ylim([0, 5])
ax2.set_ylim([0, 1])
plt.show()
def polar_twin(ax):
ax2 = ax.figure.add_axes(ax.get_position(), projection='polar',
label='twin', frameon=False,
theta_direction=ax.get_theta_direction(),
theta_offset=ax.get_theta_offset())
ax2.xaxis.set_visible(False)
# There should be a method for this, but there isn't... Pull request?
ax2._r_label_position._t = (22.5 + 180, 0.0)
ax2._r_label_position.invalidate()
# Bit of a hack to ensure that the original axes tick labels are on top of
# whatever is plotted in the twinned axes. Tick labels will be drawn twice.
for label in ax.get_yticklabels():
ax.figure.texts.append(label)
return ax2
if __name__ == '__main__':
main()
请修复您的缩进。看起来您正在使用大量自定义的类,这样可以排除困难。你可以用标准的matplotlib对象来演示你想要的吗? – tacaswell
感谢您提供图像。我的信誉分数太低了,无法为我自己做。 – fidelitas
关于如何获得最后一个半径标签显示的第二个问题,只是因为您使用'np.arange'设置了刻度。 'arange'在端点之前停止(例如'np.arange(0,0.5,0.1)'产生'array([0.0,0.1,0.2,0.3,0.4])'')。如果您想包含端点,请使用'end + dx'(其中'dx'是您的间隔)。 –