# Initialize Enviroment


```python
from IPython.display import display, Math
from sympy import *
init_printing()
```

# Plotting
## Single variable function
### Basic plotting



```python
from sympy import symbols
from sympy.plotting import plot
x = symbols('x')

expr = x*x

plot(expr);
```


    <Figure size 640x480 with 1 Axes>


## Suppresss Str representation

If you don't like the '<sympy.plotting.plot.Plot at 0x7f6d26fb8780> string, add a ';' at the end of the plot command.


```python
plot(expr);
```

### Plot with range

To specify the plotting range, pass a tuple of the form ```(variable, lower_limit, upper_limit)```


```python
from sympy import symbols
from sympy.plotting import plot
x = symbols('x')

expr = x**2
expr_range = (x,-2,3)

plot(expr, expr_range);
```

### Title and Label

Pass in keyword arguments ```Title```,```xlabel```,```ylabel```. Latex is supported by these arguments.


```python
from sympy import symbols
from sympy.plotting import plot
x = symbols('x')

expr = x**2
expr_range = (x,-2,3)

title = '$y = {}$'.format(latex(expr))

plot(expr, expr_range, title = title, xlabel = 'x', ylabel = 'y');
```

### Line color

Pass keyword argument ```line_color```.


```python
from sympy import symbols
from sympy.plotting import plot
x = symbols('x')

expr = x**2

plot(expr, expr_range, line_color = 'r');
```

### Multiple plot with same range

Use the syntax of ```plot(expr_1, expr_2, expr_3, range)```


```python
expr_1 = x
expr_2 = x**2
expr_3 = x**3

plot(expr_1, expr_2, expr_3, (x, -1, 1));
```

### Multiple plots with different range

Use the syntax of 
```
plot(
    (expr_1,range_1),
    (expr_2,range_2),
    ...
)
```


```python
expr_1 = x**2
range_1 = (x,-2,2)

expr_2 = x
range_2 = (x,-1,1)

plot(
    (expr_1,range_1),
    (expr_2,range_2)
);
```

### Multiple plots with differnt colors

Plotting multiple plots with different colors is not straight forward. 

1. Pass ```show = False``` to suppress displaying the plot, save the returned object to a variable.
2. Get the indivisual data series by indexing the plotting object and set line_color seperately.
3. Call ```show()``` method of the plotting object to display the image.


```python
expr_1 = x**2
range_1 = (x,-2,2)

expr_2 = x
range_2 = (x,-1,1)

p = plot(
    (expr_1,range_1),
    (expr_2,range_2),
    show = False
);

p[0].line_color = 'r'
p[1].line_color = 'b'

p.show()
```

To add a legend to the plot, take additional two steps.

1. Pass ```Legend = True``` when construct the plotting object.
2. Set label for each data series seperately.


```python
expr_1 = x**2
range_1 = (x,-2,2)

expr_2 = x
range_2 = (x,-1,1)

p = plot(
    (expr_1,range_1),
    (expr_2,range_2),
    show = False,
    legend = True
);

p[0].line_color = 'r'
p[1].line_color = 'b'

p[0].label = 'Line 1'
p[1].label = 'Line 2'

p.show()
```

## 3D surface plot
### Single plot

Use ```plot3d(expr, x_range , y_range)``` to draw a 3D surface plot.


```python
from sympy import symbols
from sympy.plotting import plot3d

x, y = symbols('x y')

expr = x*y
x_range = (x, -5, 5)
y_range = (y, -5, 5)

plot3d(expr, x_range, y_range);
```

### Multiple plot

Use the following syntax to draw multiple 3D surface plot.

```
plot3d(
    （expr_1, x_range_1 , y_range_1）,
    （expr_2, x_range_2 , y_range_2）
)
```


```python
plot3d(
    (x**2 + y**2, (x, -5, 5), (y, -5, 5)),
    (x*y, (x, -3, 3), (y, -3, 3))
);
```

