Instagram
youtube
Facebook
Twitter

When to use object-oriented style?

Description:
A Python program demonstrating when and why to use Matplotlib's object-oriented (OO) style for creating cleaner and scalable visualizations.

Code Explanation:
fig, ax = plt.subplots(): Creates a figure and axes object explicitly.
ax.plot(...): Plots the data on the specified Axes.
ax.set_title(...): Sets the title using the OO method.
plt.show(): Displays the plot.

Program:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [2, 4, 1])
ax.set_title("Object-Oriented Plot")
plt.show()

Output: