1

I am new to sympy in python.

When I tried to create a plot with sympy today, I was not able to change the linestyle. I find that the plot object in sympy has both 'line_color' attribute and 'linestyle' attribute.

I was able to use the line_color attribute to change color successfully, but the linestyle attribute did not work for me.

Any suggestions? Thank you!

x = symbols('x')
plot_1 = plot(x**2, xlim=[0,10], ylim=[0,10], show=False)
plot_2 = plot(0.5*x**2, xlim=[0,10], ylim=[0,10], show=False)
plot_1.extend(plot_2)
plot_1[0].linestyle='dashed'
plot_1[0].line_color='red'
plot_1.show()

The output looks like this:

Leshui
  • 111
  • 4
  • Within sympy, only the line color can be changed. It can be set directly via `plot(x**2, ..., line_color='green')`. To change other properties, you might need to move the plot to matplotlib (see [this post](https://stackoverflow.com/questions/46810880/display-two-sympy-plots-as-two-matplotlib-subplots/46813804#46813804) including the comment about newer versions of sympy) – JohanC May 11 '20 at 12:48
  • Thank you! It is helpful to confirm that. I hope future updates will allow for this feature. – Leshui May 11 '20 at 17:43

1 Answers1

1

In a Jupyter Notebook you can use matplotlib magic and the _backend attribute of the sympy plot object to access all attributes of the corresponding matplotlib plot object:

from sympy import *
x = symbols('x')
%matplotlib notebook
plot_1 = plot(x**2, xlim=[0,10], ylim=[0,10], show=False)
plot_2 = plot(0.5*x**2, xlim=[0,10], ylim=[0,10], show=False)
plot_1.extend(plot_2)
#plot_1[0].linestyle='dashed'
plot_1[0].line_color='red'
plot_1.show()
plot_1._backend.ax[0]._children[0].set_linestyle('dashed')

Dashed line in a Jupyter notebook

JohanC
  • 71,591
  • 8
  • 33
  • 66