## Single variable parametric function

Use ```plot_parametric(expr_x, expr_y, range_u)``` to draw a single variable parametric function.


```python
from sympy import symbols, cos, sin
from sympy.plotting import plot_parametric

u = symbols('u')
expr_x = cos(u)
expr_y = sin(u)

p = plot_parametric(expr_x, expr_y, (u, -5, 5));
```

The result is not a perfect circle. Sympy offers the ```aspect_ratio``` argment to adjust the ratio, but it doesn't work in sympy 1.0 yet. The community is working on the problem and it may work in the next release.

## 3D parametric line

Use ```plot3d_parametric_line(expr_x, expr_y, expr_z, range_u)``` to draw a 3D parametric function.


```python
from sympy import symbols, cos, sin
from sympy.plotting import plot3d_parametric_line
u = symbols('u')
expr_x = cos(u)
expr_y = sin(u)
expr_z = u

plot3d_parametric_line(expr_x, expr_y, expr_z, (u, -5, 5));
```

## 3D parametric surface
Use ```plot3d_parametric_surface(expr_x, expr_y, expr_z, u_range, v_range)``` to draw a 3d parametric surface.


```python
from sympy import symbols, cos, sin
from sympy.plotting import plot3d_parametric_surface
u, v = symbols('u v')

expr_x = cos(u + v)
expr_y = sin(u-v)
expr_z = u-v
u_range = (u, -5, 5)
v_range = (v, -5, 5)

plot3d_parametric_surface(expr_x, expr_y, expr_z, u_range, v_range);
```

## Implicit function
### Single variable implicit function
Use ```plot_implicit(equition)``` to draw implicit function plotting. 


```python
p1 = plot_implicit(Eq(x**2 + y**2-5))
```

### Multiple variable implicit function


```python
p2 = plot_implicit(
    Eq(x**2 + y**2, 3),
    (x, -3, 3), 
    (y, -3, 3)
)
```

## Implicit inequilities

Pass an inequility to ```plot_implicit```


```python
plot_implicit(y > x**2);
```

To combine several conditions to define the region, and ```And,Or``` logic conjunctions.


```python
plot_implicit(And(y > x, y > -x));
```


```python
plot_implicit(Or(y > x, y > -x));
```

Sometimes Sympy doesn't choose the variable for horizontal axis as you expect.


```python
plot_implicit(Eq(y - 1));
```

In this case, use ```x_var``` to choose the variable for x_axis.


```python
plot_implicit(Eq(y - 1),x_var=x);
```

# Reference
[Sympy Documentation](http://docs.sympy.org/latest/index.html)

# Related Articles
* [Sympy Notes I]({filename}0026_sympy_intro_1_en.ipynb)
* [Sympy Notes II]({filename}0027_sympy_intro_2_en.ipynb)
* [Sympy Notes III]({filename}0028_sympy_intro_3_en.ipynb)
* [Sympy Notes IV]({filename}0029_sympy_intro_4_en.ipynb)

9f531f3a905df5f28dcd8b0fe3c659c14f2395b3
ipynb
Jupyter Notebook
0011_sympy/0029_sympy_intro_4_en.ipynb
junjiecai/jupyter_demos
8aa8a0320545c0ea09e05e94aea82bc8aa537750
MIT
2019-09-16T10:44:39.000Z
2021-09-04T18:55:52.000Z
0011_sympy/0029_sympy_intro_4_en.ipynb
junjiecai/jupyter_demos
8aa8a0320545c0ea09e05e94aea82bc8aa537750
MIT
0011_sympy/0029_sympy_intro_4_en.ipynb
junjiecai/jupyter_demos
8aa8a0320545c0ea09e05e94aea82bc8aa537750
MIT
2020-10-24T16:19:29.000Z
2021-09-04T18:55:57.000Z
Qwen/Qwen-72B
1. YES
2. YES