0
0
Data Analysis Pythondata~5 mins

Figure and axes creation in Data Analysis Python

Choose your learning style9 modes available
Introduction

We create figures and axes to draw charts and graphs. This helps us see data clearly.

When you want to draw a simple line or bar chart.
When you need to create multiple plots in one window.
When you want to customize the size or layout of your graph.
When you want to add titles, labels, or legends to your plot.
When you want to save your graph as an image file.
Syntax
Data Analysis Python
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).

Examples
Create a default figure and one axes.
Data Analysis Python
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
Create a figure 8 inches wide and 4 inches tall.
Data Analysis Python
fig, ax = plt.subplots(figsize=(8, 4))
Create a 2 by 2 grid of axes (4 plots in one figure).
Data Analysis Python
fig, axs = plt.subplots(2, 2)
Sample Program

This code creates a figure and axes, draws a line, adds labels, and shows the plot.

Data Analysis Python
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()
OutputSuccess
Important Notes

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.

Summary

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.