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

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

如何在 Matplotlib 中繪制一個非常大的數據集(日期、時間(x 軸)與記錄的值(y 軸)

如何在 Matplotlib 中繪制一個非常大的數據集(日期、時間(x 軸)與記錄的值(y 軸)

森欄 2023-06-13 16:49:38
我應該在 python 中準備一個 x vs y 圖。我的數據集由日期 - 時間和溫度組成,以每年 15 分鐘的間隔記錄。假設我有一個月的數據,我試圖在 Matplotlib 中繪制它。我得到的圖表不是那么清楚,因為 x 軸(數據時間)在整個軸上都被填充了,我沒有得到清晰的圖片,而 Excel 與 matplotlib 相比給出了一個很好的圖表。我用來打開 30 個單獨的每日 csv 數據記錄文件并將其連接起來形成一個數據框的代碼如下import pandas as pdfrom openpyxl import load_workbookimport tkinter as tkimport datetimefrom datetime import datetimefrom datetime import timefrom tkinter import filedialogimport matplotlib.pyplot as pltroot = tk.Tk()root.withdraw()root.call('wm', 'attributes', '.', '-topmost', True)files = filedialog.askopenfilename(multiple=True) %gui tkvar = root.tk.splitlist(files)filePaths = []for f in var:    df = pd.read_csv(f,skiprows=8, index_col=None, header=0, parse_dates=True, squeeze=True, encoding='ISO-8859–1', names=['Date', 'Time', 'Temperature', 'Humidty']) #,     filePaths.append(df)    df = pd.concat(filePaths, axis=0, join='outer', ignore_index=False, sort=True, verify_integrity=False, levels=None)     df["Time period"] = df["Date"] + df["Time"]    plt.figure()    plt.subplots(figsize=(25,20))    plt.plot('Time period', 'Temperature', data=df, linewidth=2, color='g')    plt.title('Temperature distribution Graph')    plt.xlabel('Time')    plt.grid(True)數據示例輸出圖如下所示:正如您在輸出圖中看到的那樣,x 軸上的數據點很豐富,而且它不是可讀的形式。此外,如果我在幾天內加載和連接 .csv 文件,matplotlib 會給出多個圖表。在 Excel/Libre 中繪制的相同數據集給出了一個平滑的圖表,x 軸上的日期排列有序,折線圖也很完美。我想重寫我的代碼來繪制一個類似于在 Excel/Libre 中繪制的圖形。請幫忙
查看完整描述

1 回答

?
慕田峪9158850

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

試試這個方法:

使用日期定位器將 x 軸格式化為您需要的日期范圍。日期定位器可用于定義以秒、分鐘……為單位的時間間隔:

  • SecondLocator:定位秒

  • MinuteLocator:定位分鐘

  • HourLocator:定位時間

  • DayLocator:定位一個月中的指定日期

  • MonthLocator:定位月份

  • YearLocator:定位年份

在示例中,我使用MinuteLocator, 間隔 15 分鐘。

在繪圖中導入matplotlib.dates工作日期:

import?matplotlib.dates?as?mdates

import?pandas?as?pd
import?matplotlib.pyplot?as?plt

獲取您的數據

# Sample data

# Data

df = pd.DataFrame({

? ? 'Date': ['07/14/2020', '07/14/2020', '07/14/2020', '07/14/2020'],

? ? 'Time': ['12:15:00 AM', '12:30:00 AM', '12:45:00 AM', '01:00:00 AM'],

? ? 'Temperature': [22.5, 22.5, 22.5, 23.0]

})

從字符串轉換Time period為日期對象:


# Convert data to Date and Time

df["Time period"] = pd.to_datetime(df['Date'] + ' ' + df['Time'])

定義min和max間隔:


min = min(df['Time period'])

max = max(df['Time period'])

創建你的情節:


# Plot


# Create figure and plot space

fig = plt.figure(figsize=(10, 10))

ax = fig.add_subplot()

使用定位器設置時間間隔:


# Set Time Interval

ax.xaxis.set_major_locator(mdates.MinuteLocator(interval=15))

ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M'))

設置繪圖選項并繪制:


# Set labels

ax.set(xlabel="Time",

? ? ? ?ylabel="Temperature",

? ? ? ?title="Temperature distribution Graph", xlim=[min , max])


# Plot chart

ax.plot('Time period', 'Temperature', data=df, linewidth=2, color='g')

ax.grid(True)

fig.autofmt_xdate()

plt.show()


查看完整回答
反對 回復 2023-06-13
  • 1 回答
  • 0 關注
  • 202 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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