如何在具有圖例的 Matplotlib 中繪製來自多個兩欄文字檔案的資料?
要在具有圖例的 Matplotlib 中繪製來自多個兩欄文字檔案的資料,我們可以執行以下步驟 -
從 pylab 匯入genfromtxt 。它有幾個選項可以從文字檔案讀取資料並繪製資料。
使用 genfromtxt 讀取兩個文字檔案 test.txt和 test1.txt (有兩欄資料),並將資料儲存在兩個變數 firstfiledata 和 secondfiledata 中。
使用plot()方法繪製資料。標籤將顯示為圖例。
要顯示圖形,請使用 show() 方法。
示例
from matplotlib import pyplot as plt;
from pylab import genfromtxt;
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
firstfiledata = genfromtxt("test.txt");
secondfiledata = genfromtxt("test1.txt");
plt.plot(firstfiledata[:, 0], firstfiledata[:, 1], label="test.txt Data");
plt.plot(secondfiledata[:, 0], secondfiledata[:, 1], label="test1.txt Data");
plt.legend();
plt.show();輸出
Suppse 這兩個文字檔案具有以下資料 -
test.txt ======== 1 3 2 5 3 27 4 9 5 11 6 13 7 15 8 17 9 19
test1.txt ========= 11 13 12 15 13 17 14 19 15 21 16 23 17 25 18 27 19 29
當我們執行程式碼時,它將產生以下輸出 -

廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C #
MongoDB
MySQL
Javascript
PHP