Matplotlib Figure Function Guide

In matplotlib, the figure function is used to create a new figure window and return a Figure object. Some common parameters for this function include:

  1. num: Specifies the number of the graphic window, with the default being None, indicating that a new number will be automatically assigned.
  2. figsize: specifies the size of the graphical window in inches, with the default being (6.4, 4.8).
  3. DPI refers to the resolution of the graphic window in dots per inch, with a default of 100.
  4. facecolor: specifies the background color of the graphic window, with the default being ‘w’ representing white.
  5. Edgecolor: specifies the color of the border of the plot window, with the default being None, indicating no border will be displayed.

After creating a new figure window by invoking the figure function, various types of graphics can be drawn on this window, such as line graphs, scatter plots, bar graphs, etc. Additionally, other functions can be used to set various properties of the figure window, such as title, axis labels, legend, etc.

For example, the following code demonstrates how to use the figure function to create a new plot window and draw a simple line graph on it.

import matplotlib.pyplot as plt

# 创建一个新的图形窗口
fig = plt.figure()

# 在图形窗口上绘制一个折线图
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
plt.plot(x, y)

# 设置图形窗口的标题和坐标轴标签
plt.title('Simple Line Plot')
plt.xlabel('x')
plt.ylabel('y')

# 显示图形窗口
plt.show()

Running the above code will prompt a new graphic window to appear, displaying a line graph on the window.

bannerAds