我有一個包含一系列數值的文本檔案。假設 .txt 檔案稱為 numbers.txt,它包含值 (283、927、273、284、251)。如何使用 python 創建這些值的折線圖?
x 軸告訴我們數字在系列中的位置。例如,927 是第二個,所以 x=2。換句話說,坐標將是 (1, 283)、(2, 927) 等。
謝謝
uj5u.com熱心網友回復:
該檔案numbers.txt包含:
283, 927, 273, 284, 251
使用 numpy 和 matplotlib:
import numpy as np
import matplotlib.pyplot as plt
data = np.loadtxt("/path/to/your/numbers.txt", delimiter=",")
plt.figure()
plt.plot(range(len(data)), data)
plt.show()
uj5u.com熱心網友回復:
您可能需要安裝 matplotlib 和python -m pip install matplotlib.
import matplotlib.pyplot as plt
# Get data from our file
with open('my_file_name.txt', 'r') as f:
text = f.read()
# This part really depends on how your txt data is formatted
text = text.replace(")", "")
text = text.replace("(", "")
text = text.replace(" ", "")
text_list = text.split(",")
# Turn our list of strings into a list of numbers
y_values = [float(i) for i in text_list]
# Get a list of x-values to correspond with our y-values
count = len(number_list)
x_values = [i for i in range(1, count 1)]
# Plot it
plt.plot(x_values, y_values)
plt.show()
如果您有任何具體問題,請告訴我。我知道如果您是 Python 新手,很多語法可能是陌生的。我也沒有測驗腳本,所以如果它失敗了請評論。
uj5u.com熱心網友回復:
假設您有一個名為numbers.txt以下內??容的檔案:
283, 927, 273, 284, 251
您可以使用, (seaborn 是 的超集)來創建您的折線圖:
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/457256.html
標籤:Python 麻木的 matplotlib 图形
