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