How is the plot function used in matplotlib?

The plot function in Matplotlib is used to create 2D graphs. Its basic usage is as follows:

import matplotlib.pyplot as plt

# 创建x轴数据
x = [1, 2, 3, 4, 5]
# 创建y轴数据
y = [2, 4, 6, 8, 10]

# 使用plot函数绘制线图
plt.plot(x, y)

# 设置标题
plt.title("Line Plot")
# 设置x轴标签
plt.xlabel("x")
# 设置y轴标签
plt.ylabel("y")

# 显示图形
plt.show()

The plot function takes two parameters, the first one being the data for the x-axis and the second one being the data for the y-axis. In the example above, the x-axis data is [1, 2, 3, 4, 5] and the y-axis data is [2, 4, 6, 8, 10]. The plot function will draw a line based on this data.

After drawing the graph, other functions can be used to set properties such as titles and labels. Finally, the graph is displayed using the plt.show() function.

In addition to creating line plots, the plot function can also be used to create scatter plots, bar charts, and more. For further detailed usage, please refer to the official documentation of Matplotlib.

bannerAds