Bro*_*het 8 python matplotlib scatter-plot polar-coordinates
我试图在极坐标中制作散点图,其中轮廓线叠加到点云上.我知道如何在笛卡尔坐标中使用numpy.histogram2d:
# Simple case: scatter plot with density contours in cartesian coordinates
import matplotlib.pyplot as pl
import numpy as np
np.random.seed(2015)
N = 1000
shift_value = -6.
x1 = np.random.randn(N) + shift_value
y1 = np.random.randn(N) + shift_value
fig, ax = pl.subplots(nrows=1,ncols=1)
ax.scatter(x1,y1,color='hotpink')
H, xedges, yedges = np.histogram2d(x1,y1)
extent = [xedges[0],xedges[-1],yedges[0],yedges[-1]]
cset1 = ax.contour(H,extent=extent)
# Modify xlim and ylim to be a bit more consistent with what's next
ax.set_xlim(xmin=-10.,xmax=+10.)
ax.set_ylim(ymin=-10.,ymax=+10.)
Run Code Online (Sandbox Code Playgroud)
输出在这里:

但是,当我尝试将我的代码转换为极坐标时,我会得到扭曲的轮廓线.这是我的代码和生成的(错误的)输出:
# Case with polar coordinates; the contour lines are distorted
np.random.seed(2015)
N = 1000
shift_value = -6.
def CartesianToPolar(x,y):
r = np.sqrt(x**2 + y**2)
theta = np.arctan2(y,x)
return theta, r
x2 = np.random.randn(N) + shift_value
y2 = np.random.randn(N) + shift_value
theta2, r2 = CartesianToPolar(x2,y2)
fig2 = pl.figure()
ax2 = pl.subplot(projection="polar")
ax2.scatter(theta2, r2, color='hotpink')
H, xedges, yedges = np.histogram2d(x2,y2)
theta_edges, r_edges = CartesianToPolar(xedges[:-1],yedges[:-1])
ax2.contour(theta_edges, r_edges,H)
Run Code Online (Sandbox Code Playgroud)
该错误的输出是在这里:

有没有办法让轮廓线处于适当的比例?
编辑以解决评论中提出的建议.
编辑2:有人建议这个问题可能与此问题重复.虽然我认识到这些问题是相似的,但我特别指出在散点图上绘制点的密度轮廓.另一个问题是如何绘制指定的任何数量的轮廓水平以及点的坐标.
Joe*_*ton 13
问题是你只是转换数组的边缘.通过仅转换边缘的x和y坐标,您可以有效地转换2D数组上对角线的坐标.此行具有非常小的theta值范围,您将该范围应用于整个网格.
在大多数情况下,你可以转换整个网格(IE的二维数组x和y,产生的二维数组theta和r)到极坐标.
代替:
H, xedges, yedges = np.histogram2d(x2,y2)
theta_edges, r_edges = CartesianToPolar(xedges[:-1],yedges[:-1])
Run Code Online (Sandbox Code Playgroud)
做类似的事情:
H, xedges, yedges = np.histogram2d(x2,y2)
xedges, yedges = np.meshgrid(xedges[:-1],yedges[:-1]
theta_edges, r_edges = CartesianToPolar(xedges, yedges)
Run Code Online (Sandbox Code Playgroud)
作为一个完整的例子:
import numpy as np
import matplotlib.pyplot as plt
def main():
x2, y2 = generate_data()
theta2, r2 = cart2polar(x2,y2)
fig2 = plt.figure()
ax2 = fig2.add_subplot(111, projection="polar")
ax2.scatter(theta2, r2, color='hotpink')
H, xedges, yedges = np.histogram2d(x2,y2)
xedges, yedges = np.meshgrid(xedges[:-1], yedges[:-1])
theta_edges, r_edges = cart2polar(xedges, yedges)
ax2.contour(theta_edges, r_edges, H)
plt.show()
def generate_data():
np.random.seed(2015)
N = 1000
shift_value = -6.
x2 = np.random.randn(N) + shift_value
y2 = np.random.randn(N) + shift_value
return x2, y2
def cart2polar(x,y):
r = np.sqrt(x**2 + y**2)
theta = np.arctan2(y,x)
return theta, r
main()
Run Code Online (Sandbox Code Playgroud)

但是,您可能会注意到这看起来有些不正确.那是因为ax.contour隐含地假设输入数据在常规网格上.我们给它一个笛卡尔坐标的规则网格,但不是极坐标中的规则网格.假设我们在极坐标中传递了一个规则网格.我们可以重新采样网格,但有一种更简单的方法.
要正确绘制2D直方图,请计算极坐标空间中的直方图.
例如,执行类似的操作:
theta2, r2 = cart2polar(x2,y2)
H, theta_edges, r_edges = np.histogram2d(theta2, r2)
ax2.contour(theta_edges[:-1], r_edges[:-1], H)
Run Code Online (Sandbox Code Playgroud)
作为一个完整的例子:
import numpy as np
import matplotlib.pyplot as plt
def main():
x2, y2 = generate_data()
theta2, r2 = cart2polar(x2,y2)
fig2 = plt.figure()
ax2 = fig2.add_subplot(111, projection="polar")
ax2.scatter(theta2, r2, color='hotpink')
H, theta_edges, r_edges = np.histogram2d(theta2, r2)
ax2.contour(theta_edges[:-1], r_edges[:-1], H)
plt.show()
def generate_data():
np.random.seed(2015)
N = 1000
shift_value = -6.
x2 = np.random.randn(N) + shift_value
y2 = np.random.randn(N) + shift_value
return x2, y2
def cart2polar(x,y):
r = np.sqrt(x**2 + y**2)
theta = np.arctan2(y,x)
return theta, r
main()
Run Code Online (Sandbox Code Playgroud)

最后,您可能会注意到上述结果略有改变.这与面向单元格的网格约定(x[0,0], y[0,0]给出单元格的中心)与面向边缘的网格约定(x[0,0], y[0,0]给出单元格的左下角)有关. ax.contour期望事物以细胞为中心,但是你给它边对齐的x和y值.
这只是一个半细胞移位,但如果您想修复它,请执行以下操作:
def centers(bins):
return np.vstack([bins[:-1], bins[1:]]).mean(axis=0)
H, theta_edges, r_edges = np.histogram2d(theta2, r2)
theta_centers, r_centers = centers(theta_edges), centers(r_edges)
ax2.contour(theta_centers, r_centers, H)
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
3693 次 |
| 最近记录: |