📜  pyplot figsize subplots (1)

📅  最后修改于: 2023-12-03 15:03:55.778000             🧑  作者: Mango

Pyplot, Figsize, and Subplots

Pyplot is a module in the matplotlib library that provides a convenient interface for creating a variety of graphs and charts, such as line plots, scatter plots, histograms, and more.

Figsize is an argument in the pyplot.figure() function that indicates the size of the figure to be created. It takes in a tuple or list of two values representing the width and height of the figure in inches.

Subplots is a function in Pyplot that allows for the creation of multiple plots within one figure. It takes in two arguments, the number of rows and columns of subplots desired, and can also specify the size of the figure using the figsize argument.

Example Usage

To create a simple line plot with a specific figure size using Pyplot and figsize, you can use the following code snippet:

import matplotlib.pyplot as plt

# create x and y data
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]

# create a figure with a specific size (width, height)
fig = plt.figure(figsize=(6, 4))

# create a line plot
plt.plot(x, y)

# show the plot
plt.show()

To create a figure with multiple subplots arranged in a grid using Pyplot and subplots, you can use the following code snippet:

import matplotlib.pyplot as plt

# create x and y data for each subplot
x1 = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
x2 = [0, 1, 2, 3, 4]
y2 = [0, 2, 4, 6, 8]
x3 = [-1, 0, 1, 2, 3]
y3 = [-1, 1, -1, 1, -1]

# create a figure with a 2x2 grid of subplots and a specific size (width, height)
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(8, 6))

# create subplots
axes[0, 0].plot(x1, y1)
axes[0, 1].scatter(x2, y2)
axes[1, 0].bar(x1, y1)
axes[1, 1].plot(x3, y3, 'ro--')

# set titles for subplots
axes[0, 0].set_title('Line Plot')
axes[0, 1].set_title('Scatter Plot')
axes[1, 0].set_title('Bar Chart')
axes[1, 1].set_title('Dashed Line Plot with Markers')

# adjust spacing between subplots
plt.subplots_adjust(wspace=0.3, hspace=0.5)

# show the plot
plt.show()

In this example, we've created a figure with a 2x2 grid of subplots, where each subplot displays a different type of graph. We've also adjusted the spacing between subplots using the subplots_adjust() function.

These are just a few examples of the many possibilities for using Pyplot, figsize, and subplots in Matplotlib. With these tools, you can create a wide range of visualizations to suit your needs.