Matplotlib 简明教程

Matplotlib VS Seaborn

Matplotlib 和 Seaborn 都是用于数据可视化的强大的 Python 库,但是它们有自己不同的优势,适用于不同的目的。

Matplotlib and Seaborn are both powerful Python libraries used for data visualization but they have different strengths that are suited for different purposes.

What is Matplotlib?

Matplotlib 是一个全面且广泛用于创建静态、交互和出版质量可视化的 Python 库。它提供了一种多功能的工具包,用于生成各种类型的图表和柱状图,使其成为数据科学家、研究人员、工程师和分析师必不可少的工具。以下是 matplotlib 库的功能。

Matplotlib is a comprehensive and widely used Python library for creating static, interactive and publication-quality visualizations. It provides a versatile toolkit for generating various types of plots and charts which makes it an essential tool for data scientists, researchers, engineers and analysts. The following are the features of the matplotlib library.

Core Library

Matplotlib 是 Python 中用于绘图的基础库。它允许用户通过允许用户创建从基本到高度定制的各种图表,对可视化进行低级控制。

Matplotlib is the foundational library for plotting in Python. It provides low-level control over visualizations by allowing users to create a wide variety of plots from basic to highly customize.

Customization

它提供了广泛的自定义选项,允许用户控制图表的每个方面。这种级别的控制有时会导致更多用于创建复杂图表的代码。

It offers extensive customization options by allowing users to control every aspect of a plot. This level of control can sometimes result in more code for creating complex plots.

Basic Plotting

虽然对于创建某些复杂图表非常灵活,但与 Seaborn 等专门库相比,可能需要更多的精力和代码。

While it’s highly flexible for creating certain complex plots might require more effort and code compared to specialized libraries like Seaborn.

Simple plot by matplotlib

下面是使用 matplotlib 库 pyplot 模块创建的简单折线图。

The following is the simple line plot created by using the matplotlib lbrary pyplot module.

Example

import matplotlib.pyplot as plt
# Creating a plot
plt.plot([1, 2, 3, 4], [10, 20, 25, 30])

# Customizing axis limits and labels
plt.xlim(0, 5)
plt.ylim(0, 35)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
simpleplot matplotlib

What is Seaborn?

Seaborn 是一个 Python 数据可视化库,它作为 Matplotlib 的一个抽象层运行。它被设计用来创建视觉上吸引人和内容丰富的统计图形,简化了从数据生成复杂可视化的过程。以下是 seaborn 库的关键特性。

Seaborn is a Python data visualization library that operates as an abstraction layer over Matplotlib. It’s designed to create visually appealing and informative statistical graphics, simplifying the process of generating complex visualizations from data. The following are the key features of the seaborn library.

Statistical Data Visualization

Seaborn 构建在 Matplotlib 的基础上,特别适合统计数据可视化。它通过提供高级抽象来简化创建复杂图表的流程。

Seaborn is built on top of Matplotlib and is particularly well-suited for statistical data visualization. It simplifies the process of creating complex plots by providing high-level abstractions.

Default Aesthetics

Seaborn 带有吸引人的默认样式和颜色面板,使图表在几乎不费力的情况下,在美学上赏心悦目。

Seaborn comes with attractive default styles and color palettes that make plots aesthetically pleasing with minimal effort.

Specialized Plots

它专门用于某些类型的图表,例如小提琴图、箱线图、配对图等,这些图表在 Seaborn 中比在 Matplotlib 中更容易创建。

It specializes in certain types of plots like violin plots, box plots, pair plots and more which are easier to create in Seaborn compared to Matplotlib.

Basic seaborn plot

以下是基本的 seaborn 折线图。

The following is the basic seaborn line plot.

Example

import seaborn as sns
import matplotlib.pyplot as plt
# Sample data
x_values = [1, 2, 3, 4, 5]
y_values = [2, 4, 6, 8, 10]

# Creating a line plot using Seaborn
sns.lineplot(x=x_values, y=y_values)
plt.show()
seaborn

Matplotlib

Seaborn

Level of Abstraction

Matplotlib is more low-level and requires more code for customizations.

Seaborn abstracts some complexities by enabling easier creation of complex statistical plots.

Default Styles

Matplotlib doesn’t have better default styles and color palettes when compared to seaborn.

Seaborn has better default styles and color palettes by making its plots visually appealing without much customization.

Specialized Plots

Matplotlib require more effort to plot certain plots readily.

Seaborn offers certain types of plots that are not readily available or require more effort in Matplotlib.

When to use each library

We can use this library when we need fine-grained control over the appearance of our plots or when creating non-standard plots that may not be available in other libraries.

We can use this library when working with statistical data especially for quick exploration and visualization of distributions, relationships and categories within the data. Seaborn’s high-level abstractions and default styles make it convenient for this purpose.

这两个库各自都有价值,有时可以将它们一起使用,以便结合两者的优势,用于高级可视化任务。

Both libraries are valuable in their own way and sometimes they can be used together to combine the strengths of both for advanced visualization tasks.