python - matplotlib 在极坐标图中颤动
问题描述
我想在 matplotlib 极坐标图中创建一个颤动图。我的 MWE 正在按预期工作rmin=0
,如下图(左子图)所示,但对于rmin
(右子图)的任何其他值都没有。
我还尝试计算极坐标中的端点并quiver()
与angles='xy'
参数一起使用,但是由于某种原因这根本不起作用,我不明白为什么。但是,如果我将线图与计算的端点结合使用,它会按预期工作。
我更喜欢箭袋图而不是线图的原因是我想为情节设置动画并动态添加/删除点。对于 quiver 图,我可以通过设置偏移量和U
和V
值来简单地做到这一点,对于线图,我必须添加/删除线图并结合set_data()
.
from matplotlib import pyplot as plt
import numpy as np
if __name__ == '__main__':
N = 11 # number of samples
x = np.linspace(0, 10, N)
y = np.sin(x)
theta, r = np.arctan2(y, x), np.hypot(x, y) # convert to polar
gradient = [np.ones(N), np.cos(x)]
gradient = gradient / np.hypot(gradient[0], gradient[1]) # normalize
endpoints_x = x + gradient[0]
endpoints_y = y + gradient[1]
# convert cartesian endpoints to polar
endpoints_theta, endpoints_r = np.arctan2(endpoints_y, endpoints_x),\
np.hypot(endpoints_x, endpoints_y)
fig, axes = plt.subplots(1, 2, figsize=(10, 8), subplot_kw=dict(polar=True))
for i in range(2):
axes[i].scatter(theta, r, marker='o')
axes[i].plot(np.stack((theta, endpoints_theta)), np.stack((r, endpoints_r)), 'r')
# works only for rmin=0
axes[i].quiver(theta, r, gradient[0], gradient[1], angles='uv')
# does not work at all
#axes[i].quiver(theta, r, endpoints_theta, endpoints_r, angles='xy')
axes[i].set_thetamin(-90)
axes[i].set_thetamax(90)
axes[1].set_rmin(2)
fig.tight_layout()
plt.show()
解决方案
推荐阅读
- excel - 如何在 xml 元素中添加字符 - VBA
- sapui5 - 单击 sap.m.Tree 列表项时的面包屑并在单击面包屑时展开折叠树
- android - 为 TextViews 应用程序范围设置文本对齐属性
- android - 在更改屏幕方向时更改相机预览模式
- angular - ngx-bootstrap 的 datePicker (onShown) 事件在真正显示日历之前触发
- python - 将 txt 文件读取为包含 numpy 数组的 dict
- python - 在哪里可以找到 Windows 中的 Python 列表、字典方法实现?
- css - 在表格单元格元素中水平和垂直居中
- java - 使用 JUnit 5 和 Spring Boot 2.0.0 进行嵌套测试
- html - CSS 和 Bootstrap - 整页图像不起作用