Matplotlib 简明教程

Matplotlib - Styles

What is Style in Matplotlib?

在 Matplotlib 库中,样式是允许我们轻松更改绘图视觉外观的配置。通过更改颜色、线型、字体、网格线等方面,它们作为预定义的一组美学选择。这些样式有助于快速自定义绘图的外观和感觉,而无需每次手动调整各个元素。

In Matplotlib library styles are configurations that allow us to change the visual appearance of our plots easily. They act as predefined sets of aesthetic choices by altering aspects such as colors, line styles, fonts, gridlines and more. These styles help in quickly customizing the look and feel of our plots without manually adjusting individual elements each time.

我们可以尝试不同的样式,以找到最适合我们的数据或视觉偏好的样式。样式提供了一种快速有效的方法,可以增强我们绘图在 Matplotlib 库中的视觉呈现。

We can experiment with different styles to find the one that best suits our data or visual preferences. Styles provide a quick and efficient way to enhance the visual presentation of our plots in Matplotlib library.

Built-in Styles

Matplotlib 提供了各种内置样式,它们提供了不同的配色方案、线型、字体大小和其他视觉属性。

Matplotlib comes with a variety of built-in styles that offer different color schemes, line styles, font sizes and other visual properties.

示例包括 ggplot, seaborn, classic, dark_background 等。

Examples include ggplot, seaborn, classic, dark_background and more.

Changing Styles

Key Aspects of Matplotlib Styles

  1. Predefined Styles − Matplotlib library comes with various built-in styles that offer different aesthetics for our plots.

  2. Ease of Use − By applying a style we can instantly change the overall appearance of our plot to match different themes or visual preferences.

  3. Consistency − Styles ensure consistency across multiple plots or figures within the same style setting.

Using Styles

使用 matlplotlib 库中的可用样式涉及几个步骤。让我们逐一查看它们。

There are several steps involved in using the available styles in matlplotlib library. Let’s see them one by one.

Setting a Style

要设置所需的样式,我们必须使用 plt.style.use('style_name') 在创建绘图之前设置特定样式。

For setting the required style we have to use plt.style.use('style_name') to set a specific style before creating our plots.

例如,如果我们希望设置 ggplot 样式,我们必须使用以下代码。

For example if we want to set the ggplot style we have to use the below code.

import matplotlib.pyplot as plt
plt.style.use('ggplot')  # Setting the 'ggplot' style

Available Styles

我们可以使用 plt.style.available 查看可用样式的列表。

We can view the list of available styles using plt.style.available.

Example

import matplotlib.pyplot as plt
print(plt.style.available)  # Prints available styles
['Solarize_Light2', '_classic_test_patch', '_mpl-gallery', '_mpl-gallery-nogrid', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark', 'seaborn-dark-palette', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'tableau-colorblind10']

Applying Custom Styles

我们可以创建带有特定配置的自定义样式文件,然后使用 plt.style.use('path_to_custom_style_file') 应用它们。

We can create custom style files with specific configurations and then use plt.style.use('path_to_custom_style_file') to apply them.

Applying the seaborn-darkgrid style

在这个示例中,样式 'seaborn-darkgrid' 正在应用于绘图并改变其外观。

In this example the style 'seaborn-darkgrid' is applying to the plot altering its appearance.

import matplotlib.pyplot as plt
# Using a specific style
plt.style.use('seaborn-darkgrid')

# Creating a sample plot
plt.plot([1, 2, 3, 4], [10, 15, 25, 30])
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Sample Plot')
plt.show()
darkgrid

Applying ggplot style

在这个示例中,我们为我们的绘图使用了 ggplot 样式。

In this example we are using the ggplot style for our plot.

import matplotlib.pyplot as plt
# Using a specific style
plt.style.use('seaborn-white')

# Creating a sample plot
plt.plot([1, 2, 3, 4], [10, 15, 25, 30])
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Sample Plot')
plt.show()
white