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

preface

We are learning about the matplotlib.pyplot() method in the past, so far we have been able to draw regular charts such as line charts, bar charts, and scatter plots.

  • Overview of matplotlib module: This section summarizes the common methods of matplotlib module

  • Basic principle of Matplotlib module: Learn how the script layer, art layer and back-end layer of Matplotlib module work

  • Matplotlib draws line chart: Summarizes related attributes of line chart

  • Matplotlib Histogram drawing: Summarize the attributes related to the histogram

  • Matplotlib Histogram drawing: Histogram related properties are summarized

  • Matplotlib Draws a scatter plot: Summarizes the properties of the scatter plot

In Matplotlib. pyplot, it is possible to draw regular charts such as broken lines, bars and scatterpoints, as well as contour charts which are commonly used in geographical flat display

In this installment, we will learn matplotlib’s properties for plotting contour plots in detail, let’s go~

1. Outline of contour map

  • What is a contour map?

    • A contour map, also known as a horizontal map, is a chart showing a 3D image in 2D form
    • Contour map, also known as contour map, connects points with the same surface height into a circular line to show the plane curve
    • Contour map is also called z-slice map. Dependent variable Z changes with independent variable X and Y
    • Contour maps can be divided into leading curve, counting curve, intermediate curve and auxiliary curve
  • Common contour diagram scenarios

    • Contour maps are often used to show the topography of a place
    • Contour maps can also be used to calculate local mountain height
    • Contour map is often used in geological and geographical survey
    • Contour maps can also be used to draw mathematical formulas such as circles and ellipses
  • Step of drawing contour map

    1. Import the matplotlib.pyplot module
    2. To prepare data, use numpy/ PANDAS
    3. Call Pyplo.contour () or Pyplo.Contourf () to draw a contour line
  • The case shows

    Many trigonometric functions, exponential functions and other formulas learned in high school are needed to draw contour maps. In this case, contour line method is used to summarize circles

    • Case data preparation

      • Np.arrage () prepares a series of consecutive data
      • Np.meshgrid () converts the data to a matrix
      import numpy as np
      Define a contiguous set of data
      
      x_value = np.arange(-5.5.0.1)
      y_value = np.arange(-5.5.0.1)
      
      # Convert to matrix data
      x,y = np.meshgrid(x_value,y_value)
      Copy the code
    • Draw contour lines

      
      import matplotlib.pyplot as plt
      
      plt.contour(x,y,z)
      
      plt.title("Display Contour")
      plt.xlabel("x(m)")
      plt.ylabel("y(m)")
      
      plt.show()
      
      plt.show()
      Copy the code

2. Contour map attributes

  • Sets the contour color

    • Keyword: colors
    • Value range:
      • Words for colors: e.g. Red, “red”
      • Short for color words such as red “r”, yellow “Y”
      • RGB format: hexadecimal format, such as “# 88C999 “; (r,g,b) tuple form
      • You can also pass in a list of colors
  • Set contour transparency:

    • Key words: alpha
    • The default is 1
    • The value ranges from 0 to 1
  • Sets the contour color level

    • Keyword: CMAP
    • The colors and Cmap keywords cannot be supplied together
    • The value can be: Indicates the color of registration
      • For example: “color table _r”
      • Commonly used are: ‘Accent’, ‘Accent_r’, ‘Blues’, ‘Blues_r’, ‘BrBG’, ‘BrBG_r’, ‘BuGn’, ‘BuGn_r’, ‘BuPu’, ‘BuPu_r’, ‘CMRmap’, ‘CMRmap_r’, ‘Dark2’, ‘Dark2_r’, ‘GnBu’, ‘GnBu_r’, ‘Greens’
  • Set the contour width

    • Keyword: linewidths
    • The default contour width is 1.5
    • Values can be a float or a list
  • Set the contour style

    • Keywords: Linestyles
    • The default value is solid
    • The value is optional: {None, ‘solid’, ‘dashed’, ‘dashdot’, ‘dotted’}
    • If linestyles is None and the line is monochrome, the negative outline of the line is set to the consistent mode
  • Let’s add some properties to the contour map from the previous section

    • The line is red, and the width of the line increases gradually. The pattern of the line is consistent, and the transparency is set to 0.5

      Contour (x,y,z,colors="r", linestyles="dashed", linewidths=np.arange(0.5,4,0.5),alpha=0.5)Copy the code

    • Passing in the colors list

      plt.contour(x,y,z,
      colors=('r'.'green'.'blue', (1.1.0),"#afeeee"."0.5"),
      linewidths=np.arange(0.5.4.0.5))
      Copy the code

    • For the contour map, set cMAP to red

      z = np.exp(-x**2-y**2)
      z1 = np.exp(-(x-1) * *2-(y-1) * *2)
      Z = (z-z1)*2
      
      plt.contour(x,y,Z,
      cmap='afmhot_r',
      linewidths=np.arange(0.5.4.0.5))
      Copy the code

3. Display outline labels

When we view the contour map, the contour label will help us to better view the chart. To add contour labels, we need to use Clabe

  • The pyplot.contour() method, which returns a QuadContourset

  • QuadContourset contains level list data

  • Use Pyplot.clabel () to accept level list data annotated on the contour line

    X_value = np.arange(-3,3,0.025) y_value = np.arange(-3,3,0.025) x,y = np.meshgrid(x_value,y_value) z = (1 - x + y * * * * 2. 5) * np in exp (- 2 - y * * * * x 2) cs = PLT. The contour (x, y, z, cmap = "Blues_r linewidths. = np arange,0.5 (0.5, 4)) plt.clabel(cs,fontsize=9,inline=True)Copy the code

4. Fill in the color

Usually in contour plots, different areas are filled with different colors to help us understand the diagram better when we look at it

  • Use Pyplot.contourf () to compare the contours of the same region to fill in the color

    z = (1-x**2+y**5)*np.exp(-x**2-y**2)
    
    cs = plt.contour(x,y,z,10,colors="b",linewidths=0.5)
    
    plt.clabel(cs,fontsize=12,inline=True)
    
    plt.contourf(x,y,z,10,cmap="Blues_r",alpha=0.75)
    Copy the code

5. Add a color bar description

We can add a colorbar specification with the pyplot.colorbar() method

z = (x**2+y**5)*np.exp(-x**2-y**2)
z1 = np.exp(-(x-1) * *2-(y-1) * *2)
Z = (z-z1)*2

cs = plt.contour(x,y,Z,10,colors="black",linewidths=0.5)

plt.clabel(cs,fontsize=12,inline=True)

plt.contourf(x,y,Z,10,cmap="afmhot_r",alpha=0.5)

plt.colorbar(shrink=0.8)
Copy the code

conclusion

This issue, matplotlib.pyplot drawing contour line method contour and Contourf related attributes learning. When drawing a contour map, we need to have a little knowledge of trigonometric functions, exponential functions, sines and cosines, etc., to draw the desired graph

In the process of learning this section, the mathematics knowledge of high school is returned to the teacher, touch the head, how the hair dropped 😱

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