图形大小单位

PythonPythonBeginner
立即练习

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

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

简介

Matplotlib 是 Python 中一个强大的数据可视化库。它用于为数据分析创建高质量的图表、图形和图像。在本实验中,我们将学习如何使用 Matplotlib 以不同单位调整图形大小。

虚拟机使用提示

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

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

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


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/ModulesandPackagesGroup(["Modules and Packages"]) python(("Python")) -.-> python/DataScienceandMachineLearningGroup(["Data Science and Machine Learning"]) python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/DataStructuresGroup(["Data Structures"]) matplotlib(("Matplotlib")) -.-> matplotlib/AdvancedTopicsGroup(["Advanced Topics"]) linux(("Linux")) -.-> linux/PackagesandSoftwaresGroup(["Packages and Softwares"]) matplotlib(("Matplotlib")) -.-> matplotlib/BasicConceptsGroup(["Basic Concepts"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("Importing Matplotlib") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("Understanding Figures and Axes") python/BasicConceptsGroup -.-> python/comments("Comments") python/DataStructuresGroup -.-> python/lists("Lists") python/DataStructuresGroup -.-> python/tuples("Tuples") python/ModulesandPackagesGroup -.-> python/importing_modules("Importing Modules") matplotlib/AdvancedTopicsGroup -.-> matplotlib/matplotlib_config("Customizing Matplotlib Configurations") linux/PackagesandSoftwaresGroup -.-> linux/pip("Python Package Installing") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("Data Visualization") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48731{{"图形大小单位"}} matplotlib/figures_axes -.-> lab-48731{{"图形大小单位"}} python/comments -.-> lab-48731{{"图形大小单位"}} python/lists -.-> lab-48731{{"图形大小单位"}} python/tuples -.-> lab-48731{{"图形大小单位"}} python/importing_modules -.-> lab-48731{{"图形大小单位"}} matplotlib/matplotlib_config -.-> lab-48731{{"图形大小单位"}} linux/pip -.-> lab-48731{{"图形大小单位"}} python/data_visualization -.-> lab-48731{{"图形大小单位"}} end

设置环境

在开始之前,我们需要通过安装 Matplotlib 来设置我们的环境。你可以在终端或命令提示符中使用 pip 进行安装。

!pip install matplotlib

我们还需要在代码中导入 Matplotlib。

import matplotlib.pyplot as plt

以英寸为单位的图形大小(默认)

Matplotlib 中默认的图形大小单位是英寸。我们可以使用 subplots 函数中的 figsize 参数来指定图形大小。以下代码展示了如何创建一个大小为 6 英寸 x 2 英寸的图形。

plt.subplots(figsize=(6, 2))
plt.show()

以厘米为单位的图形大小

我们也可以指定以厘米为单位的图形大小。要做到这一点,我们需要将基于厘米的数值转换为英寸。我们可以通过将厘米值乘以从厘米到英寸的转换因子(即 1/2.54)来实现。然后,我们可以将这个值用作 subplots 函数中的 figsize 参数。以下代码展示了如何创建一个大小为 15 厘米 x 5 厘米的图形。

cm = 1/2.54  ## 厘米换算为英寸
plt.subplots(figsize=(15*cm, 5*cm))
plt.show()

以像素为单位的图形大小

我们还可以指定以像素为单位的图形大小。要做到这一点,我们需要将像素值转换为英寸。我们可以通过用 1 除以每英寸点数(dpi)值来获得从像素到英寸的转换因子。然后,我们可以将这个值用作 subplots 函数中的 figsize 参数。以下代码展示了如何创建一个大小为 600 像素 x 200 像素的图形。

px = 1/plt.rcParams['figure.dpi']  ## 像素换算为英寸
plt.subplots(figsize=(600*px, 200*px))
plt.show()

快速交互式工作

对于快速交互式工作,像素通常是一个不错的单位大小。我们可以使用默认的 100 dpi 值将像素值转换为英寸。然后,我们可以将这个值用作 subplots 函数中的 figsize 参数。以下代码展示了如何使用像素值创建一个大小为 6 英寸 x 2 英寸的图形。

plt.subplots(figsize=(600/100, 200/100))
plt.show()

总结

在本实验中,我们学习了如何使用 Matplotlib 以不同单位调整图形大小。我们可以指定以英寸、厘米或像素为单位的图形大小。默认情况下,Matplotlib 中的图形大小单位是英寸。我们可以将基于厘米和像素的值转换为英寸,以指定这些单位下的图形大小。