对于 270 到 90 度之间的值,将 x 轴中心置于 0 处

问题描述 投票:0回答:1

我有一组以 0 为中心、跨度从 90 到 270 度的数据。当我制作散点图时,我希望它也以笛卡尔平面 0 为中心。

import numpy as np
import matplotlib as plt 

ang = np.array([0, 90, 270])
radi = [1, 2, 3]

plt.scatter(ang, radi)

将数据转换为-180到180区间,并重新定义x轴标签:

test = -1*(np.array(ang)+180)%360 - 180

fig, ax = plt.subplots()

labels = [90, 0, 270]
ax.scatter(test, radi)

plt.xticks(np.sort(test), labels, rotation='vertical')

plt.show()

我得到了想要的可视化效果。有更好的方法吗?

python numpy matplotlib
1个回答
0
投票

问题是,您试图将极坐标(角度和半径)绘制为笛卡尔坐标(x 和 y)。您可以像这样制作极坐标图:

ang = np.array([0, 90, 270])
radi = np.array([1, 2, 3])

theta = ang * np.pi / 180

fig = plt.figure()
ax = fig.add_subplot(projection='polar')
ax.scatter(theta, radi)
plt.show()

或者,您可以使用公式将极坐标转换为笛卡尔坐标:

x = radius * cos(theta)
y = radius *  sin(theta)

x = radi * np.cos(theta)
y = radi * np.sin(theta)

plt.figure()
plt.scatter(x, y)
plt.xlim(-4, 4)
plt.ylim(-4, 4)
plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.