如何使用 sympy 图的标记参数?

发布于 2025-01-13 23:58:13 字数 391 浏览 0 评论 0原文

sympy plot 命令有一个 markers 参数:

markers :指定所需标记类型的字典列表。字典中的键应该相当于 matplotlib 的plot()函数的参数以及与标记相关的关键字参数。

如何使用markers参数?我失败的尝试范围从

from sympy import *
x = symbols ('x')
plot (sin (x), markers = 'o')

plot (sin (x), markers = list (dict (marker = 'o')))

The sympy plot command has a markers parameter:

markers : A list of dictionaries specifying the type the markers required. The keys in the dictionary should be equivalent to the arguments of the matplotlib's plot() function along with the marker related keyworded arguments.

How do I use the markers parameter? My failed attempts range from

from sympy import *
x = symbols ('x')
plot (sin (x), markers = 'o')

to

plot (sin (x), markers = list (dict (marker = 'o')))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提笔落墨 2025-01-20 23:58:13

不错的发现!

文档并没有把事情说清楚。深入研究源代码,会导致 图中的这些行.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

在 sympy 绘图中使用标记

PS:源代码显示了带有注释、矩形和填充的字典的类似方法(使用plt.fill Between()):

        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)

Nice find!

The documentation doesn't make things clear. Diving into the source code, leads to 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)

So, sympy just calls matplotlib's plot with:

  • the args key of the dictionary as positional parameters
  • all the other keys of the dictionary as keyword parameters

As matplotlib's plot allows a huge variety of parameters, they all are supported here. They are primarily meant to show extra markers onto the plot (you need to give their positions).

An example:

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}])

These get converted to:

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

using markers in sympy's plot

PS: The source code shows a similar approach for dictionaries with annotations, rectangles and fills (using 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)
卷耳 2025-01-20 23:58:13

更新使用@cards建议(在评论中)和这篇文章

您可以使用 Matplotlib 后端将标记直接添加到轴。

import sympy as sp
from sympy.plotting.plot import MatplotlibBackend

# plot with sympy
x = sp.symbols('x')

p0 = sp.plot(sp.sin(x),(x,-sp.pi,sp.pi),line_color='b',legend=True,show=False)

# point to be displayed
x0 = float(sp.pi/2)
y0 = float(sp.sin(sp.pi/2))

# plot with the commands from the backend (default matplotlib)
be = MatplotlibBackend(p0)
be.process_series()

plt = be.plt
plt.plot([x0,-x0],[y0,-y0],'r*',markersize=10,label="Star marker")

# Update the legend to include markers
plt.legend()

plt.show()

此解决方案还使用标记更新图例。

(Updated using @cards suggestion (in comments) and this post.

You can add markers directly to the axis using the Matplotlib backend.

import sympy as sp
from sympy.plotting.plot import MatplotlibBackend

# plot with sympy
x = sp.symbols('x')

p0 = sp.plot(sp.sin(x),(x,-sp.pi,sp.pi),line_color='b',legend=True,show=False)

# point to be displayed
x0 = float(sp.pi/2)
y0 = float(sp.sin(sp.pi/2))

# plot with the commands from the backend (default matplotlib)
be = MatplotlibBackend(p0)
be.process_series()

plt = be.plt
plt.plot([x0,-x0],[y0,-y0],'r*',markersize=10,label="Star marker")

# Update the legend to include markers
plt.legend()

plt.show()

This solution also updates the legend with the markers.

Adding markers to Sympy plot

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文