简体   繁体   中英

Matplotlib plot x axis (data file datetime)

I am going to display a figure graph using Python's GUI. GUI code and general code are applied differently (x-axis). I haven't put any options specifically in the plot, but the cause is unknown. Using obspy module, it is simple code to load mseed file which is earthquake data format and display graph. So far, in order to put the x-axis as the datetime, I tried using the 'arange' function and the 'for in' function to put it in the plot, but the GUI program does not run normally and ends. The problem is that the plot is normally plotted, but the x-axis is displayed as the sample rate of the data, not time.

The full code looks like this:

import sys, os
from os.path import basename
import numpy as np
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import matplotlib.pyplot as plt
import obspy.signal.util as sig
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from obspy import read, UTCDateTime
from matplotlib.backends.backend_qt5 import NavigationToolbar2QT as NavigationToolbar


class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setupUI()

    def setupUI(self):
        # GUI WINDOW
        self.setGeometry(100, 100, 1000, 700)
        self.setWindowTitle("TEST TITLE")

        # OPEN FILE DIALOG
        self.pushButton = QPushButton("LOAD FILE")
        self.pushButton.clicked.connect(self.pushButtonClicked)

        # LABLE, NOT USED
        self.label = QLabel("", self)
        self.label.resize(1000, 30)

        # PLOT FIGURE, CANVAS AND NAVIGATION TOOLBAR
        self.fig = plt.Figure()
        self.canvas = FigureCanvas(self.fig)
        self.toolbar = NavigationToolbar(self.canvas, self)

        # LAYOUT(CANVAS, TOOLBAR)
        topLayout = QVBoxLayout()
        topLayout.addWidget(self.toolbar)
        topLayout.addWidget(self.canvas)

        # LAYOUT(BUTTON)
        bottomLayout = QHBoxLayout()
        bottomLayout.addWidget(self.pushButton)
        bottomLayout.addStretch(1)

        # ALL LAYOUT
        layout = QVBoxLayout()
        layout.addLayout(topLayout)
        layout.addLayout(bottomLayout)
        layout.setStretchFactor(topLayout, 1)
        layout.setStretchFactor(bottomLayout, 0)

        # SET LOAYOUT
        self.setLayout(layout)

    # LOAD DATA FILE, DIALOG BUTTON
    def pushButtonClicked(self):

        #LOAD FILE
        pathfile0 = QFileDialog.getOpenFileName(self)
        fname = basename(pathfile0[0])
        data = read(fname)

        self.fig.clear()
        self.ax = self.fig.add_subplot(111)
        self.ax.cla()
        self.ax.plot(data[0])
        self.ax.set_ylabel("Count")
        self.ax.set_xlabel("Time Domain [s]")
        self.ax.grid()

        self.canvas.draw()
        self.canvas.show()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MyWindow()
    window.show()
    app.exec_()

GUI_plot

from obspy import read, UTCDateTime

def check_ms(fname) :

    data = read(fname)
    #myS = UTCDateTime("2020-02-12T02:30:00")
    #myE = myS + 120
    #data.trim(myS, myE)

    data.plot()

check_ms('KE2.TS.00.BGE.2020.043')

This code loads and plots mseed earthquake data in pure python code without a GUI.

NOT_GUI_plot

Instead of creating a new ax it is necessary to just pass the Figure to the plot method:

def pushButtonClicked(self):
    pathfile, _ = QFileDialog.getOpenFileName(self)
    if pathfile:
        fname = basename(pathfile)
        data = read(fname)
        data.plot(fig=self.fig)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM