plt fig,ax in python

To create a plot in matplotlib, you create a figure and one or more axes (subplots) to display the data. Here's an example of how to create a figure with one axes object:

main.py
import matplotlib.pyplot as plt

# Create a figure and an axis (subplot)
fig, ax = plt.subplots()

# Plot some data on the axes
ax.plot([1, 2, 3], [4, 5, 6])
158 chars
8 lines

This will create a figure with one subplot and plot the data [1, 2, 3] on the x-axis and [4, 5, 6] on the y-axis.

You can create multiple subplots in a figure by specifying the number of rows and columns. For example, to create a figure with two subplots that are arranged vertically, you would use:

main.py
import matplotlib.pyplot as plt

# Create a figure with two subplots arranged vertically
fig, axs = plt.subplots(nrows=2, ncols=1)

# Plot some data on the first subplot
axs[0].plot([1, 2, 3], [4, 5, 6])

# Plot some data on the second subplot
axs[1].plot([1, 2, 3], [6, 5, 4])
278 chars
11 lines

This code will create a figure with two subplots, one above the other, and plot data on each of them.

By default, matplotlib will create a single subplot if you don't specify the number of rows and columns.

related categories

gistlibby LogSnag