Python 如何在Matplotlib中获取当前图形编号

Python 如何在Matplotlib中获取当前图形编号

在本文中,我们将介绍如何在Python的matplotlib中获取当前图形编号。

Matplotlib是一个Python库,是NumPy库的数值数学扩展。Pyplot是Matplotlib模块的基于状态的接口,提供类似于MATLAB的功能。Pyplot提供了折线图、等高线图、直方图、散点图、3D图和其他图形的绘制。

使用plt.gcf().number

什么是plt.gcf()

matplotlib.pyplot.gcf()函数主要用于获取当前的图形。如果没有可用的当前图形,则使用figure()函数生成一个图形。

matplotlib.pyplot.gcf()

步骤

以下是执行所需任务的算法/步骤:

  • 从matplotlib中导入pyplot
Pyplot is a Matplotlib module that offers a MATLAB-style interface.  
Matplotlib is intended to be as user-friendly as MATLAB, but with
the added benefit of being free and open-source. Each pyplot function
alters a figure in some way, such as creating a figure, creating a
plotting area in a figure, plotting some lines in a plotting area,
decorating the plot with labels, and so on. Pyplot supports the following
plot types: Line Plot, Histogram, Scatter, 3D Plot, Image, Contour, and Polar
  • 使用import关键字导入numpy

  • 使用arange()函数获取从0.1到2的值范围(NumPy的arange()是一个基于数值范围创建数组的函数。它创建一个具有均匀间隔数值的ndarray实例,并返回对它的引用)。

  • 获取上面创建的numpy数组的正弦值,并将其存储在一个变量中,然后将正弦值乘以2。

  • 获取上述创建的numpy数组的正弦值,并将其存储在一个变量中,然后将正弦值乘以4以显示区别。

  • 使用pyplot创建一个图,并将图的数量作为参数传递给figure()函数(函数plt.figure()用于创建一个图形对象。图形对象被视为整个图形。当我们想要更改图形的大小或在单个图形中添加多个Axes对象时,必须显式使用plt.figure())。

  • 使用plot()函数创建图(使用plot()函数在图表中绘制点(标记)。plot()函数默认绘制点与点之间的线条。该函数接受参数来指定图表点。第一个参数是x轴点的数组。第二个参数是包含y轴点的数组)。这里我们将arange的值作为x轴,将正弦值作为y轴。

  • 创建另一个具有不同正弦值的图。

  • 使用plt.gcf()函数的number属性获取当前图表的编号。

  • 打印当前图表的编号。

  • 使用show()函数显示图表(plt.show()启动一个事件循环,搜索当前活动的图形对象,并在一个或多个交互式窗口中显示您的图形或图形)。

以下程序返回Python中matplotlib的当前图表编号,使用 plt.gcf().number

# Importing pyplot from matplotlib as plt using the alias(as) keyword
from matplotlib import pyplot as plt
import numpy as np

# Taking sine values as numpy array
t = np.arange(0.1, 2.0)

# Sine curves
sineValues1 = np.sin(2*np.pi*t)
sineValues2 = np.sin(4*np.pi*t)

# Creating a numpy figure
plt.figure(1)

# Plotting the figure by passing sine curve values
plt.plot(t, sineValues1)

# Creating second numpy figure
plt.figure(2)

# Plotting the second figure by passing another sine curve values
plt.plot(t, sineValues2)

# Getting the current Figure number using gcf().number attribute
currentFigureNumber = plt.gcf().number

# Printing current figure number
print("Current Figure Number: ", currentFigureNumber)

# Showing the plots
plt.show()

# Importing pyplot from matplotlib as plt using the alias(as) keyword
from matplotlib import pyplot as plt
import numpy as np

# Taking sine values as numpy array
t = np.arange(0.1, 2.0)

# Sine curves
sineValues1 = np.sin(2*np.pi*t)
sineValues2 = np.sin(4*np.pi*t)

# Creating a numpy figure
plt.figure(1)

# Plotting the figure by passing sine curve values
plt.plot(t, sineValues1)

# Creating second numpy figure
plt.figure(2)

# Plotting the second figure by passing another sine curve values
plt.plot(t, sineValues2)

# Getting the current Figure number using gcf().number attribute
currentFigureNumber = plt.gcf().number

# Printing current figure number
print("Current Figure Number: ", currentFigureNumber)

# Showing the plots
plt.show()

输出

在执行时,上述程序将生成以下输出 –

Python 如何在Matplotlib中获取当前图形编号

因为我们最近创建了第二个图形,所以当前工作的图形是2.

注意

您也可以将图形制作为文本字符串而不仅仅是数字,例如 plt.figure (“first”)。在切换图形时,这可能比数字更容易记忆。不过,plt.gcf().number 返回整数 “1”,表示它是一个自动编号系统。

结论

在这篇文章中,我们学习了如何在matplotlib中获取当前图形的编号。我们还学习了matplotlib是什么,如何在绘图时创建图形,如何使用gcf()函数获取图形等等。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程