Matplotlib Line Graph Guide

To draw a line graph using matplotlib, you can follow the steps below:

  1. Import the necessary libraries and modules.
import matplotlib.pyplot as plt
  1. Prepare the data:
x = [1, 2, 3, 4, 5]  # x轴数据
y = [5, 7, 3, 8, 4]  # y轴数据
  1. Create a drawing window.
plt.figure()
  1. Create a line graph:
plt.plot(x, y)
  1. Include a title, x-axis label, and y-axis label:
plt.title('折线图')  # 标题
plt.xlabel('x轴')  # x轴标签
plt.ylabel('y轴')  # y轴标签
  1. 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.

bannerAds