溫馨提示×

python怎么用matplotlib畫折線圖

小億
93
2024-02-04 17:40:42
欄目: 編程語言

要用matplotlib畫折線圖,可以按照以下步驟進行操作:

  1. 導入所需的庫和模塊:
import matplotlib.pyplot as plt
  1. 準備數據:
x = [1, 2, 3, 4, 5]  # x軸數據
y = [5, 7, 3, 8, 4]  # y軸數據
  1. 創建繪圖窗口:
plt.figure()
  1. 繪制折線圖:
plt.plot(x, y)
  1. 添加標題、x軸標簽和y軸標簽:
plt.title('折線圖')  # 標題
plt.xlabel('x軸')  # x軸標簽
plt.ylabel('y軸')  # y軸標簽
  1. 顯示圖形:
plt.show()

綜合起來,完整的代碼如下所示:

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()

運行代碼,就可以生成一個簡單的折線圖。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女