This is the 21st day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Review and

The most used class in the Matplotlib module is Pyplot. Pyplot is located in the script layer of the Matplotlib module. It provides beginners with a quick way to plot broken lines, bars, scatters, pie charts and other graphs

  • Matplotlib structure: describes the functions of the bottom three layers of the Matplotlib module

  • Matplotlib draws a line diagram: pyplot.plot() summarizes the related properties

  • Matplotlib draws a histogram: Pyplot.bar () for summary description

  • The matplotlib Animation class draws animations: The Animation class provides classes for quickly drawing dynamic diagrams

The Matplotlib module provides users with quick drawing of dynamic graphs. In addition to animation class, PyPlot also provides methods for drawing dynamic graphs.

In this installment, we’ll learn the related method of drawing dynamic graphs using PyPlot, Let’s gp~

1. Pyplot display mode

After learning about the drawing method, we will call the pyplot.show() method and run the program to display the image.

There are usually two modes for drawing graphs in matplotlib mode:

  • Block: block mode

    • Blocking mode does not display a new FIG window after opening one
    • All of the plotting methods provided by Pyplot are direct plots, requiring a call to Pyplot.show () to display the plot
    • In Python scripts, matplotlib is blocked by default
    • Pyplot.show () is True in blocking cases
  • Interactive: indicates the interactive mode

    • The newly created image is displayed immediately
    • The data on the chart is automatically redrawn based on the changes
    • When using interactive mode, you need to ensure that the event loop is running with the response number
    • On the Python command line, matplotlib defaults to interactive mode
    • Pyplot. show is False in interactive mode

  • Pyplot provides a way to plot giFs

    methods role
    pyplot.ion() Turn on interactive mode
    pyplot.ioff() Turn off interaction mode
    pyplot.clf() Clears the current canvas Figure object
    pyplot.cla() Clears the current Axes object
    pyplot.pause() Pause function

2. Pyplot plot steps

In the matplotlib module, we can use interactive mode to combine with the GUI main loop to draw dynamic graphics. The main steps are as follows:

  • Import the matplotlib.pyplot module to draw graphs
import matplotlib.pyplot as plt
Copy the code
  • Call the Pyplot.ion () method to turn on the interactive mode
plt.ion()
Copy the code
  • Prepare x and y data using numpy.linspace(), numpy.arange(), etc
x = np.linspace(0,np.pi*i+1.1000)
y = np.cos(x)
Copy the code
  • Call pyplot.xlim(),pyplot.ylim() to set the x and y coordinate ranges
plt.xlim(-0.2.20.4)
plt.ylim(-1.2.1.2)
Copy the code
  • Calling PyPlot provides methods to plot broken lines, bars, and so on
plt.plot(x,y,color="pink")
Copy the code
  • Use the for loop to contain the x,y data created above, the x,y coordinates, and the drawing method

  • Use the pyplot.cla() method to clear a Axes object before the start of the for loop

plt.cla()
Copy the code
  • Gonn is paused by calling Pyplot.pause () at the end of the for loop
plt.pause(0.1)
Copy the code
  • When the for loop ends, you need to call Pyplot.ioff () to turn off the interactive mode and have Pyplot.show to show the image
plt.ioff()

plt.show()
Copy the code

3. Test the cat

We will learn how to plot a dynamic plot using pyplot, so let’s put it into practice

  • Call numpy.arange() to prepare x and y data
  • Call Pyplot.Scatter () to draw the scatter plot
  • Using the for loop includes the above steps by calling Pyplot.ion () at the beginning of the for loop to turn on the interaction mode
  • After the for loop ends, pyplot.ioff() is called to turn off the interaction mode
  • Finally, pyplot.show() is called to show the image screen
Def scatter_plot(): # open interactive plt.ion() for index in range(50): Grid (True) point_count = 5 x_index = np.random. Random (point_count) y_index = np.random.random(point_count) color_list = np.random.random(point_count) scale_list = np.random.random(point_count) * Scatter (x_index, y_index, s=scale_list, C =color_list, marker="^") plt.pause(0.2) plt.ioff() plt.show()Copy the code

conclusion

In this issue, we will learn the method of matplotlib module Pyplot to draw dynamic graphs. In drawing graphs, we will use the for loop to generate data every time. Finally, when using Pyplot.show () to display graphs, we need to turn off the interactive mode first, otherwise the graphs will flash by and flash back.

That’s the content of this episode. Please give us your thumbs up and comments. See you next time