python - 如何使用 matplotlib 在极坐标图中绘制曲线/弧线?
问题描述
我试图弄清楚如何在极坐标图中的 2 个点之间创建一条弧线,但我绘制的线是连接它们的直线,即使该图是极坐标的。
我需要使用不同的绘图功能来代替ax.plot
吗?
我注意到有些补丁matplotlib
可能是我应该使用的,但我不确定如何以这种方式添加它们。
如何在极坐标图上从 A 点和 B 点画一条曲线?
# Create polar plot object
with plt.style.context("seaborn-white"):
fig = plt.figure(figsize=(5,5))
ax = fig.add_subplot(111, projection="polar")
# Draw 3 lines
for degree in [90, 210, 330]:
rad = np.deg2rad(degree)
ax.plot([rad,rad], [0,1], color="black", linewidth=2)
# Connect two points with a curve
for curve in [[[90, 210], [0.5, 0.8]]]:
curve[0] = np.deg2rad(curve[0])
ax.plot(curve[0], curve[1])
解决方案
极坐标投影意味着您不再使用 x,y 坐标系,而是使用极坐标系。尽管如此,两点之间的图仍然是它们之间的直线。
你想要做的就是像这样自己定义弧:
from matplotlib import pyplot as plt
from scipy.interpolate import interp1d
import numpy as np
with plt.style.context("seaborn-white"):
fig = plt.figure(figsize=(5,5))
ax = fig.add_subplot(111, projection="polar")
# Draw 3 lines
for degree in [90, 210, 330]:
rad = np.deg2rad(degree)
ax.plot([rad,rad], [0,1], color="black", linewidth=2)
# Connect two points with a curve
for curve in [[[90, 210], [0.5, 0.8]]]:
curve[0] = np.deg2rad(curve[0])
x = np.linspace( curve[0][0], curve[0][1], 500)
y = interp1d( curve[0], curve[1])( x)
ax.plot(x, y)
plt.show()
推荐阅读
- sql - 四舍五入消除小数
- javascript - GridFS:TypeError:grid.mongo.ObjectID 不是 createReadStream 的构造函数
- javascript - 如何使用谷歌应用脚本获取特定列谷歌电子表格中的下一个空行
- reactjs - React Apollo 和条件布尔变量
- r - 有没有办法在尺寸美学中添加调色板?
- python-3.x - FFMPEG BlockingIOError: [Errno 11] 资源暂时不可用
- c# - 可疑的数据库访问功能
- powershell - 使用 get-appxpackage 卸载所有未由我安装的应用程序
- javascript - Moment.js 日期/时间基于不同时区偏移
- python - Selenium 无法定位元素