What do you usually teach visualization for !!!! That's why

ʻI want to evolve the OP function !!`

Create a function to draw a beautiful curve ʻOP` with matplotlib of Python language Make visualization.

In addition, it is not interesting if it is just a still image It has the function of ipywidgets.

Beyond Beyond moves when the number `t`

is changed.

```
import numpy as np
import matplotlib.pyplot as plt
from ipywidgets import interact, FloatSlider, IntSlider
import warnings
warnings.simplefilter('ignore')
%matplotlib inline
def oppai(y,t):
x_1 = (1.5 * np.exp((0.12*np.sin(t)-0.5) * (y + 0.16 *np.sin(t)) ** 2)) / (1 + np.exp(-20 * (5 * y + np.sin(t))))
x_2 = ((1.5 + 0.8 * (y + 0.2*np.sin(t)) ** 3) * (1 + np.exp(20 * (5 * y +np.sin(t)))) ** -1)
x_3 = (1+np.exp(-(100*(y+1)+16*np.sin(t))))
x_4 = (0.2 * (np.exp(-(y + 1) ** 2) + 1)) / (1 + np.exp(100 * (y + 1) + 16*np.sin(t)))
x_5 = (0.1 / np.exp(2 * (10 * y + 1.2*(2+np.sin(t))*np.sin(t)) ** 4))
x = x_1 + (x_2 / x_3) + x_4 + x_5
return x
t = FloatSlider(min=0.1, max=5.0, step=0.1, value=0)
y = np.arange(-3, 3.01, 0.01)
@interact(t=t)
def plot_oppai(t):
x = oppai(y,t)
plt.figure(figsize=(10,9))
plt.axes().set_aspect('equal', 'datalim')
plt.grid()
plt.plot(x, y, 'black')
plt.show()
```

Whew With this, it's just drawn with a dull black curve You haven't reached the realm of art.

Let's color the function to get closer to the ideal.

```
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from ipywidgets import interact, FloatSlider, IntSlider
import warnings
warnings.simplefilter('ignore')
%matplotlib inline
def oppai(y,t):
x_1 = (1.5 * np.exp((0.12*np.sin(t)-0.5) * (y + 0.16 *np.sin(t)) ** 2)) / (1 + np.exp(-20 * (5 * y + np.sin(t))))
x_2 = ((1.5 + 0.8 * (y + 0.2*np.sin(t)) ** 3) * (1 + np.exp(20 * (5 * y +np.sin(t)))) ** -1)
x_3 = (1+np.exp(-(100*(y+1)+16*np.sin(t))))
x_4 = (0.2 * (np.exp(-(y + 1) ** 2) + 1)) / (1 + np.exp(100 * (y + 1) + 16*np.sin(t)))
x_5 = (0.1 / np.exp(2 * (10 * y + 1.2*(2+np.sin(t))*np.sin(t)) ** 4))
x = x_1 + (x_2 / x_3) + x_4 + x_5
return x
t = FloatSlider(min=0.1, max=5.0, step=0.1, value=0)
y = np.arange(-3, 3.01, 0.01)
@interact(t=t)
def plot_oppai(t):
x = oppai(y,t)
plt.figure(figsize=(10,9))
plt.axes().set_aspect('equal', 'datalim')
plt.grid()
#Improvement points
b_chiku = (0.1 / np.exp(2 * (10 * y + 1.2*(2+np.sin(t))*np.sin(t)) ** 4))
b_index = [i for i ,n in enumerate(b_chiku>3.08361524e-003) if n]
x_2,y_2 = x[b_index],y[b_index]
plt.axes().set_aspect('equal', 'datalim')
plt.plot(x, y, '#F5D1B7')
plt.fill_between(x, y, facecolor='#F5D1B7', alpha=1)
plt.plot(x_2, y_2, '#F8ABA6')
plt.fill_between(x_2, y_2, facecolor='#F8ABA6', alpha=1)
plt.show()
```

It's a wonderful color! !! !!

On the mechanism of matplotlib First you have to decide where to plot.

Two numbers are needed for the scatter plot. The values on the vertical axis and the values on the horizontal axis.

The values in the vertical axis used for the entire drawing are fixed.
`y = np.arange(-3, 3.01, 0.01)`

`y`

is a group of numbers from -3 to 3 in increments of 0.01.

On the other hand, the value in the horizontal axis direction is calculated.
The function ʻoppai (y, t) `calculates one by one for the numerical group`

y`. Create a set of numbers `

x`.

The argument `t`

is a numerical value for fine adjustment.
This is also calculated in increments of 0.1 from 0.1 to 5.

Plot on a scatter plot using the x and y numbers.
`plt.plot(x, y)`

This will draw this smooth curve ʻOP`.

Next is `plt.fill_between`

This fills with a color.

And the biggest point is the curved `B district`

This is my personal favorite point.

Again, you have to calculate the range of fill points.

```
b_chiku = (0.1 / np.exp(2 * (10 * y + 1.2*(2+np.sin(t))*np.sin(t)) ** 4))
b_index = [i for i ,n in enumerate(b_chiku>3.08361524e-003) if n]
x_2,y_2 = x[b_index],y[b_index]
```

Here, it seems that it fits in the right range in the vertical and horizontal directions I am doing numerical calculation.

ʻEnumerate (b_chiku> 3.08361524e-003)` Whether it is larger than that number is returned as`

True` or`

False`. Since that is the index value of `

B area`, the value of`

B area` Get the numerical value by index from the numerical group of the whole curve ʻOP`

.

Now that you have calculated the `B district`

, paint it with a nice color.
Finish.

No Numerical calculation is difficult when moving

But You should be able to get the real thrill that you can't get with still images.

That's why using Python or matplotlib. Even if you're not familiar with data science, visualization is worth studying.

By the way, the color is `# F8ABA6`

I liked this color.

Please change the color to your favorite color. Well then

Otsu py's HP: http://www.otupy.net/

Youtube： https://www.youtube.com/channel/UCaT7xpeq8n1G_HcJKKSOXMw

Twitter： https://twitter.com/otupython

Recommended Posts