英文:
Create Polar Contour Plot
问题
我正在尝试使用Matplotlib重新创建下面显示的轮廓图,使用以下代码生成数据:
theta = np.arange(math.radians(0), math.radians(361), 0.01).tolist() # 初始化theta值
g4 = []
for i in theta:
v = ((2**(1-1)) * (1 + (1*math.cos(i)))**1) # 生成数据的方程
g4.append(v)
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.scatter(theta, g4, c=g4, cmap='viridis')
迄今为止,我已经能够使用此数据生成散点图,如下所示:
但是,我完全不确定如何将我的数据转换为正确的形式以生成所需的散点图,我尝试了:
ax.contourf(theta, g4, g4)
但是我得到了TypeError: 输入z必须是2D,而不是1D。
英文:
I am trying to recreate the contour plot shown below in Matplotlib, using the following code to generate the data:
theta = np.arange(math.radians(0), math.radians(361),0.01).tolist() # initialise theta values
g4 = []
for i in theta:
v = ((2**(1-1)) * (1 + (1*math.cos(i)))**1) # equation to generate the data
g4.append(v)
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.scatter(theta, g4, c=g4, cmap='viridis')
So far, I have been able to generate a scatter plot using this data which looks as follows:
However I am completely unsure of how to convert my data into the correct form to produce the required scatter plot, I have tried:
ax.contourf(theta, g4, g4)
But I get TypeError: Input z must be 2D, not 1D
答案1
得分: 3
基于这个回答这里,你可以这样做:
azimuths = np.radians(np.linspace(0, 360, 50))
zeniths = np.linspace(0, 4, 50)
r, theta = np.meshgrid(zeniths, azimuths)
f = 1 + np.cos(theta) # 你的函数基本上是这样的
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.contourf(theta, r, f)
这将产生:
这显然看起来不像你显示的示例图,但这是因为你的函数只是关于θ的函数,不是关于r的函数。因此,对于任何θ的值,它在径向方向上都是常数。
要获得类似示例图的东西,你可以将变量r
添加到函数中,例如:
azimuths = np.radians(np.linspace(0, 360, 250))
zeniths = np.linspace(0, 4, 250)
r, theta = np.meshgrid(zeniths, azimuths)
# 还依赖于r的函数
f = 1 + np.cos(theta) - (0.35 * r)**2
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
f[f <= 0] = np.inf # 掩盖负值的值
ax.grid()
ax.pcolormesh(theta, r, f, cmap="viridis_r") # 绘制函数
ax.grid(True)
ax.contour(theta, r, f, levels=10, colors="k", linewidths=0.25) # 添加轮廓
ax.set_rticks([0, 1, 2, 3, 4]); # 设置径向刻度值
英文:
Based on the answer here, you could do:
azimuths = np.radians(np.linspace(0, 360, 50))
zeniths = np.linspace(0, 4, 50)
r, theta = np.meshgrid(zeniths, azimuths)
f = 1 + np.cos(theta) # your function is essentially this
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.contourf(theta, r, f)
which produces:
This obviously doesn't look like the example plot that you show, but that's because your function is only a function of theta and not of r. So, it is therefore constant in the radial direction for any value of theta.
To get something similar to the example plot, you could add the r
variable in to the function, e.g.:
azimuths = np.radians(np.linspace(0, 360, 250))
zeniths = np.linspace(0, 4, 250)
r, theta = np.meshgrid(zeniths, azimuths)
# function that also depends on r
f = 1 + np.cos(theta) - (0.35 * r)**2
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
f[f <= 0] = np.inf # mask out values that are negative
ax.grid()
ax.pcolormesh(theta, r, f, cmap="viridis_r") # plot function
ax.grid(True)
ax.contour(theta, r, f, levels=10, colors="k", linewidths=0.25) # add contours
ax.set_rticks([0, 1, 2, 3, 4]); # set radial tick values
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论