How to use the plot function in R?

The plot function is a function in the Matplotlib library used for drawing graphs, often used for drawing line plots. Its basic usage is as follows:

  1. Import the pyplot module from the Matplotlib library.
import matplotlib.pyplot as plt
  1. Create data
x = [1, 2, 3, 4, 5]  # x轴数据
y = [10, 20, 15, 25, 30]  # y轴数据
  1. Invoke the plot function to draw the graph:
plt.plot(x, y)
  1. Optional: Include titles, axis labels, and other features.
plt.title("Line Plot")  # 添加标题
plt.xlabel("x")  # 添加x轴标签
plt.ylabel("y")  # 添加y轴标签
  1. Optional: Display Graphics:
plt.show()

The complete sample code is as follows:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 25, 30]

plt.plot(x, y)
plt.title("Line Plot")
plt.xlabel("x")
plt.ylabel("y")
plt.show()

After running the above code, a line plot connecting the data points will be generated.

bannerAds