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