Use MatPlotLib to plot DWBA, Plotly for Ex levels
This commit is contained in:
parent
0b2e0ee0cd
commit
f58c988128
107
PyGUIQt6/MatPlotLibWindow.py
Normal file
107
PyGUIQt6/MatPlotLibWindow.py
Normal file
|
@ -0,0 +1,107 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
from PyQt6.QtWidgets import (
|
||||
QGridLayout, QWidget, QCheckBox
|
||||
)
|
||||
|
||||
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib.backends.backend_qtagg import NavigationToolbar2QT as NavigationToolbar
|
||||
from matplotlib import get_backend
|
||||
|
||||
# Set backend to a Qt-compatible one
|
||||
plt.switch_backend('QtAgg') # Or use 'Qt5Agg' if there are still issues
|
||||
|
||||
class MatPlotLibWindow(QWidget):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
self.setWindowTitle("DWBA Plot")
|
||||
self.setGeometry(100, 100, 800, 600)
|
||||
|
||||
self.x = []
|
||||
self.data = []
|
||||
self.headers = []
|
||||
|
||||
self.figure, self.ax = plt.subplots()
|
||||
self.canvas = FigureCanvas(self.figure)
|
||||
self.toolbar = NavigationToolbar(self.canvas, self)
|
||||
|
||||
self.log_scale_checkbox = QCheckBox("Use Log Scale for Y-Axis")
|
||||
self.log_scale_checkbox.setChecked(True)
|
||||
self.log_scale_checkbox.stateChanged.connect(self.plot_matplotlib_graph)
|
||||
|
||||
self.gridline_checkbox = QCheckBox("Show Gridlines")
|
||||
self.gridline_checkbox.stateChanged.connect(self.plot_matplotlib_graph)
|
||||
|
||||
self.showMarker_checkBox = QCheckBox("Show Markers")
|
||||
self.showMarker_checkBox.setChecked(True)
|
||||
self.showMarker_checkBox.stateChanged.connect(self.plot_matplotlib_graph)
|
||||
|
||||
layout = QGridLayout(self)
|
||||
layout.addWidget(self.toolbar, 0, 0, 1, 3)
|
||||
layout.addWidget(self.showMarker_checkBox, 1, 0)
|
||||
layout.addWidget(self.log_scale_checkbox, 1, 1)
|
||||
layout.addWidget(self.gridline_checkbox, 1, 2)
|
||||
layout.addWidget(self.canvas, 2, 0, 5, 3)
|
||||
|
||||
def read_data(self,file_path):
|
||||
self.x = [] # List for the first column
|
||||
self.data = [] # 2D list for other columns
|
||||
self.headers = [] # List to store headers
|
||||
|
||||
with open(file_path, 'r') as file:
|
||||
header_found = False # Flag to indicate if the header has been found
|
||||
for line in file:
|
||||
# Skip lines that start with '#' and empty lines
|
||||
if line.startswith('#') or not line.strip():
|
||||
continue
|
||||
|
||||
if not header_found:
|
||||
self.headers = line.split() # Use the split parts as headers
|
||||
header_found = True # Set the flag to True to skip this block in future iterations
|
||||
# print(f"ELab parts found: {elab_parts}") # Print or process this as needed
|
||||
continue
|
||||
|
||||
# Split the line by whitespace
|
||||
parts = line.split()
|
||||
if len(parts) > 0: # Make sure there is at least one column
|
||||
self.x.append(float(parts[0])) # First column
|
||||
# Append the rest of the columns to data
|
||||
if len(self.data) == 0:
|
||||
# Initialize the data array with the right number of sublists
|
||||
self.data = [[] for _ in range(len(parts) - 1)]
|
||||
for i in range(len(parts) - 1):
|
||||
self.data[i].append(float(parts[i + 1])) # Rest of the columns
|
||||
|
||||
# print(self.headers)
|
||||
|
||||
def plot_matplotlib_graph(self):
|
||||
self.ax.clear()
|
||||
|
||||
plotStyle = '-' if not self.showMarker_checkBox.isChecked() else '-o'
|
||||
|
||||
for i, y in enumerate(self.data):
|
||||
self.ax.plot(self.x, y, plotStyle, label=self.headers[i + 1])
|
||||
|
||||
self.ax.set_xlabel("Angle_CM [Deg]")
|
||||
self.ax.set_ylabel("Xsec [mb/sr]")
|
||||
self.ax.legend(loc='upper right', frameon=True)
|
||||
|
||||
# Apply log scale for y-axis if selected
|
||||
if self.log_scale_checkbox.isChecked():
|
||||
self.ax.set_yscale('log')
|
||||
else:
|
||||
self.ax.set_yscale('linear')
|
||||
|
||||
# Toggle gridlines
|
||||
if self.gridline_checkbox.isChecked():
|
||||
self.ax.grid(True, which='both', linestyle='--', linewidth=0.5, color='gray')
|
||||
else:
|
||||
self.ax.grid(False)
|
||||
|
||||
self.ax.autoscale(enable=True, axis='x', tight=True)
|
||||
self.figure.tight_layout()
|
||||
|
||||
self.canvas.draw_idle()
|
||||
|
|
@ -15,6 +15,7 @@ from PyQt6.QtGui import QFont
|
|||
from ExtractXsecPy import extract_xsec
|
||||
from PlotWindow import PlotWindow
|
||||
from ExWindow import ExWindow
|
||||
from MatPlotLibWindow import MatPlotLibWindow
|
||||
|
||||
################################################## MainWindow
|
||||
class MyWindow(QMainWindow):
|
||||
|
@ -27,8 +28,8 @@ class MyWindow(QMainWindow):
|
|||
|
||||
self.DWBAFileName = "DWBA"
|
||||
self.bashResult = ""
|
||||
self.plot_window = None
|
||||
self.Ex_window = None
|
||||
self.plot_window = MatPlotLibWindow()
|
||||
self.Ex_window = ExWindow()
|
||||
|
||||
# Set up Group Box for DWBA Control
|
||||
self.gbDWBA = QGroupBox("DWBA")
|
||||
|
@ -104,35 +105,35 @@ class MyWindow(QMainWindow):
|
|||
group_layout.addWidget(self.bnCalDWBA, 12, 0, 1, 2)
|
||||
|
||||
# Ex Group
|
||||
# self.gbEx = QGroupBox("Ex")
|
||||
# Ex_layout = QGridLayout()
|
||||
# Ex_layout.setAlignment(Qt.AlignmentFlag.AlignTop)
|
||||
# self.gbEx.setLayout(Ex_layout)
|
||||
self.gbEx = QGroupBox("Ex")
|
||||
Ex_layout = QGridLayout()
|
||||
Ex_layout.setAlignment(Qt.AlignmentFlag.AlignTop)
|
||||
self.gbEx.setLayout(Ex_layout)
|
||||
|
||||
# lbName = QLabel("Isotop :")
|
||||
# lbName.setAlignment(Qt.AlignmentFlag.AlignRight)
|
||||
lbName = QLabel("Isotop :")
|
||||
lbName.setAlignment(Qt.AlignmentFlag.AlignRight)
|
||||
|
||||
# self.leName = QLineEdit()
|
||||
# self.leName.setText("12C")
|
||||
self.leName = QLineEdit()
|
||||
self.leName.setText("12C")
|
||||
|
||||
# lbMaxEx = QLabel("Max Ex [MeV]:")
|
||||
# lbMaxEx.setAlignment(Qt.AlignmentFlag.AlignRight)
|
||||
lbMaxEx = QLabel("Max Ex [MeV]:")
|
||||
lbMaxEx.setAlignment(Qt.AlignmentFlag.AlignRight)
|
||||
|
||||
# self.sbMaXEx = QDoubleSpinBox()
|
||||
# self.sbMaXEx.setMinimum(0)
|
||||
# self.sbMaXEx.setMaximum(20)
|
||||
# self.sbMaXEx.setDecimals(1)
|
||||
# self.sbMaXEx.setValue(10)
|
||||
self.sbMaXEx = QDoubleSpinBox()
|
||||
self.sbMaXEx.setMinimum(0)
|
||||
self.sbMaXEx.setMaximum(20)
|
||||
self.sbMaXEx.setDecimals(1)
|
||||
self.sbMaXEx.setValue(10)
|
||||
|
||||
# buEx = QPushButton("Get & Plot Ex")
|
||||
# buEx.setFixedHeight(40)
|
||||
# buEx.clicked.connect(self.open_Ex_window)
|
||||
buEx = QPushButton("Get & Plot Ex")
|
||||
buEx.setFixedHeight(40)
|
||||
buEx.clicked.connect(self.open_Ex_window)
|
||||
|
||||
# Ex_layout.addWidget(lbName, 0, 0)
|
||||
# Ex_layout.addWidget(self.leName, 0, 1)
|
||||
# Ex_layout.addWidget(lbMaxEx, 1, 0)
|
||||
# Ex_layout.addWidget(self.sbMaXEx, 1, 1)
|
||||
# Ex_layout.addWidget(buEx, 2, 0, 1, 2)
|
||||
Ex_layout.addWidget(lbName, 0, 0)
|
||||
Ex_layout.addWidget(self.leName, 0, 1)
|
||||
Ex_layout.addWidget(lbMaxEx, 1, 0)
|
||||
Ex_layout.addWidget(self.sbMaXEx, 1, 1)
|
||||
Ex_layout.addWidget(buEx, 2, 0, 1, 2)
|
||||
|
||||
# Set up the Right Side
|
||||
|
||||
|
@ -158,9 +159,9 @@ class MyWindow(QMainWindow):
|
|||
|
||||
# Set up the layout
|
||||
layout = QGridLayout()
|
||||
layout.addWidget(self.gbDWBA, 0, 0, 7, 1)
|
||||
# layout.addWidget(self.gbDWBA, 0, 0, 5, 1)
|
||||
# layout.addWidget(self.gbEx, 5, 0, 2, 1)
|
||||
# layout.addWidget(self.gbDWBA, 0, 0, 7, 1)
|
||||
layout.addWidget(self.gbDWBA, 0, 0, 5, 1)
|
||||
layout.addWidget(self.gbEx, 5, 0, 2, 1)
|
||||
|
||||
layout.addWidget(self.bnOpenDWBASource, 0, 1)
|
||||
layout.addWidget(self.leFileName, 0, 2, 1, 3)
|
||||
|
@ -256,33 +257,30 @@ class MyWindow(QMainWindow):
|
|||
if self.chkPlot.isChecked() and self.file_exists(self.DWBAFileName + ".Xsec.txt") :
|
||||
self.open_plot_window()
|
||||
|
||||
# def open_plot_window(self):
|
||||
# if self.plot_window is None :
|
||||
# self.plot_window = PlotWindow(self.DWBAFileName + ".Xsec.txt")
|
||||
# self.plot_window.show()
|
||||
# # self.plot_window.setAttribute(Qt.WA_DeleteOnClose) # Optional: Automatically delete when closed
|
||||
# else:
|
||||
# self.plot_window.read_data(self.DWBAFileName + ".Xsec.txt")
|
||||
# self.plot_window.plot_plotly_graph()
|
||||
# self.plot_window.show()
|
||||
|
||||
def open_plot_window(self):
|
||||
if self.plot_window is None :
|
||||
self.plot_window = PlotWindow(self.DWBAFileName + ".Xsec.txt")
|
||||
self.plot_window.show()
|
||||
# self.plot_window.setAttribute(Qt.WA_DeleteOnClose) # Optional: Automatically delete when closed
|
||||
else:
|
||||
self.plot_window.read_data(self.DWBAFileName + ".Xsec.txt")
|
||||
self.plot_window.plot_plotly_graph()
|
||||
self.plot_window.plot_matplotlib_graph()
|
||||
self.plot_window.show()
|
||||
|
||||
# def open_Ex_window(self):
|
||||
# if self.plot_window:
|
||||
# self.plot_window.close()
|
||||
# self.plot_window.__del__()
|
||||
|
||||
# if self.Ex_window is None :
|
||||
# self.Ex_window = ExWindow()
|
||||
|
||||
# self.Ex_window.GetEx(self.leName.text(), self.sbMaXEx.value())
|
||||
# self.Ex_window.plot_Ex_graph()
|
||||
# self.Ex_window.show()
|
||||
def open_Ex_window(self):
|
||||
self.Ex_window.GetEx(self.leName.text(), self.sbMaXEx.value())
|
||||
self.Ex_window.plot_Ex_graph()
|
||||
self.Ex_window.show()
|
||||
|
||||
|
||||
def closeEvent(self, event):
|
||||
if self.plot_window:
|
||||
self.plot_window.close() # Close the PlotWindow when MainWindow closes
|
||||
self.plot_window.__del__()
|
||||
if self.Ex_window:
|
||||
self.Ex_window.close() # Close the PlotWindow when MainWindow closes
|
||||
self.Ex_window.__del__()
|
||||
|
|
Loading…
Reference in New Issue
Block a user