# 在 Python 中繪製陣列

Vaibhav Vaibhav 2023年1月30日

## 在 Python 中繪製一維陣列

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

x = np.array(range(0, 10))
y = np.array([100, 23, 44, 12, 55, 85, 69, 33, 75, 2])
plt.title("Plotting 1-D array")
plt.xlabel("X axis")
plt.ylabel("Y axis")
plt.plot(x, y, color="red", marker="o", label="Array elements")
plt.legend()
plt.show()
``````

## 在 Python 中繪製二維陣列

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

figure(figsize=(8, 6))
x = np.array(range(0, 10))
y = np.array(
[
[32, 69, 92, 81, 27, 97, 64, 98, 26, 22],
[85, 76, 63, 44, 94, 52, 71, 66, 2, 62],
[45, 80, 46, 24, 10, 74, 10, 33, 12, 60],
[73, 94, 21, 6, 93, 74, 36, 47, 16, 99],
[96, 84, 66, 12, 43, 58, 17, 16, 28, 6],
[83, 4, 56, 94, 97, 36, 94, 41, 74, 14],
[90, 13, 11, 71, 79, 49, 9, 69, 73, 61],
[94, 52, 28, 18, 14, 15, 91, 95, 16, 42],
[73, 35, 40, 25, 46, 25, 33, 1, 99, 30],
[61, 7, 89, 33, 94, 64, 1, 60, 41, 81],
]
)
plt.title("Plotting a2-D array")
plt.xlabel("X axis")
plt.ylabel("Y axis")

for i, array in enumerate(y):
plt.plot(
x,
array,
color=np.random.rand(
3,
),
marker="o",
label=f"Array #{i}",
)

plt.legend(loc="center left", bbox_to_anchor=(1, 0.5))
plt.show()
``````

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.