Introduction
We create figures and axes to draw charts and graphs. This helps us see data clearly.
We create figures and axes to draw charts and graphs. This helps us see data clearly.
import matplotlib.pyplot as plt fig, ax = plt.subplots(figsize=(width, height))
fig is the whole drawing area (the figure).
ax is the area where the data is drawn (the axes).
import matplotlib.pyplot as plt fig, ax = plt.subplots()
fig, ax = plt.subplots(figsize=(8, 4))
fig, axs = plt.subplots(2, 2)
This code creates a figure and axes, draws a line, adds labels, and shows the plot.
import matplotlib.pyplot as plt # Create a figure and one axes fig, ax = plt.subplots(figsize=(6, 3)) # Plot a simple line ax.plot([1, 2, 3], [4, 5, 6]) # Add title and labels ax.set_title('Simple Line Plot') ax.set_xlabel('X axis') ax.set_ylabel('Y axis') # Show the plot plt.show()
You can create multiple axes to compare different data side by side.
Changing figsize changes the size of the whole figure.
Always call plt.show() to display the plot when running scripts.
Figures hold one or more axes where data is drawn.
Use plt.subplots() to create figures and axes easily.
Customize size and layout with parameters like figsize and grid shape.
plt.subplots() function return in matplotlib?axes when running fig, axes = plt.subplots(2, 2)?fig, ax = plt.subplots(2, 2) ax.plot([1, 2, 3], [4, 5, 6])
Pass the test and this pattern moves unknown