重叠的极坐标等高线图和散点图

2 投票
1 回答
1552 浏览
提问于 2025-04-17 22:00

感谢这个非常有帮助的帖子,我终于搞明白了怎么制作极坐标填充等高线图。不过,当我想在同一个图上添加散点时,遇到了一些问题。以下是我最开始的代码:

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, 20))
zeniths = np.arange(0, 70, 10)

r, theta = np.meshgrid(zeniths, azimuths)
values = np.random.random((azimuths.size, zeniths.size))

#-- Plot... ------------------------------------------------
fig, ax = plt.subplots(subplot_kw=dict(projection='polar'))
ax.contourf(theta, r, values)

plt.show()

这段代码生成了这个图像:

Polar filled contour without scatter

如果我再添加一个散点图:

#-- Plot... ------------------------------------------------
fig, ax = plt.subplots(subplot_kw=dict(projection='polar'))
ax.contourf(theta, r, values)
ax.scatter(np.radians(140), 40, s=20, c='White')

我得到的图像变成了这样:

Polar filled contour with scatter

为什么我在填充等高线和坐标轴之间会出现厚厚的白色边框?我该怎么去掉它呢?

非常感谢!

1 个回答

2

哦,抱歉,我在问完问题两分钟后才想到答案。我刚意识到,添加散点图会 somehow 改变坐标轴的范围。强制设置坐标轴到想要的区间就能解决这个问题。

fig, ax = plt.subplots(subplot_kw=dict(projection='polar'))
ax.contourf(theta, r, values)
ax.scatter([np.radians(140)], [40], s=20, c='White')
ax.set_rmax(60)
ax.set_rmin(0)

enter image description here

我觉得我可以把这个问题留着,可能对其他用户也有帮助。

撰写回答