fig.tight_layout()
时间: 2024-05-03 16:22:38 浏览: 61
The `fig.tight_layout()` function is a method in Matplotlib that automatically adjusts the padding between subplots so that the figure fits the canvas without overlapping or leaving too much whitespace. This function is useful when creating multiple subplots in a single figure and you want to ensure that they are all visible and properly spaced.
For example, if you have a figure with several subplots created using `plt.subplots()`, you can call `fig.tight_layout()` at the end of your code to adjust the padding between the subplots:
```
import matplotlib.pyplot as plt
fig, ax = plt.subplots(nrows=2, ncols=2)
# plot data on subplots...
fig.tight_layout()
plt.show()
```
This will adjust the padding between the subplots so that they are evenly spaced and there is no overlapping or wasted whitespace. The resulting figure will be more visually appealing and easier to read.
阅读全文