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

Review and

Matplotlib is a very powerful module, including the class of drawing, animation, graphics processing class, etc. We have already learned the common plot line, column drawing method, after the last section, we have also learned the physics of more than a lot of measuring field plot drawing methods.

  • Pyplot.quiver () plots quantity field: matplotlib plots quantity field

  • Pyplot.ion () Interactive plot dynamic graph: Matplotlib plots dynamic graph

  • Animation class draws dynamic diagrams: Matplotlib Animation class

  • Image class for image processing: Matplotlib image processing

In matplotlib module, quantity field graph is usually used in physics, while box graph is more used in statistics.

In this issue, we will learn matplotlib. Pyplot. Boxplot method () related properties of learning, let ‘s go ~

1. Overview of box diagram

  • What is a box diagram?

    • Box diagrams, which look like boxes, are also called box and beard diagrams, box diagrams, or box diagrams.
    • A boxplot is a statistical diagram used to show the dispersion of a group of data
    • Box plots can display the maximum, minimum, median, and upper and lower quartiles of a set of data

  • Application scenarios of the box diagram

    • Box charts are often used in quality management because they show the dispersion of a group of data
    • Box diagram is conducive to data cleaning, can quickly know the data separately
    • The box chart helps to analyze the bias of data such as the income level of employees of a company
  • Use the box diagram method

    import matplotlib.pyplot as plt 
    plt.boxplot(x)
    Copy the code

2. Box diagram properties

  • Set the box bump

    • The key word: notch
    • The default value is False, showing non-convex
  • Set the box position

    • Vert
    • The default value is True
    • When vert is set to True, draw the vertical box
    • When vert is set to False, draw the horizontal box
  • Set box color filling

    • Key word: patch_artist
    • The default location is False
    • When patch_artist is False, it is Line2D artist
    • When patch_artist is True, patchARTIST can be filled with color
  • Set the box mean style

    • Key words: meanline
    • The default value is False
    • When the meanline is False, the meanline is not displayed
    • When meanLine is True and showmeans=True mean lines are displayed as dashed lines
  • Set the box border display

    • Set the keyword showcaps on the end of the box
    • Showbox Specifies the keyword for displaying boxes
    • The default value is True

3. Step of drawing box diagram

  • Import matplotlib. Pyplot class
import matplotlib.pyplot as plt
Copy the code
  • Prepare x array vector sequences using arange(),random() and other methods in numpy library
x = np.arange(10.20.5)
Copy the code
  • Call the pyplot.boxplot() method to draw a boxplot showing the median (red) and mean (dashed) lines
plt.boxplot(x,meanline=True,showmeans=True,labels=["A"])
Copy the code
  • Finally, pyplot.show() is called to render and print out the box plot

4. Test the cat

Learning the box-related properties and drawing steps above, let’s draw a set of data box diagrams and fill them with different colors

  • Call numpy.random.randint() to prepare five groups of data

  • Define the box-style boxprops property in dictionary form

  • Define the boxed mean-style meanprops property in dictionary form

x = np.random.randint(10.100,size=(5.5))

box = {"linestyle":The '-'."linewidth":3."color":'blue'}

mean = {"marker":'o'.'markerfacecolor':'pink'.'markersize':12}

plt.boxplot(x,meanline=True,showmeans=True,labels=["A"."B"."C"."D"."E"],boxprops=box,meanprops=mean)

plt.show()
Copy the code

  • If the box is to be filled with color, you need to set the patch_artist property to True
b = plt.boxplot(x,meanline=True,showmeans=True,labels=["A"."B"."C"."D"."E"],boxprops=box,meanprops=mean,patch_artist=True)

for c in b["boxes"] :

    c.set(color="lightblue")
Copy the code

conclusion

In this installment, we’ll learn about the properties that matplotlib.pyplot provides for boxplot() to plot boxes. For box graphs, it is more convenient to quickly know the distribution of a group of data.

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