Matplotlib Line Graph Guide
To draw a line graph using matplotlib, you can follow the steps below:
- Import the necessary libraries and modules.
import matplotlib.pyplot as plt
- Prepare the data:
x = [1, 2, 3, 4, 5] # x轴数据
y = [5, 7, 3, 8, 4] # y轴数据
- Create a drawing window.
plt.figure()
- Create a line graph:
plt.plot(x, y)
- Include a title, x-axis label, and y-axis label:
plt.title('折线图') # 标题
plt.xlabel('x轴') # x轴标签
plt.ylabel('y轴') # y轴标签
- Display graphic:
plt.show()
In summary, the complete code is as follows:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5] # x轴数据
y = [5, 7, 3, 8, 4] # y轴数据
plt.figure()
plt.plot(x, y)
plt.title('折线图') # 标题
plt.xlabel('x轴') # x轴标签
plt.ylabel('y轴') # y轴标签
plt.show()
By running the code, you can generate a simple line graph.