current position:Home>Python Matplotlib drawing contour map
Python Matplotlib drawing contour map
2022-01-31 17:02:12 【Little cute in the circle of friends】
This is my participation 11 The fourth of the yuegengwen challenge 15 God , Check out the activity details :2021 One last more challenge
Preface
We were in the past matplotlib.pyplot() Method learning , By now we have been able to draw a line chart 、 Histogram 、 Scatter and other regular charts ( The contents of previous periods are as follows , You can easily view the contents of previous periods )
-
matplotlib The module overview : Yes matplotlib Summarize the common methods of the module
-
matplotlib Module bottom principle : Yes matplotlib Module script layer 、 The art layer and the back-end layer learn if they work
-
matplotlib Draw line chart : Summarize the properties of the polyline
-
matplotlib Draw a histogram : Summarize the related attributes of the histogram
-
matplotlib Draw histogram : Histogram related attributes are summarized
-
matplotlib Draw a scatter plot : Summarize the related attributes of scatter chart
stay matplotlib.pyplot In addition to drawing regular charts, such as broken lines 、 Columnar 、 Take it easy and wait , It can also draw the contour map commonly used in geographical plane display
In this issue , We will study in detail matplotlib Drawing contour map related attributes of learning ,let's go~
1. Contour map Overview
-
What is a contour map ?
- Contour map is also called horizontal map , adopt 2D Form show 3D Graph of images
- Contour map is also called contour line map , Connect the points with the same surface height into a loop line and display it on the plane curve
- Contour map is also called Z Slice , The dependent variable Z And independent variables X,Y Change and change
- The contour map can be divided into the first curve 、 Meter curve 、 Intermediate curve and auxiliary curve
-
Contour map common scenes
- Contour maps are often used to show the terrain of a place
- The contour map can also calculate the height of local mountains
- Contour maps are often used in geology 、 Drawn by geographical survey
- Contour maps can also be used to draw circles 、 Mathematical formulas such as ellipse show
-
To draw a contour map
- Import matplotlib.pyplot modular
- Prepare the data , have access to numpy/pandas Collating data
- call pyplot.contour() perhaps pyplot.contourf() Draw contours
-
The case shows
Contour map drawing needs the help of many trigonometric functions learned in high school 、 Exponential function and other formulas , In this case, we use the contour method to summarize circles
-
Case data preparation
- np.arrage() Prepare a series of continuous data
- np.meshgrid() Convert data into a matrix
import numpy as np # Define a continuous 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 code
-
Draw contours
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 code
-
2. Contour map properties
-
Set the contour color
- keyword :colors
- Value range :
- English words for color : Like red "red"
- Abbreviations of words indicating color, such as : Red "r", yellow "y"
- RGB Format : Hexadecimal format, such as "#88c999";(r,g,b) Tuple form
- You can also pass in a color list
-
Set contour transparency :
- keyword :alpha
- The default is 1
- The value range is :0~1
-
Sets the contour color level
- keyword :cmap
- colors and cmap Two keywords cannot be provided at the same time
- The value is : The registered color indicates
- In the form of :" color table _r"
- Commonly used :'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'
-
Sets the contour width
- keyword :linewidths
- The default contour width is 1.5
- The value can be float Type or list
-
Set the contour style
- keyword :linestyles
- The default value is :solid
- The value is optional :{None, 'solid', 'dashed', 'dashdot', 'dotted'}
- linestyles by None When the line is monochrome , The lines with negative contour will be set to dashed
-
We added some attributes to the contour map in the previous section
-
The line is red , The line width gradually increases , The line style is dashed, Transparency set to 0.5
```python plt.contour(x,y,z,colors="r", linestyles="dashed", linewidths=np.arange(0.5,4,0.5),alpha=0.5) ``` Copy code
-
Pass in 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 code
-
Is a contour map , Set up cmap It's 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 code
-
3. Show outline labels
When we look at the contour map , The outline tab will help us better view the chart . Add outline labels , We need the help of clabe
-
pyplot.contour() Method of drawing contour lines , Returns the QuadContourset
-
QuadContourset contain level The list of data
-
Use pyplot.clabel() Accept level List data is marked on contour lines
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**2+y**5)*np.exp(-x**2-y**2) cs = plt.contour(x,y,z,cmap="Blues_r",linewidths=np.arange(0.5,4,0.5)) plt.clabel(cs,fontsize=9,inline=True) Copy code
4. Fill color
Usually in contour plots , Different areas are filled with different colors , Help us better understand when looking at the chart
-
Use pyplot.contourf() Contrast the contour of the same area for filling 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 code
5. Add color bar description
We can use pyplot.colorbar() Method to add a color bar description
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 code
summary
In this issue , Yes matplotlib.pyplot Method of drawing contour lines contour and contourf Learning related attributes . When drawing contour maps , We need to do trigonometric functions 、 Exponential function 、 Have a little knowledge of sine and cosine functions , To draw the chart you want
During the study of this section , High school math knowledge is returned to the teacher , Feel the head , Why did you lose your hair again
The above is the content of this issue , Welcome big guys to praise and comment , See you next time ~
copyright notice
author[Little cute in the circle of friends],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201311702106608.html
The sidebar is recommended
- Django (make an epidemic data report)
- Daily python, Part 8 - if statement
- Django model class 1
- The same Python code draws many different cherry trees. Which one do you like?
- Python code reading (Chapter 54): Fibonacci sequence
- Django model class 2
- Python crawler Basics
- Mapping 3D model surface distances using Python VTK
- How to implement encrypted message signature and verification in Python -- HMAC
- leetcode 1945. Sum of Digits of String After Convert(python)
guess what you like
-
leetcode 2062. Count Vowel Substrings of a String(python)
-
Analysis of Matplotlib module of Python visualization
-
Django permission management
-
Python integrated programming -- visual hot search list and new epidemic situation map
-
[Python data collection] scripy realizes picture download
-
Python interface automation test framework (basic part) -- loop statement of process control for & while
-
Daily python, Chapter 9, while loop
-
Van * Python | save the crawled data with docx and PDF
-
Five life saving Python tips
-
Django frequency control
Random recommended
- Python - convert Matplotlib image to numpy Array or PIL Image
- Python and Java crawl personal blog information and export it to excel
- Using class decorators in Python
- Untested Python code is not far from crashing
- Python efficient derivation (8)
- Python requests Library
- leetcode 2047. Number of Valid Words in a Sentence(python)
- leetcode 2027. Minimum Moves to Convert String(python)
- How IOS developers learn Python Programming 5 - data types 2
- leetcode 1971. Find if Path Exists in Graph(python)
- leetcode 1984. Minimum Difference Between Highest and Lowest of K Scores(python)
- Python interface automation test framework (basic) -- basic syntax
- Detailed explanation of Python derivation
- Python reptile lesson 2-9 Chinese monster database. It is found that there is a classification of color (he) desire (Xie) monsters during operation
- A brief note on the method of creating Python virtual environment in Intranet Environment
- [worth collecting] for Python beginners, sort out the common errors of beginners + Python Mini applet! (code attached)
- [Python souvenir book] two people in one room have three meals and four seasons: 'how many years is it only XX years away from a hundred years of good marriage' ~?? Just come in and have a look.
- The unknown side of Python functions
- Python based interface automation test project, complete actual project, with source code sharing
- A python artifact handles automatic chart color matching
- Python crawls the map of Gaode and the weather conditions of each city
- leetcode 1275. Find Winner on a Tic Tac Toe Game(python)
- leetcode 2016. Maximum Difference Between Increasing Elements(python)
- Run through Python date and time processing (Part 2)
- Application of urllib package in Python
- Django API Version (II)
- Python utility module playsound
- Database addition, deletion, modification and query of Python Sqlalchemy basic operation
- Tiobe November programming language ranking: Python surpasses C language to become the first! PHP is about to fall out of the top ten?
- Learn how to use opencv and python to realize face recognition!
- Using OpenCV and python to identify credit card numbers
- Principle of Python Apriori algorithm (11)
- Python AI steals your voice in 5 seconds
- A glance at Python's file processing (Part 1)
- Python cloud cat
- Python crawler actual combat, pyecharts module, python data analysis tells you which goods are popular on free fish~
- Using pandas to implement SQL group_ concat
- How IOS developers learn Python Programming 8 - set type 3
- windows10+apache2. 4 + Django deployment
- Django parser