Matplotlib 简明教程

Matplotlib Tutorial

What Is Matplotlib?

Matplotlib 是用于数据可视化的最流行的 Python 软件包之一。它是一个跨平台库,用于从数组中的数据生成二维图。它提供了一个面向对象 API,用于使用 Python GUI 工具包(如 PyQtWxPythonTkinter )将图表嵌入到应用程序中。它可在 Python 和 IPython 外壳、 Jupyter notebook 和 Web 应用程序服务器中使用。

Matplotlib is one of the most popular Python packages used for data visualization. It is a cross-platform library for making 2D plots from data in arrays. It provides an object-oriented API that helps in embedding plots in applications using Python GUI toolkits such as PyQt, WxPython, or Tkinter. It can be used in Python and IPython shells, Jupyter notebook and web application servers also.

Matplotlib 是一个专门设计用于执行有效数据可视化的 Python 库。它是 Python 中绘图库的基石,使初学者能够深入了解有吸引力的数据可视化的世界。Matplotlib 是一个开源 Python 库,提供各种数据可视化(如折线图、直方图、散点图、条形图、散点图、饼图和面积图等)。Python matplotlib 库的优点在于它的 Python 代码。其脚本具有结构,表示在大多数情况下生成可视化数据图只需几行代码。

Matplotlib is a Python library that is specifically designed to do effective data visualization. It’s a cornerstone of plotting libraries in Python which empowers beginners to dive into the world of attractive data visualization. Matplotlib is an open-source Python library that offers various data visualization (like Line plots, histograms, scatter plots, bar charts, Scatter plots, Pie Charts, and Area Plot etc). A beauty of the Python matplotlib library is its Python code. Its script is structured which denotes that a few lines of code are all that are required in most instances to generate a visual data plot.

Matplotlib and Pyplot

Matplotlib 是一个多功能工具包,允许在 Python 编程语言中创建静态、动画和交互式可视化。

Matplotlib is a versatile toolkit that allows for the creation of static, animated, and interactive visualizations in the Python programming language.

通常,matplotlib 覆盖两个 API:

Generally, matplotlib overlays two APIs:

  1. The pyplot API: to make plot using matplotlib.pyplot.

  2. Object-Oriented API: A group of objects assembled with greater flexibility than pyplot. It provides direct access to Matplotlib’s backend layers.

Matplotlib 简化了简单任务,并使复杂任务能够得到完成。以下是 matplotlib 的主要方面:

Matplotlib simplifies simple tasks and enables complex tasks to be accomplished. Following are the key aspects of matplotlib:

  1. Matplotlib offers to create quality plots.

  2. Matplotlib offers interactive figures and customizes their visual style that can be manipulated as per need.

  3. Matplotlib offers export to many file formats.

Online Editor

我们提供了一个 Online Python Compiler/Interpreter 。它可帮助您直接在浏览器中 EditExecute Python 代码。您还可以使用它执行 Matplotlib 程序。

We have provided an Online Python Compiler/Interpreter. Which helps you to Edit and Execute the Python code directly from your browser. You can also execute the Matplotlib programs using this.

尝试单击图标以运行以下 matplotlib 代码以显示基本折线图。

Try to click the icon to run the following matplotlib code to display a basic line plot.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2 * np.pi, 200)
y = np.sin(x)

fig, ax = plt.subplots(figsize=(7, 4))
ax.set_title('Sin Wave')
ax.plot(x, y)
plt.show()

Applications of Matplotlib

matplotlib 最常见的应用程序包括:

The most common applications of matplotlib include:

  1. Data Visualization: Many scientific researches, data analytics, and machine learning applications use Matplotlib to visualize data.

  2. Scientific Research: Matplotlib helps scientists visualize experimental data, simulation findings, and statistical analysis. It improves data comprehension and communication for researchers.

  3. Engineering: Matplotlib helps engineers to visualize sensor readings, simulation findings, and design parameters. It excels at graphing in mechanical, civil, aeronautical, and electrical engineering.

  4. Finance: Finance professionals use Matplotlib to visualize stock prices, market trends, portfolio performance, and risk assessments. It helps analysts and traders make decisions by visualizing complicated financial data in simple graphics.

  5. Geospatial Analysis: Matplotlib, Basemap, and Cartopy are used to visualize geographical data such as maps, satellite images, climate data, and GIS data. Users may generate interactive maps, plot geographical characteristics, and overlay data for spatial analysis.

  6. Biology and Bioinformatics: Matplotlib helps biologists and bioinformaticians visualize DNA sequences, protein structures, phylogenetic trees, and gene expression patterns. It helps researchers to visualize complicated biological processes.

  7. Education: Educational institutions use Matplotlib to teach data visualization, programming, and scientific computing. Its easy-to-use visualization interface makes it suited for high school and university students and teachers.

  8. Web Development: Flask, Django, and Plotly Dash can incorporate Matplotlib into online apps. It lets developers build dynamic, interactive visualizations for web pages and dashboards.

  9. Machine Learning: Machine learning projects visualize data distributions, model performance metrics, decision boundaries, and training progress with Matplotlib. It helps machine learning practitioners analyze algorithm behavior and troubleshoot model-building concerns.

  10. Presentation and Publication: Matplotlib creates high-quality figures for scientific research, reports, presentations, and posters. It offers many customization options to optimize the plot look for publishing and presentation.

