How do I use the markers parameter of a sympy plot?
Question:
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')))
Answers:
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
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)
(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.
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')))
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
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)
(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.