I want to win if there is the most useless visualization grand prix in the world ・ Learn visualization by evolving the OP function

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

ʻI want to evolve the OP function !!`

Click here for the OP function created last time

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()
スクリーンショット 2020-07-12 19.51.46.png

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.

Click here for the improved OP function

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()

タイトルなし.gif

It's a wonderful color! !! !!

Visualization mechanism

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 asTrue 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.

Finally

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

Author information

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

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

Twitter: https://twitter.com/otupython

Recommended Posts

I want to win if there is the most useless visualization grand prix in the world ・ Learn visualization by evolving the OP function
I want to initialize if the value is empty (python)
I made a function to check if the webhook is received in Lambda for the time being
I want to know the population of each country in the world.
I want to receive the configuration file and check if the JSON file generated by jinja2 is a valid JSON
I want to change the color by clicking the scatter point in matplotlib
[Python] I want to know the variables in the function when an error occurs!
I want to get the file name, line number, and function name in Python 3.4
I want to use the activation function Mish
I want to display the progress in Python!
I tried to learn the sin function with chainer
If there were no DI containers in the world.
I want to write in Python! (3) Utilize the mock
I want to say that there is data preprocessing ~
I want to use the R dataset in python
If you want to put an argument in the closure function and execute it later