Python uses matplotlib to fill in the code example of the specified area in the graph, pythonmatplotlib
The code in this article focuses on the usage of the fill_between () function in the Python extension library matplotlib. pyplot.
Import numpy as npimport matplotlib. pyplot as plt # generate simulation data x = np. arange (0.0, 4.0 * np. pi, 0.01) y = np. sin (x) # Draw the sine curve plt. plot (x, y) # Draw the reference horizontal line plt. plot (x. min (), x. max (), (0, 0) # Set the axis label plt. xlabel ('x') plt. ylabel ('y') # Fill in the plt of the specified area. fill_between (x, y, where = (2.3 <x) & (x <4.3) | (x> 10), facecolor = 'purple ') # The plt can be filled multiple times. fill_between (x, y, where = (7 <x) & (x <8), facecolor = 'green') plt. show ()
Demo effect:
Summary
The above is all the content about the sample code of using matplotlib to fill the specified area of the graph in Python. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!