Matplotlib 简明教程
Matplotlib - Stylesheets
What are StyleSheets?
在 Matplotlib 库中,样式表是控制图表整体外观的一组预定义的美学配置。它们提供了一种便捷的方法来以最小的工作量更改图表的外观和风格。
In Matplotlib library stylesheets are predefined sets of aesthetic configurations that control the overall appearance of plots. They offer a convenient way to change the look and feel of our plots with minimal effort.
样式表包含针对图表各种元素的预定义设置,例如,颜色、线条样式、字体、网格样式等。Matplotlib 提供了一系列内建样式表,允许我们向图表快速应用不同的视觉主题。
A stylesheet consists of predefined settings for various elements of a plot such as colors, line styles, fonts, grid styles and much more. Matplotlib provides a collection of built-in stylesheets that allow us to quickly apply different visual themes to our plots.
在未设置任何特定样式时使用默认样式,但 Matplotlib 包含 gplot, seaborn, bmh, dark_background 等几种其他样式。这些样式表提供不同的配色方案、线条样式、字体设置和整体美学。
The default style is used when no specific style is set but Matplotlib includes several other styles like gplot, seaborn, bmh, dark_background and more. These stylesheets offer different color schemes, line styles, font settings and overall aesthetics.
Matplotlib 提供了各种内建样式表。以下是如何使用它们概述:
Matplotlib provides a variety of built-in stylesheets. Here’s an overview of how you can work with them −
Viewing Available Stylesheets
Matplotlib 提供了不同的样式表,它们改变图表整体外观并改变颜色、线样式、字体大小等元素。样式表提供了一种快速简便的方法来更改我们的可视化的美学。
Matplotlib provides different stylesheets that change the overall appearance of plots and altering elements such as colors, line styles, font sizes etc. Stylesheets offer a quick and easy way to change the aesthetics of our visualizations.
Syntax
我们可以使用以下语法检查可用的样式表。
We can check the available stylesheets using the below syntax.
plt.style.available
Example
在本例中,我们使用 plt.style.available 获取 Matplotlib 库中的所有可用样式表。
Here in this example we are using the plt.style.available to get all the available stylesheets in the Matplotlib library.
import matplotlib.pyplot as plt
# List available stylesheets
print("Available stylesheets:", plt.style.available)
Available stylesheets: [
'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']
Using Stylesheets
使用 Matplotlib 样式表非常简单。我们可以将特定样式应用到我们的图表。以下语法。
Absolutely to use the Matplotlib stylesheets is straightforward. We can apply a specific style to our plots. Here’s the syntax.
Syntax
plt.style.use('stylesheet_name')
其中,
Where,
-
plt.style.use() − This is used to use the defined stylesheets to the entire plot.
-
stylesheet_name − This is the name of the stylesheet that we want to apply.
Example
在本例中,我们在创建我们的图表之前通过使用 plt.style.use('ggplot') 采用 ‘ggplot’ 样式表。
In this example we are using the stylesheet ‘ggplot’ by employing the plt.style.use('ggplot') before creating our plot.
import matplotlib.pyplot as plt
# Using a specific stylesheet
plt.style.use('ggplot') # Example: Using the 'ggplot' style
x = [10,30,20,50]
y = [30,23,45,10]
plt.plot(x,y)
plt.title("Plot with ggplot style sheet")
plt.show()
Applying Stylesheet Temporarily
如果我们要将样式表临时应用到特定代码块而不影响其他图表,我们可以使用 plt.style.context('stylesheet_name') 。
If we want to apply a stylesheet temporarily to a specific block of code without affecting other plots we can use with plt.style.context('stylesheet_name').
此临时上下文仅将指定的样式应用到 with 语句下缩进的代码块中。
This temporary context applies the specified style only within the block of code indented under the with statement.
Example
在这个示例中,我们通过使用 matplotlib 库中提供的 plt.style.context() 函数将样式表设置为 seaborn-dark 。
In this example we are setting the stylesheet to seaborn-dark by using the plt.style.context() functin available in matplotlib library.
import matplotlib.pyplot as plt
x = [10,30,20,50]
y = [30,23,45,10]
with plt.style.context('seaborn-dark'):
# Code for a plot with 'seaborn-dark' style
plt.plot(x, y)
plt.title('Seaborn-Dark Style')
plt.show() # The 'seaborn-dark' style will only affect this plot
Creating Custom Stylesheets
我们还可以通过定义 .mplstyle 文件或使用指定样式参数的 Python 字典来创建我们自己的自定义样式表 −
We can also create our custom stylesheets by defining a .mplstyle file or using a Python dictionary specifying the style parameters −
Example
在此示例中,我们通过使用字典创建自定义样式表。
In this example we are creating the custom stylesheets by using the dictionary.
import matplotlib.pyplot as plt
# Define a custom style using a dictionary
custom_style = {
'lines.linewidth': 10,
'lines.color': 'red',
'axes.labelsize': 30,
# Add more style configurations as needed
}
# Use the custom style
plt.style.use(custom_style)
x = [10,30,20,50]
y = [30,23,45,10]
plt.plot(x,y)
plt.title("Plot with custom style sheet")
plt.show()
样式表提供了一种有效的途径来维持多幅图表的统一性,或轻松地使用各种可视化样式进行实验。我们可以选择最符合我们的喜好或我们的数据可视化特定要求的样式表。
Stylesheets offer an efficient way to maintain consistency across multiple plots or to experiment with various visual styles easily. We can select stylesheets that best suit our preferences or the specific requirements of our data visualization.