Matplotlib draws pictures (figures) so you can see your data as graphs. Understanding how it creates these pictures helps you make better charts.
0
0
How Matplotlib renders figures
Introduction
You want to show data trends with a line or bar chart.
You need to save a graph as an image file to share.
You want to customize how your chart looks before showing it.
You want to update a chart dynamically in a program.
You want to understand why your chart looks a certain way.
Syntax
Matplotlib
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.plot([1, 2, 3], [4, 5, 6]) plt.show()
fig is the whole picture (figure).
ax is the area where the data is drawn (axes).
Examples
This creates a figure and adds one plot area manually.
Matplotlib
import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111) ax.plot([1, 2, 3], [4, 5, 6]) plt.show()
This creates two stacked plots in one figure.
Matplotlib
import matplotlib.pyplot as plt fig, ax = plt.subplots(2, 1) ax[0].plot([1, 2, 3], [4, 5, 6]) ax[1].bar([1, 2, 3], [6, 5, 4]) plt.show()
Sample Program
This code creates a figure with one plot area, draws a line graph, adds labels and a title, then shows the figure window.
Matplotlib
import matplotlib.pyplot as plt # Create a figure and axes fig, ax = plt.subplots() # Plot some data ax.plot([0, 1, 2, 3], [10, 20, 25, 30], label='Line') # Add title and labels ax.set_title('Simple Line Plot') ax.set_xlabel('X axis') ax.set_ylabel('Y axis') # Show legend ax.legend() # Display the figure plt.show()
OutputSuccess
Important Notes
Matplotlib first creates a Figure object which is like a blank canvas.
Inside the figure, Axes objects are created where data is drawn.
When you call plt.show(), Matplotlib renders the figure on your screen.
Summary
Matplotlib draws figures by creating a Figure and one or more Axes inside it.
You add data to Axes, then call plt.show() to see the graph.
Understanding this helps you control how your charts look and behave.