Color the integration interval with matplotlib.pyplot

The function I used the other day when I wanted to color the area obtained by integrating the graph was convenient, so I will make a note of it.

matplotlib.pyplot.fill_between#

matplotlib.pyplot.fill_between(x, y1, y2=0, where=None, interpolate=False, step=None, \*, data=None, **kwargs)

This is the function I used this time. This time I just wanted to fill the bottom of the graph so I'm using

matplotlib.pyplot.fill_between(x, y1, color='color',alpha=(float))

Only parameters. Like the basic plot, the first two contain x-axis and y-axis data And from other parameters that can be specified with ** kwargs, color ='color' and alpha ='transparency' are specified.

Implementation#

I implemented it and saw it

ex_fill_between.py


import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-10,10,100)
y = -0.1*(x**2)+10

plt.plot(x,y,'blue')
plt.fill_between(x,y,color='blue',alpha=0.1)
plt.show()


result スクリーンショット 2020-09-17 22.15.05.png

It is like this. If you want to fill the area between the two curves, you can enter the value of y2.

ex_fill_between2.py


import numpy as np
import matplotlib.pyplot as plt
           
x = np.linspace(-10,10,100)
y = -0.1*(x**2)+10
y2 = 0.05*(x**2)+1
plt.plot(x,y,'blue')
plt.plot(x,y2,'red')
plt.fill_between(x,y,y2,where = y>y2,color='blue',alpha=0.1)
plt.show()
~              

result スクリーンショット 2020-09-17 22.28.59.png

It will be. I'm using where here, but it seems to select the place to adapt and specify it with bool. As a result, only the places surrounded by the red and blue lines (blue line> red line) are adapted to the places.

Digression --- If you want to paint a shape separately, it seems better to use the fill function.

ex_fill.py


import matplotlib.pyplot as plt
  
x = [-1,0,1,0]
y = [0,-1,0,1]

plt.fill(x,y,color='blue',alpha=0.1)
plt.show()

result スクリーンショット 2020-09-17 22.36.28.png


At the end

Perhaps this level is common sense, but I personally found it convenient. I think it's important to output small things steadily, so I'd like to write something like this as well.

[reference]##

http://cranethree.hatenablog.com/entry/2015/07/25/204608 Illustration of the area with matplotlib https://matplotlib.org/api/_as_gen/matplotlib.pyplot.fill_between.html matplotlib official https://sabopy.com/py/matplotlib-28/ [How to use matplotlib] 28. Filled graph below the line graph

Recommended Posts

Color the integration interval with matplotlib.pyplot
Set the xticklabels color individually with matplotlib
Fill the background with a single color with OpenCV2 + Python
Specify the project name of docker-compose with Docker integration of Pycharm
Read the VTK file and display the color map with jupyter.
Insert the debugger with nose
Kill the process with sudo kill -9
[Python] Adjusting the color bar
Guess the password with klee
gethostbyaddr () communicates with the outside
scraping the Nikkei 225 with playwright-python
Check the code with flake8
Calibrate the model with PyCaret
Call the API with python3.
Gradient color selection with matplotlib
Color extraction with Python + OpenCV solved the mystery of the green background
Create a color picker for the color wheel with Python + Qt (PySide)
When you want to adjust the axis scale interval with APLpy