在 Matplotlib 绘图中自定义文本样式

PythonPythonBeginner
立即练习

This tutorial is from open-source community. Access the source code

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在本实验中,我们将学习如何使用字典来控制Matplotlib绘图中文字和标签的样式。通过创建一个选项字典,我们可以在多个文本对象和标签之间共享参数。这将使我们能够轻松地自定义绘图中文字的字体族、颜色、粗细和大小。

虚拟机使用提示

虚拟机启动完成后,点击左上角切换到“笔记本”标签页,以访问Jupyter Notebook进行练习。

有时,你可能需要等待几秒钟让Jupyter Notebook完成加载。由于Jupyter Notebook的限制,操作验证无法自动化。

如果你在学习过程中遇到问题,随时向Labby提问。课程结束后提供反馈,我们会及时为你解决问题。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/DataScienceandMachineLearningGroup(["Data Science and Machine Learning"]) matplotlib(("Matplotlib")) -.-> matplotlib/BasicConceptsGroup(["Basic Concepts"]) matplotlib(("Matplotlib")) -.-> matplotlib/PlottingDataGroup(["Plotting Data"]) matplotlib(("Matplotlib")) -.-> matplotlib/PlotCustomizationGroup(["Plot Customization"]) python(("Python")) -.-> python/ModulesandPackagesGroup(["Modules and Packages"]) python(("Python")) -.-> python/DataStructuresGroup(["Data Structures"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("Importing Matplotlib") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("Understanding Figures and Axes") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("Line Plots") python/DataStructuresGroup -.-> python/tuples("Tuples") python/DataStructuresGroup -.-> python/dictionaries("Dictionaries") matplotlib/PlotCustomizationGroup -.-> matplotlib/titles_labels("Adding Titles and Labels") python/ModulesandPackagesGroup -.-> python/importing_modules("Importing Modules") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("Numerical Computing") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("Data Visualization") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48983{{"在 Matplotlib 绘图中自定义文本样式"}} matplotlib/figures_axes -.-> lab-48983{{"在 Matplotlib 绘图中自定义文本样式"}} matplotlib/line_plots -.-> lab-48983{{"在 Matplotlib 绘图中自定义文本样式"}} python/tuples -.-> lab-48983{{"在 Matplotlib 绘图中自定义文本样式"}} python/dictionaries -.-> lab-48983{{"在 Matplotlib 绘图中自定义文本样式"}} matplotlib/titles_labels -.-> lab-48983{{"在 Matplotlib 绘图中自定义文本样式"}} python/importing_modules -.-> lab-48983{{"在 Matplotlib 绘图中自定义文本样式"}} python/numerical_computing -.-> lab-48983{{"在 Matplotlib 绘图中自定义文本样式"}} python/data_visualization -.-> lab-48983{{"在 Matplotlib 绘图中自定义文本样式"}} end

导入必要的库

我们将从导入本教程所需的库开始。我们将使用Matplotlib和NumPy。

import matplotlib.pyplot as plt
import numpy as np

定义字体字典

接下来,我们将定义一个字体字典,其中将包含我们文本和标签的样式选项。在这个例子中,我们将字体族设置为“serif”,颜色设置为“darkred”,粗细设置为“normal”,大小设置为16。

font = {'family': 'serif',
        'color':  'darkred',
        'weight': 'normal',
        'size': 16,
        }

创建绘图

现在,我们可以创建我们的绘图了。我们将使用NumPy生成一些数据,并绘制一条阻尼指数衰减曲线。

x = np.linspace(0.0, 5.0, 100)
y = np.cos(2*np.pi*x) * np.exp(-x)

plt.plot(x, y, 'k')

自定义标题

我们可以使用之前定义的字体字典来自定义绘图的标题。我们将把title()函数的fontdict参数设置为我们的字体字典。

plt.title('Damped Exponential Decay', fontdict=font)

向绘图添加文本

我们可以使用text()函数向绘图添加文本。在这个例子中,我们将使用字体字典来定制样式,向绘图添加一个LaTeX表达式。

plt.text(2, 0.65, r'$\cos(2 \pi t) \exp(-t)$', fontdict=font)

自定义坐标轴标签

我们也可以使用字体字典来自定义绘图的坐标轴标签。我们将把xlabel()和ylabel()函数的fontdict参数设置为我们的字体字典。

plt.xlabel('Time (s)', fontdict=font)
plt.ylabel('Voltage (mV)', fontdict=font)

调整间距

最后,我们可以调整绘图的间距,以防止ylabel被裁剪。我们将使用subplots_adjust()函数来调整左边距。

plt.subplots_adjust(left=0.15)

总结

在本实验中,我们学习了如何使用字典来控制Matplotlib绘图中文字和标签的样式。通过创建一个字体字典,我们可以轻松地自定义绘图中文字的字体族、颜色、粗细和大小。我们使用这种技术来定制绘图的标题、文字和坐标轴标签,并调整了间距以防止ylabel被裁剪。