'How to change sympy plot linestyle?

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:



Solution 1:[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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JohanC