极坐标Matplotlib P中的箭头

2024-06-01 14:25:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图绘制一个串联R-L-C电路中电阻、电容和电感的电压相量。我已经做了所有的计算,我可以得到一个正常的ax.plot(theta,r,....)图。

我想使相量向量看起来像箭头。我一直在尝试使用ax.arrow(0,0,theta,magnitude),但它看起来还是一行。我编写的代码的要点如下:GIST

我创建的图像是

我尝试按照我在this list上找到的示例进行操作,因为它与我想要完成的任务非常相似,它会生成以下图像:

当我在电脑上运行他们的代码时

我在Xubuntu 14.04上运行matplotlib 1.3.1。我确实看到我使用的示例是在2009年使用matplotlib 0.99。

任何帮助都将不胜感激。


Tags: 代码图像示例plotmatplotlib绘制ax向量
1条回答
网友
1楼 · 发布于 2024-06-01 14:25:05

箭头尺寸太大,这个:

import matplotlib
import numpy as np
import matplotlib.pyplot as plt

print "matplotlib.__version__   = ", matplotlib.__version__
print "matplotlib.get_backend() = ", matplotlib.get_backend()


# radar green, solid grid lines
plt.rc('grid', color='#316931', linewidth=1, linestyle='-')
plt.rc('xtick', labelsize=15)
plt.rc('ytick', labelsize=15)

# force square figure and square axes looks better for polar, IMO
width, height = matplotlib.rcParams['figure.figsize']
size = min(width, height)
# make a square figure
fig = plt.figure(figsize=(size, size))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True, axisbg='#d5de9c')

r = np.arange(0, 3.0, 0.01)
theta = 2*np.pi*r
ax.plot(theta, r, color='#ee8d18', lw=3)
ax.set_rmax(2.0)
plt.grid(True)

ax.set_title("And there was much rejoicing!", fontsize=20)
#This is the line I added:
arr1 = plt.arrow(0, 0.5, 0, 1, alpha = 0.5, width = 0.015,
                 edgecolor = 'black', facecolor = 'green', lw = 2, zorder = 5)

# arrow at 45 degree
arr2 = plt.arrow(45/180.*np.pi, 0.5, 0, 1, alpha = 0.5, width = 0.015,
                 edgecolor = 'black', facecolor = 'green', lw = 2, zorder = 5)

plt.show()

产生:

enter image description here

更好?:)

相关问题 更多 >