1
import sympy
import matplotlib.pyplot as plt
from sympy import plot
x=sympy.symbols('x')
f=(x**2-4)**2/8-1
plot(f,(x,0,3),xlabel='x',ylabel='y',label='$f(x)$')
plt.scatter(2,-1,label="titik optimum",color="blue",marker="s",s=50)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Metode Golden Search')
plt.legend()
plt.show()

enter image description here

enter image description here

I want to plot symbolic function and point in one figure. But the result is graph of symbolic function is separated with the point plot. Anyone know how to show plot symbolic function and point in one figure in python?

  • 1
    Please see [here](https://stackoverflow.com/a/46813804/8881141), set `show=False`, and read the comment by Sébastien Loisel that you have to use `backend._process_series(backend.parent._series, ax, backend.parent)`. – Mr. T Jan 20 '22 at 07:58

1 Answers1

1

Sympy's plot supports markers= and annotations= keywords. These are lists of dictionaries towards matplotlib's plot and annotate functions. The markers don't seem to appear in the legend, but you can use annotations instead.

from sympy import symbols, plot

x = symbols('x')
f = (x ** 2 - 4) ** 2 / 8 - 1
plot(f, (x, 0, 3), xlabel='x', ylabel='y', label='$f(x)$', title='Metode Golden Search', legend=True, size=(12, 5),
     markers=[{'args': [2, -1], 'mfc': "none", 'mec': "blue", 'marker': "s", 'ms': 20}],
     annotations=[{'xy': (2, -1), 'text': "titik optimum\n", 'ha': 'center', 'va': 'bottom', 'color': 'blue'}])

sympy plot with markers and annotations

JohanC
  • 71,591
  • 8
  • 33
  • 66