极坐标图中的箭头

4 投票
1 回答
16097 浏览
提问于 2025-04-18 08:12

我正在尝试绘制一个串联R-L-C电路中电阻器、电容器和电感器的相量图。我已经完成了所有的计算,并且可以用普通的 ax.plot(theta,r,....) 得到一个不错的图。

我想让这些相量看起来像箭头。我试着使用 ax.arrow(0,0,theta,magnitude),但它看起来还是一条线。我写的代码大致是这样的:GIST

我创建的图像是

我试着跟随我在 这个列表 上找到的例子,因为它和我想要实现的非常相似,结果生成了以下图像:

当我在我的电脑上运行他们的代码时,我得到了:

我使用的是Xubuntu 14.04,运行的是matplotlib 1.3.1。我注意到我使用的例子是在2009年使用的matplotlib 0.99。

任何帮助都将非常感激。

1 个回答

5

箭头的大小太大了,这段代码:

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()

生成了这个效果:

在这里输入图片描述

这样看起来更好了吗? :)

撰写回答