Matplotlib: 在极坐标轴上绘制一系列径向线

5
我正在尝试使用matplotlib复制某个图形:它应该看起来像这样。 Final plot 我已经了解到可以使用极坐标轴(PolarAxes)绘制径向点:例如,我使用以下片段创建了一个非常简单的极坐标图。
import matplotlib.pyplot as plt
fig = plt.figure()
# Set the axes as polar
ax = fig.add_subplot(111, polar=True)
# Draw some points
ax.plot([0],[1], 'o')
ax.plot([3],[1], 'o')
ax.plot([6],[1], 'o')

# Go clockwise
ax.set_theta_direction(-1)
# Start from the top
ax.set_theta_offset(1.570796327)

plt.savefig('test.png')

我得到了下面这样的图像:
所以我的问题是:是否有一种方法可以绘制如第一个图中的线条,并调整宽度以适应整个圆周?另外,关于如何处理颜色的提示将非常感谢。
更新:要绘制的数据非常简单:每个轨迹都是一个浮点数数组,其范围在0到9之间(颜色是从颜色映射RdYlGn派生的)。数组长度是96的倍数。
更新2:这是我使用的代码片段。
# mydata is a simple list of floats
a = np.array([[x for i in range(10)] for x in mydata])

# construct the grid
radius = np.linspace(0.2,0.4,10)
theta = np.linspace(0,2*np.pi,len(a))
R,T  = np.meshgrid(radius,theta)

fig = plt.figure()
ax = fig.add_subplot(111, polar = True)

# plot the values using the appropriate colormap
ax.pcolor(T,R,a,cmap=cm.RdYlGn)

2
你能说一下你的数据是什么格式吗? - aganders3
是的,非常抱歉我忘记了:马上更新... - mgalardini
1个回答

9

如果没有更多有关数据组织方式的信息,很难确定重新创建此图的最佳方法。在极坐标图上绘制不同宽度和颜色的线条很容易。但是,如果需要像示例中一样多的线条,可能会变得很慢。我还提供了一个极坐标伪彩色图的示例。

import numpy as np
import matplotlib.pyplot as plt

#Create radius and theta arrays, and a 2d radius/theta array
radius = np.linspace(0.2,0.4,51)
theta = np.linspace(0,2*np.pi,51)
R,T  = np.meshgrid(radius,theta)

#Calculate some values to plot
Zfun = lambda R,T: R**2*np.cos(T)
Z = Zfun(R,T)

#Create figure and polar axis
fig = plt.figure()
ax = fig.add_subplot(111, polar = True)

ax.pcolor(T,R,Z)    #Plot calculated values

#Plot thick red section and label it
theta = np.linspace(0,np.pi/4,21)
ax.plot(theta,[1.23 for t in theta],color='#AA5555',linewidth=10)   #Colors are set by hex codes
ax.text(np.pi/8,1.25,"Text")

ax.set_rmax(1.25)   #Set maximum radius

#Turn off polar labels
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)

Plot


谢谢您的回答:我已经更新了问题的数据格式。我注意到您在内部轨道上使用了pcolor函数,看起来很不错,但是每个矩形都有一个渐变,我想要每个矩形只有一种颜色。您认为我可以使用您用于外部轨道的普通绘图函数吗? - mgalardini
我有点困惑... 为什么你要创建一个 R、T 网格和一个从 (R,T) 到函数的映射,然后在绘图时交换 R 和 T 的顺序? - Dr Fabio Gori
如果你正在处理的函数是半径和角度的函数,那么说它是角度和半径的函数也同样有效。顺序不重要。你可以使用这些数组在第三维中计算值,因此你只需要保持一致性。在极坐标系下工作时,惯例是将半径写为第一个变量。对我来说,这是本能的。话虽如此,在matplotlib中使用极轴时,第一个参数是角度数组,第二个参数是半径数组。我们只需要按正确的顺序传递参数以正确绘制它。 - Mr. Squig

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接