亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何加載文本文件并在單個圖中繪制多列

如何加載文本文件并在單個圖中繪制多列

寶慕林4294392 2023-08-08 16:05:21
我有一個包含 5 列的文本文件。第 1 列是 X 軸(1,2,3,4),其余列是 y 軸。我想將它們繪制在一張圖中。1 0 0 0 02 7 14 2.53381 0.06918483 6 16 2.61242 0.05078564 6 17 2.65154 0.040285我正在嘗試將此代碼用于單個y值。import matplotlib.pyplot as pltimport numpy as npfile_name = input("Enter the file name:")x, y = np.loadtxt(file_name, delimiter=',', unpack=True)plt.plot(x,y, label='Loaded from file!')plt.xlabel('Action')plt.ylabel('Rate')plt.title('Plot of Rate')plt.legend()plt.show()如何y提取并繪制多個值?
查看完整描述

4 回答

?
眼眸繁星

TA貢獻1873條經驗 獲得超9個贊

用于*y將所有列數據(x 列之后)存儲在 y 變量中。delimite=' '如果您的數據是空格分隔的,請使用。

因此,只需在加載文件時進行此更正并保留其他代碼即可): x, *y = np.loadtxt(file_name, delimiter=',', unpack=True)

結果:

https://img1.sycdn.imooc.com//64d1f7620001ee4a06270465.jpg

查看完整回答
反對 回復 2023-08-08
?
慕哥9229398

TA貢獻1877條經驗 獲得超6個贊

  • 用于pandas.read_csv讀入文件,并將第0列設置為索引

    • 根據所示示例使用sep='\\s+',但如果它不是 OP 中的內容,請使用適當的分隔符。

    • 根據示例,使用header=None,但根據文件需要進行更改。

  • 用于pandas.DataFrame.plot在 1 行中繪制數據框。

  • 此外,使用的好處pandas是現在可以輕松分析數據。嘗試df.describe()獲取按列的統計數據。

import pandas as pd


# read the file

df = pd.read_csv('file.txt', sep='\\s+', header=None, index_col=0)


# add column names if desired; the list must have as many values as there are columns

df.columns = ['a', 'b', 'c', 'd']


# plot the data

df.plot(figsize=(7, 4), xlabel='Action', ylabel='Rate', title='Plot of Rate')

https://img2.sycdn.imooc.com/64d1f7770001ee7104500280.jpg

數據匯總統計

df.describe()


? ? ? ? ? ? ?a? ? ? ?b? ? ? ? c? ? ? ? d

count? 4.00000? ?4.000? 4.00000? 4.00000

mean? ?4.75000? 11.750? 1.94944? 0.04006

std? ? 3.20156? ?7.932? 1.30055? 0.02926

min? ? 0.00000? ?0.000? 0.00000? 0.00000

25%? ? 4.50000? 10.500? 1.90036? 0.03021

50%? ? 6.00000? 15.000? 2.57312? 0.04554

75%? ? 6.25000? 16.250? 2.62220? 0.05539

max? ? 7.00000? 17.000? 2.65154? 0.06918


查看完整回答
反對 回復 2023-08-08
?
慕勒3428872

TA貢獻1848條經驗 獲得超6個贊

請檢查片段

https://img1.sycdn.imooc.com//64d1f78d0001151d05840458.jpg

import matplotlib.pyplot as plt

import pandas as pd

import matplotlib.ticker as ticker


df = pd.read_csv('samp.txt', sep=" ", header=None)

df.columns = ["x", "y1", "y2", "y3","y4"]

print(df)


fig, ax = plt.subplots()


ax.plot(df['x'],df['y1'], label='Line1')

ax.plot(df['x'],df['y2'], label='Line2')

ax.plot(df['x'],df['y3'], label='Line3')

ax.plot(df['x'],df['y4'], label='Line4')

tick_spacing = 1

ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))

plt.xlabel('Action')

plt.ylabel('Rate')

plt.title('Plot of Rate')

plt.legend()

plt.show()


查看完整回答
反對 回復 2023-08-08
?
Smart貓小萌

TA貢獻1911條經驗 獲得超7個贊

所以首先我將數據加載到 4 個變量中,其中一個是 x,其中三個是 y1、y2、y3,然后我只使用該方法三次以創建'plot'三個不同的圖形


import matplotlib.pyplot as plt

import numpy as np


file_name = input("Enter the file name:")

data = np.loadtxt(file_name, delimiter=',', unpack=True)


x = data[0]

y1 = data[1] # the first graph

y2 = data[2] # the second graph

y3 = data[3] # the third graph

y4 = data[4] # the fourth graph


plt.plot(x,y1, label='y1')

plt.plot(x,y2, label='y2')

plt.plot(x,y3, label='y3')

plt.plot(x,y4, label='y3')

plt.xlabel('Action')

plt.ylabel('Rate')

plt.title('Plot of Rate')

plt.legend()

plt.show()

https://img1.sycdn.imooc.com//64d1f7a50001cfff03890278.jpg

查看完整回答
反對 回復 2023-08-08
  • 4 回答
  • 0 關注
  • 221 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號