"""
This widget contains the layout for graphs which use the pyqtgraph
library to generate graphs. The widget can be executed as a standalone
version.
########################################
Functionalities provided in this widget:
########################################
1. Provides a canvas to plot graphs into it.
2. Adds the seismic colorbar to the default colorbars
3. Sets style of plots
4. Has method to disable autoscale
5. Provides clearing of canvas if plots are consecutively drawn (i.e. in
a for loop).
"""
if __name__ == "__main__":
# Add directories to path for imports
import os, sys, inspect
currentdir = os.path.dirname(
os.path.abspath(inspect.getfile(inspect.currentframe()))
)
parentdir = os.path.dirname(currentdir)
sys.path.insert(0, os.path.join(currentdir, "ui_files"))
sys.path.insert(0, parentdir)
# PyQt imports
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtCore import QRunnable, QThreadPool
# Import RegEx input validators
from regex_validators import *
# Import Qt Threading wrapper
from qt_multithreading_wrapper import Worker
# Import of file made from UI designer
from ui_pyqtgraph import Ui_widget_pyqtgraph as Ui_pyqtgraph
from PyQt5.QtCore import QRunnable, QThreadPool, pyqtSignal, QObject
# PyQTGraph
from pyqtgraph import PlotWidget, plot
import pyqtgraph as pg
# Import matplotlib colormaps to add seismic colorbar
from matplotlib import cm
# (For convenience purposes we need numpy)
import numpy as np
# Load the instanciate colormap
colormap = cm.get_cmap("seismic") # cm.get_cmap("CMRmap")
colormap._init()
# Add new entry to pyqtgraph Gradients dictionary that follows the
# seismic colormap from matplotlib
pg.graphicsItems.GradientEditorItem.Gradients["seismic"] = {
"mode": "rgb",
"ticks": [
(level, colormap(level, bytes=True)) for level in np.linspace(0, 1, num=7)
],
}
if __name__ == "__main__":
import numpy as np
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, obj=None, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setWindowTitle("Please god let this work too")
self.widget = WidgetPyqtgraph()
self.setCentralWidget(self.widget)
# Add test plot
self.widget.plots["test"] = self.widget.graphics_layout.addPlot(
row=0, col=0
)
# create shorter name
test_plot = self.widget.plots["test"]
# Set title
test_plot.setTitle("This is the title.")
# Set axis labels
test_plot.setLabel("bottom", "x-axis [cm<sup>-1</sup>]")
test_plot.setLabel("left", "y-axis 'pseudo[unit]'")
# Set style
self.widget.set_style(test_plot)
# Add data to plot
pen1 = pg.mkPen(color="#2ca02c", width=2.5, style=QtCore.Qt.SolidLine)
n = 20
test_plot.plot(
np.arange(n),
np.random.randint(100, size=n),
name="random test data",
pen=pen1,
)
# Remove plot
# self.widget.remove_plots()
self.show()
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
app.exec_()