create a facet plot with matplotlib in python

To create a facet plot with matplotlib in Python, you can use the subplots function to create a grid of subplots, and then plot your data on each subplot.

Here's an example to create a facet plot with matplotlib:

main.py
import matplotlib.pyplot as plt
import numpy as np

# Generate some fake data
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)

# Create a 2x2 grid of subplots
fig, axes = plt.subplots(nrows=2, ncols=2)

# Plot data on each subplot
axes[0, 0].plot(x, y1)
axes[0, 0].set_title("Plot 1")

axes[0, 1].plot(x, y2)
axes[0, 1].set_title("Plot 2")

axes[1, 0].plot(x, y3)
axes[1, 0].set_title("Plot 3")

# Remove the empty subplot
fig.delaxes(axes[1, 1])

# Adjust spacing between subplots
plt.tight_layout()

# Show the plot
plt.show()
554 chars
31 lines

In this example, we create a 2x2 grid of subplots using the subplots function. We then use indexing to access each subplot and plot our data on it. Finally, we adjust the spacing between subplots and display the plot using show().

This will result in a facet plot with 3 plots arranged in a 2x2 grid.

Note: You can modify the number of rows and columns in the subplots function to create a different grid layout.

gistlibby LogSnag