0

I would like to plot points over curves using Sympy. I know there is a solution using a specific parameter markers, e.g. markers=[{'args': [-1, 2.5, 'ro']}], but from the documentation, it seems there is a possibility to plot points directly:

The figure can contain an arbitrary number of plots of SymPy expressions, lists of coordinates of points, etc.

I wasn't able to determine how point coordinates should be provided to the function, but from examples it seems two separate lists are used, one for x coordinates, one for y coordinates. However this doesn't work:

from sympy import plot

x = [-1]
y = [2.5]

point_plots = plot(x, y)

File ~\miniconda3\envs\gis\lib\site-packages\sympy\plotting\plot.py:1850 in plot series = [LineOver1DRangeSeries(*arg, **kwargs) for arg in plot_expr]

TypeError: 'NoneType' object is not iterable

So how does it work? I'm sorry to ask a such basic question...

mins
  • 6,478
  • 12
  • 56
  • 75

1 Answers1

1

Sadly, that functionality is badly documented and one has to look at the source code to understand how to use it.

markers accepts a list of dictionaries. Each dictionary must have the args keyword, which is a list/tuple containing the x-coordinates and y-coordinates. Then, you can provide other keyword arguments that will be used by matplotlib's plt.plot command to customize the output.

Sadly, if you only pass the markers keyword arguments, the plotting module will create a 3D plot :| So, you have to plot a single symbolic expression to tell it "use the 2D plot".

p = plot(0, # plot a symbolic expression to force to use 2D plot
    markers=[
        {"args": [[1, 2, 3, 4], [5, 4, 3, 2]]},
        {"args": [[4, 3], [1, 5]], "color": "r", "marker": "s", "linestyle": "None"},
    ],
    xlim=(0, 5)
)

PS: you might want to check out this improved plotting module: https://github.com/Davide-sd/sympy-plot-backends. It exposes the plot_list function that accepts list of coordinates instead of symbolic expressions.

Davide_sd
  • 10,578
  • 3
  • 18
  • 30
  • Ok, to be clear, to plot individual points, either I can use `MatplotlibBackend` (like [here](https://stackoverflow.com/questions/53289609/how-to-graph-points-with-sympy/70486636#70486636)) or use the module you linked, but there is no direct possibility using `sympy.plot`? – mins Sep 01 '22 at 15:46
  • It really depends on what you are trying to achieve. Are you looking to plot symbolic expressions and individual points? Then the above should answer your question. Are you looking to only plot individual points but you'd like to save yourself from typing all the necessary matplotlib's boilerplate code? Then the new module and `plot_list` is your friend. – Davide_sd Sep 01 '22 at 20:00