不错的发现!
文档没有说清楚。深入研究源代码,导致these lines in plot.py:
for marker in parent.markers:
# make a copy of the marker dictionary
# so that it doesn't get altered
m = marker.copy()
args = m.pop('args')
ax.plot(*args, **m)
所以,sympy 只是调用 matplotlib 的plot:
- 字典的
args键作为位置参数
- 字典的所有其他键作为关键字参数
由于 matplotlib 的 plot 允许参数种类繁多,因此这里都支持它们。它们主要是为了在绘图上显示额外的标记(您需要给出它们的位置)。
一个例子:
from sympy import symbols, sin, plot
x = symbols('x')
plot(sin(x), markers=[{'args': [2, 0, 'go']},
{'args': [[1, 3], [1, 1], 'r*'], 'ms': 20},
{'args': [[2, 4, 6], [-1, 0, -1], ], 'color': 'turquoise', 'ls': '--', 'lw': 3}])
这些被转换为:
ax.plot(2, 0, 'go') # draw a green dot at position 2,0
ax.plot([3, 5], [1, 1], 'r*', ms=20) # draw red stars of size 20 at positions 3,1 and 5,1
ax.plot([2, 4, 6], [-1, 0, -1], ], color='turquoise', ls='--', lw=3)
# draw a dotted line from 2,-1 over 4,0 to 6,-1
PS:源代码显示了一种类似的字典方法,带有注释、矩形和填充(使用plt.fillbetween()):
if parent.annotations:
for a in parent.annotations:
ax.annotate(**a)
if parent.rectangles:
for r in parent.rectangles:
rect = self.matplotlib.patches.Rectangle(**r)
ax.add_patch(rect)
if parent.fill:
ax.fill_between(**parent.fill)