Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to draw Contour Map with Python+matplotlib

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to draw a contour map with Python+matplotlib". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to draw a contour map with Python+matplotlib.

1. Outline of Contour Map what is a contour map?

A contour map, also known as a horizontal map, displays a chart of a 3D image in 2D form.

The contour map, also known as the contour map, connects the points with the same surface height into a loop and displays it on the plane curve.

The contour map is also known as the Z slice map, which varies with the change of the variable Z and the independent variable XPerry Y.

The contour map can be divided into head curve, counter curve, inter-curve and auxiliary curve.

Scenes commonly used in contour maps

Contour maps are often used to show the topography of a place.

The contour map can also be used to calculate the height of the local mountains.

Contour maps are often used in geological and geographical surveys.

The contour map can also be used to display mathematical formulas such as circle and oval.

To draw a contour map

Import matplotlib.pyplot Modul

To prepare the data, you can use numpy/pandas to organize the data

Call pyplot.contour () or pyplot.contourf () to draw contours

Case presentation

The contour map needs to be drawn with the help of many formulas such as trigonometric function and exponential function learned in high school. In this issue, we use the contour method to summarize the circle.

Case data preparation

Np.arrage () prepares a series of consecutive data

Np.meshgrid () converts data into a matrix

Import numpy as np# defines a contiguous set of data x_value = np.arange (- 5mem5magin0.1) y_value = np.arange (- 5pence5recover0.1) # converted into matrix data x _ journal y = np.meshgrid (x _ ray valuevalue _ y _ value)

Draw contours

Import matplotlib.pyplot as pltplt.contour (xmemyjinz) plt.title ("Display Contour") plt.xlabel ("x (m)") plt.ylabel ("y (m)") plt.show () plt.show ()

two。 The contour map property sets the contour color

Keyword: colors

Value range:

English words for color: such as red "red"

Abbreviations for color words such as red "r" and yellow "y"

RGB format: hexadecimal format such as "# 88c999"; (rMagnegMagneb) tuple form

You can also pass in a color list

Set the transparency of contours

Keyword: alpha

Default is 1

Value range: 0: 1

Set the contour color level

Keyword: cmap

Colors and cmap keywords cannot be provided at the same time

The value is: the registered color indicates

Form such as "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 is 1.5 for contour width.

Values can be float type or list

Set contours styl

Keyword: linestyles

Default value is: solid

Available values: {None, 'solid',' dashed', 'dashdot',' dotted'}

When linestyles is None and the line is monochrome, the line with negative outline will be set to dashed

Let's add some attributes to the contour map in the previous section

The line is red, the line width increases gradually, the line style is dashed, and the transparency is set to 0.5.

````pythonplt.contour (x _ rect _ y _ r _ r = "r", linestyles= "dashed", linewidths=np.arange (0.5 ~ 0.5), alpha=0.5) ```

Pass in the colors list

Plt.contour ("x afeeee", "0.5"), linewidths=np.arange (0.5))

Is a contour map and sets cmap to red.

Z = np.exp (- x**2-y**2) Z1 = np.exp (- (xMui 1) * * 2-(yMui 1) * * 2) Z = (z-z1) * 2 plt.contour (x meme Zphaijinymeme cmapparia afmhotothers rpm linewidthsplaynp.arange (0.5)

3. Show outline label

When we look at the contour map, the outline label will help us to look at the chart better. To add profile tags, we need to use clabe

Pyplot.contour () draws contours, which returns QuadContourset

QuadContourset contains level list data

Use pyplot.clabel () to accept level list data annotated on contours

X_value = np.arange, y_value = np.arange, y = np.meshgrid, z = (1-x**2+y**5) * np.exp (- x**2-y**2) cs = plt.contour, plt.clabel (cs,fontsize=9,inline=True)

4. Fill color

Usually in the contour map, different areas are filled with different colors to help us better understand when we look at the chart.

Use pyplot.contourf () to contrast the fill color with the outline of the area

Z = (1-x**2+y**5) * np.exp (- x**2-y**2) cs = plt.contour (x plt.contourf, linewidths=0.5) plt.clabel (cs,fontsize=12,inline=True) Blues_r = "Blues_r", alpha=0.75)

5. Add a color bar description

We can use the pyplot.colorbar () method to add a color bar description

Z = (x**2+y**5) * np.exp (- x**2-y**2) Z1 = np.exp (- (Xmuri 1) * * 2-(Ymuri 1) * * 2) Z = (z-z1) * 2 cs = plt.contour (x linewidths=0.5) plt.clabel (cs,fontsize=12,inline=True) plt.contourf

At this point, I believe you have a deeper understanding of "how to draw a contour map with Python+matplotlib". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report