# 在 Python Matplotlib 中绘制多线图

Suraj Joshi 2024年2月15日

## 在 Python Matplotlib 中绘制单条线

``````import numpy as np
import matplotlib.pyplot as plt

x = np.arange(8)
y = 4 * x - 10

plt.plot(x, y)
plt.title("Plot line in Matplotlib", fontsize=15)
plt.xlabel("X", fontsize=13)
plt.ylabel("Y", fontsize=13)
plt.show()
``````

## 在 Python Matplotlib 中绘制多条线

``````import numpy as np
import matplotlib.pyplot as plt

x = np.arange(8)
y1 = 4 * x + 5
y2 = 3 * x + 5
y3 = 2 * x + 5
y4 = x + 5

plt.plot(x, y1)
plt.plot(x, y2)
plt.plot(x, y3)
plt.plot(x, y4)

plt.title("Plot Mutiple lines in Matplotlib", fontsize=15)
plt.xlabel("X", fontsize=13)
plt.ylabel("Y", fontsize=13)
plt.show()
``````

``````import numpy as np
import matplotlib.pyplot as plt

x = np.arange(8)
y1 = 4 * x + 5
y2 = 3 * x + 5
y3 = 2 * x + 5
y4 = x + 5

plt.plot(x, y1, label="4x+5")
plt.plot(x, y2, label="3x+5")
plt.plot(x, y3, label="2x+5")
plt.plot(x, y4, label="x+5")

plt.title("Plot Mutiple lines in Matplotlib", fontsize=15)
plt.xlabel("X", fontsize=13)
plt.ylabel("Y", fontsize=13)
plt.legend()
plt.show()
``````

``````import numpy as np
import matplotlib.pyplot as plt

x = np.arange(8)
y1 = 4 * x + 5
y2 = 3 * x + 5
y3 = 2 * x + 5
y4 = x + 5

colors = ["orange", "purple", "green", "red"]

plt.gca().set_prop_cycle(color=colors)
plt.plot(x, y1, label="4x+5")
plt.plot(x, y2, label="3x+5")
plt.plot(x, y3, label="2x+5")
plt.plot(x, y4, label="x+5")

plt.title("Plot Mutiple lines in Matplotlib", fontsize=15)
plt.xlabel("X", fontsize=13)
plt.ylabel("Y", fontsize=13)
plt.legend()
plt.show()
``````

Suraj Joshi is a backend software engineer at Matrice.ai.