Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do you get the current figure number in Python's Matplotlib?
In this article, we will learn how to get the current figure number in Python's Matplotlib. This is useful when working with multiple figures and you need to identify which figure is currently active.
Matplotlib is a comprehensive plotting library for Python that provides MATLAB-like functionality. When working with multiple plots, each figure gets assigned a unique number that you can retrieve programmatically.
Using plt.gcf().number
What is plt.gcf()?
The matplotlib.pyplot.gcf() function returns the current figure object. If no figure exists, it creates a new one automatically.
matplotlib.pyplot.gcf()
Example
Here's how to get the current figure number using plt.gcf().number ?
import matplotlib.pyplot as plt
import numpy as np
# Create some sample data
t = np.arange(0.1, 2.0, 0.1)
sineValues1 = np.sin(2 * np.pi * t)
sineValues2 = np.sin(4 * np.pi * t)
# Create first figure
plt.figure(1)
plt.plot(t, sineValues1)
plt.title('Figure 1: sin(2?t)')
# Create second figure
plt.figure(2)
plt.plot(t, sineValues2)
plt.title('Figure 2: sin(4?t)')
# Get current figure number
currentFigureNumber = plt.gcf().number
print("Current Figure Number:", currentFigureNumber)
Current Figure Number: 2
Working with Named Figures
You can also create figures with string names instead of numbers ?
import matplotlib.pyplot as plt
import numpy as np
# Create figures with string names
plt.figure('first_plot')
plt.plot([1, 2, 3], [1, 4, 9])
plt.title('First Plot')
plt.figure('second_plot')
plt.plot([1, 2, 3], [1, 8, 27])
plt.title('Second Plot')
# Get current figure number (still returns integer)
current_num = plt.gcf().number
print("Current Figure Number:", current_num)
print("Current Figure Label:", plt.gcf()._label)
Current Figure Number: 2 Current Figure Label: second_plot
Switching Between Figures
You can switch to a specific figure and check its number ?
import matplotlib.pyplot as plt
# Create multiple figures
fig1 = plt.figure(1)
fig2 = plt.figure(2)
fig3 = plt.figure(3)
# Switch back to figure 1
plt.figure(1)
print("After switching to figure 1:", plt.gcf().number)
# Switch to figure 3
plt.figure(3)
print("After switching to figure 3:", plt.gcf().number)
After switching to figure 1: 1 After switching to figure 3: 3
Key Points
-
plt.gcf().numberreturns the integer ID of the current active figure - Figure numbers start from 1 and increment automatically
- Even when using string names for figures,
.numberreturns the integer ID - Use
plt.figure(n)to switch between existing figures
Conclusion
Use plt.gcf().number to get the current figure number in Matplotlib. This is particularly useful when managing multiple plots and need to track which figure is currently active for plotting operations.