Matplotlib 允许用户生成信息丰富且有吸引力的可视化,以进行分析、交流和决策制定。

Matplotlib lets users produce informative and attractive visualizations for analysis, communication, and decision-making.

Why To Learn Matplotlib?

Matplotlib 是一个用于在 Python 中创建静态、动态和交互式可视化的综合库。它已成为 Python 生态系统中最广泛使用的绘图库之一。Matplotlib 流行的一些原因如下:

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. It has become one of the most widely used plotting libraries in the Python ecosystem. Some of the reasons are as to make Matplotlib popular:

  1. Plotting Capabilities: Matplotlib provides extensive functionality for creating a variety of plots like line plots, scatter plots, bar charts, histograms, pie charts, 3D plots, etc.

  2. Quality Graphics: It allows its users to control every aspect of their plots, including colors, line styles, markers, fonts, and annotations. Integration with NumPy and Pandas: Matplotlib works with NumPy and Pandas to visualize arrays, data frames, and other data structures.

  3. Cross-Platform Compatibility: Matplotlib operates on Windows, macOS, and Linux, making it accessible to many people.

  4. Extensive Documentation and Tutorials: Beginners and experts may easily get started with Matplotlib thanks to its extensive documentation and online training.

Matplotlib 是一款强大而通用的 Python 工具包,用于可视化数据,这使其对于数据分析师、科学家、工程师和其他处理数据的人员不可或缺。

Matplotlib is a robust and versatile Python toolkit used for visualizing data which makes it indispensable for data analysts, scientists, engineers, and other professionals working with data.

Who Should Learn Matplotlib?

Matplotlib tutorial 已为想要了解 Matplotlib Python package 基础和进步的人员编写。在数据科学、工程、研究、农业科学、管理、统计学和其他相关领域中最广泛地使用,其中数据可视化主要需要使用图表和图形来查找数据见解以了解数据模式。它真正帮助企业进行战略决策制定。

This Matplotlib tutorial has been prepared for those who want to learn about the foundations and advances of the Matplotlib Python package. It is most widely used in the domains of data science, engineering, research, agriculture science, management, statistics, and other related fields where data visualization primarily requires finding data insights using charts and graphs to understand the data patterns. It really helps the companies in strategic decision-making.

Matplotlib tutorial 旨在为初学者和专业人士设计,涵盖 matplotlib 概念,包括安装 matplotlib 和制作不同绘图的过程。它提供了详细的描述、有价值的见解以及构建有吸引力的可视化的基本原理。无论您是踏入数据科学领域的学生还是一名专业人士,本教程都为使用 matplotlib 通过数据可视化来探索数据分析提供了坚实的基础,以展示数据。因此,本教程旨在解释 matplotlib 的不同功能以用于数据分析。

This Matplotlib tutorial is designed for beginners and professionals to cover matplotlib concepts, including the process of installing matplotlib and making different plots. It offers a detailed description, valuable insights, and the fundamental principles of constructing attractive visualizations. Whether you are a student embarking in the field of data science or a professional, this tutorial provides a strong foundation to explore data analysis using data visualization through Matplotlib to present the data. Hence, this tutorial aims to explain the different functions of Matplotlib for data analysis.

Prerequisites To Learn Matplotlib

您应该对计算机编程有基本的了解。对 Python 或任何一种编程语言的基本了解会更好。统计学和数学方面的基本知识有助于数据分析和解读。Matplotlib 提供了数据可视化功能。通过对上述内容有牢固的基础,您将能够充分利用 matplotlib 以实现数据可视化。

You should have a basic understanding of computer programming. A basic understanding of Python and any of the programming languages is a plus. Basic knowledge of statistics and mathematics is helpful for data analysis and interpretation. Matplotlib offers functions for data visualization. By having a strong foundation of above mentioned, you’ll be well-equipped to leverage the power of matplotlib for data visualization.

Frequently Asked Questions about Matplotlib

有一些关于 SQL 的常见问题 (FAQ),本部分会尝试简要回答这些问题。

There are some very Frequently Asked Questions(FAQ) about SQL, this section tries to answer them briefly.