[Deep Learning] Python and NumPy Tutorial Series (15): Detailed explanation of Matplotlib: 2. 3d drawing type (1): Wireframe Plot

Table of contents

 I. Introduction

2. Experimental environment

3. Detailed explanation of Matplotlib

 1. 2d drawing type

2. 3D drawing type

0. Set Chinese font

1. Wireframe Plot


 I. Introduction

        Python is a high-level programming language created by Guido van Rossum in 1991. It is known for its concise, easy-to-read syntax, powerful functionality, and wide range of applications. Python has a rich standard library and third-party libraries that can be used to develop various types of applications, including web development, data analysis, artificial intelligence, scientific computing, automation scripts, etc.

        Python itself is a great general-purpose programming language and, with the help of some popular libraries (numpy, scipy, matplotlib), becomes a powerful environment for scientific computing. This series will introduce the Python programming language and methods of using Python for scientific computing, mainly including the following content:

  • Python: basic data types, containers (lists, tuples, sets, dictionaries), functions, classes
  • Numpy: array creation, array operations, array mathematics, broadcasting
  • Matplotlib: 2d drawing, 3d drawing, chart customization, multiple subplots and layout, chart customization, multiple subplots and layout
  • IPython: Creating notebooks, typical workflow

2. Experimental environment

matplotlib 3.5.3
numpy 1.21.6
python 3.7.16
  • Run the following command to check the Python version
 python --version 
  • Run the following code to check the Python, NumPy, and Matplotlib versions
import sys
import numpy as np
import matplotlib

print("Python 版本:", sys.version)
print("NumPy 版本:", np.__version__)
print("matplotlib 版本:", matplotlib.__version__)

3. Detailed explanation of Matplotlib

        Matplotlib is a Python library for creating data visualizations. It offers a wide range of plotting options, capable of producing various types of charts, graphs and visualizations. Here are some of the main features of Matplotlib:

  1. Drawing styles and types : Matplotlib supports various drawing styles and types, including line charts, scatter charts, bar charts, pie charts, contour charts, 3D charts, etc. You can choose the appropriate chart type to display and analyze data as needed. .

  2. Data visualization : Matplotlib makes it easy to convert data into visual representations. You can use Matplotlib to draw charts to show the distribution, trends, relationships, etc. of the data, which helps to better understand the data and discover potential patterns and associations.

  3. Chart customization : Matplotlib provides a wealth of chart customization options, which can adjust the chart's title, labels, coordinate axes, line style, color, etc. This enables you to create high-quality diagrams that suit your specific needs and tastes.

  4. Multiple subplots and layouts : Matplotlib allows you to create multiple subplots within a single image to present multiple related charts or data views simultaneously. You can customize the layout and arrangement of subgraphs to meet specific presentation needs.

  5. Export images : Matplotlib supports exporting images to a variety of formats, including PNG, JPEG, PDF, SVG, etc. This allows you to easily save the generated diagrams as files or embed them into documents, reports and presentations.

        Whether you are conducting scientific research, data analysis, report writing, or visual presentation, Matplotlib is a powerful and flexible tool. It is widely used in various fields, such as data science, machine learning, financial analysis, engineering visualization, etc.

 1. 2d drawing type

2d drawing (top): line chart, scatter chart, column chart, histogram, pie chart_QomolangmaH's blog-CSDN blog https://blog.csdn.net/m0_63834988/article/details/132872575?spm=1001.2014 icon-default.png?t=N7T8. 3001.5501

2d drawing (Part 2): box plot, heat map, area chart, contour chart, polar coordinate chart_QomolangmaH's blog-CSDN blog icon-default.png?t=N7T8https://blog.csdn.net/m0_63834988/article/details/132890656?spm =1001.2014.3001.5501

2. 3D drawing type

0. Set Chinese font

import matplotlib

matplotlib.rcParams['font.family'] = 'Microsoft YaHei'  # 设置为微软雅黑字体
matplotlib.rcParams['font.sans-serif'] = ['SimHei']     # 设置中文字体为黑体

        If this setting is not made, an error message of missing font will be reported.

1. Wireframe Plot

        Used to visualize three-dimensional data by drawing lines connecting data points to show the distribution and shape of the data.

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

# 生成数据
x = np.linspace(-5, 5, 50)  # x轴坐标
y = np.linspace(-5, 5, 50)  # y轴坐标
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))  # z轴坐标,这里使用sin函数生成一个曲面

# 创建一个三维坐标系
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# 绘制线框图
ax.plot_wireframe(X, Y, Z)

# 设置坐标轴标签
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

# 显示图形
plt.show()
  • The coordinate points of the x-axis and y-axis are generated
  • Use np.meshgrida function to generate grid point coordinates, and then calculate the corresponding z-axis coordinates based on the coordinates.
  • A three-dimensional coordinate system is created and ax.plot_wireframethe wireframe is drawn using a function. The function accepts three parameters: X, Y and Z, which represent the x, y and z coordinates of the grid points respectively.
  • We set the axis labels and use plt.show()the display graph.

Guess you like

Origin blog.csdn.net/m0_63834988/article/details/132890293