QWidget raise above matplotlib canvas - python-3.x

I am working on a project on which I have a GUI (coded by hand) with two tabs, and on each tab I have a different canvas (to plot different things in each tabs).
But, I added also some widgets on these tabs and when I add them to the layout, if I add the canvas at the same position of a button in the layout for example, I can click on this button anymore.
I know on PyQt it is possible to raise the level of the widget, so is there a way to do the same thing with a canvas?
Thank you in advance for your help. On this example, the "Quit" is active only on the right half.
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import matplotlib
matplotlib.use('Qt5Agg')
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
class FenetrePrincipale(QWidget):
def __init__(self, parent=None):
super(FenetrePrincipale, self).__init__(parent)
self.setupUi(self)
# Fonction de configuration de la classe
def setupUi(self, Form):
self.Form = Form
Form.setMinimumSize(1220, 850)
self.creation_GUI()
self.creation_figure()
self.creation_layout()
self.tabWidget.setCurrentIndex(0)
self.Bouton_quitter.clicked.connect(self.close)
def resizeEvent(self, QResizeEvent):
self.tabWidget.setMinimumSize(QSize(self.width() - 20, self.height() - 60))
def creation_GUI(self):
self.tabWidget = QTabWidget()
self.tab1 = QWidget()
self.Widget_choixPalette_Label = QLabel(self.tab1)
self.Widget_choixPalette_Label.setText("Text1")
self.Widget_choixPalette_ComboBox = QComboBox(self.tab1)
self.Widget_choixPalette_ComboBox.addItem("Try1")
self.Widget_choixPalette_ComboBox.addItem("Try2")
self.Bouton_quitter = QPushButton(self.tab1)
self.Bouton_quitter.setText("Quit")
def creation_layout(self):
LayoutForm = QGridLayout(self.Form)
LayoutG1 = QGridLayout()
LayoutTab1 = QGridLayout(self.tab1)
WidgetTemp = QWidget()
LayoutWidgetTemp = QGridLayout()
LayoutG1.addWidget(self.Bouton_quitter, 21, 29, 1, 2, Qt.AlignRight | Qt.AlignBottom)
LayoutG1.addWidget(self.canvas, 2, 10, 20, 20)
LayoutWidgetTemp.addWidget(self.Widget_choixPalette_Label, 0, 0, 1, 4)
LayoutWidgetTemp.addWidget(self.Widget_choixPalette_ComboBox, 1, 0, 1, 4)
WidgetTemp.setLayout(LayoutWidgetTemp)
LayoutG1.addWidget(WidgetTemp, 1, 18, 2, 4)
LayoutTab1.addLayout(LayoutG1, 0, 0, 1, 1)
self.tabWidget.addTab(self.tab1, " Tab1 ")
LayoutForm.addWidget(self.tabWidget, 1, 0, 1, 1)
def creation_figure(self):
# Create figure (transparent background)
self.figure = plt.figure()
self.figure.patch.set_facecolor('None')
self.canvas = FigureCanvas(self.figure)
self.canvas.setStyleSheet("background-color:transparent;")
# Adding one subplot for image
self.axe0 = self.figure.add_subplot(111)
self.axe0.get_xaxis().set_visible(False)
self.axe0.get_yaxis().set_visible(False)
plt.tight_layout()
# Data for init image
self.imageInit = [[255] * 320 for i in range(240)]
self.imageInit[0][0] = 0
# Init image and add colorbar
self.image = self.axe0.imshow(self.imageInit, interpolation='none')
divider = make_axes_locatable(self.axe0)
cax = divider.new_vertical(size="5%", pad=0.05, pack_start=True)
self.colorbar = self.figure.add_axes(cax)
self.figure.colorbar(self.image, cax=cax, orientation='horizontal')
self.canvas.draw()
if __name__ == '__main__':
app = QApplication(sys.argv)
# QApplication.setStyle(QStyleFactory.create("plastique"))
form = FenetrePrincipale()
form.show()
sys.exit(app.exec_())
Operating system: windows 7 Pro
Matplotlib version: 4.0.4
Matplotlib backend: Qt5Agg
Python version: 3.6
Other libraries: PyQt5
Edit 25/10/17 : new code for example

Below is a version of your example script that fixes all the issues. Most of the problems are caused by a very muddled use of layouts. I had to completely
re-write the creation_layout method in order to get a sane starting point so I could see where the problems were. I also temporarily restored the background colour of the canvas to make it easier to see how the widgets are layed out relative to each other. I realize that it won't be easy to incorporate some of my changes into your real code. But hopefully it will give you some ideas on how to simplify your layout structure.
The most important fix is the use of subplots_adjust in the creation_figure method. This removes all the empty space at the top of the canvas, so there is no longer any need to try to position other widgets on top of it.
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import matplotlib
matplotlib.use('Qt5Agg')
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
class FenetrePrincipale(QWidget):
def __init__(self, parent=None):
super(FenetrePrincipale, self).__init__(parent)
self.setupUi(self)
# Fonction de configuration de la classe
def setupUi(self, Form):
self.Form = Form
Form.setMinimumSize(1220, 850)
self.creation_GUI()
self.creation_figure()
self.creation_layout()
self.tabWidget.setCurrentIndex(0)
self.Bouton_quitter.clicked.connect(self.close)
def resizeEvent(self, QResizeEvent):
self.tabWidget.setMinimumSize(QSize(self.width() - 20, self.height() - 60))
def creation_GUI(self):
self.tabWidget = QTabWidget()
self.tab1 = QWidget()
self.tabWidget.addTab(self.tab1, " Tab1 ")
self.Widget_choixPalette_Label = QLabel(self.tab1)
self.Widget_choixPalette_Label.setText("Text1")
self.Widget_choixPalette_ComboBox = QComboBox(self.tab1)
self.Widget_choixPalette_ComboBox.addItem("Try1")
self.Widget_choixPalette_ComboBox.addItem("Try2")
self.Bouton_quitter = QPushButton(self.tab1)
self.Bouton_quitter.setText("Quit")
def creation_layout(self):
LayoutForm = QGridLayout(self)
LayoutForm.addWidget(self.tabWidget, 0, 0, 1, 1)
LayoutTab1 = QGridLayout(self.tab1)
LayoutTab1.addWidget(self.Widget_choixPalette_Label, 0, 1, 1, 1)
LayoutTab1.addWidget(self.Widget_choixPalette_ComboBox, 1, 1, 1, 1)
self.Widget_choixPalette_ComboBox.setMinimumWidth(200)
LayoutTab1.addWidget(self.canvas, 2, 0, 1, 3)
LayoutTab1.addWidget(self.Bouton_quitter, 2, 3, 1, 1, Qt.AlignRight | Qt.AlignBottom)
LayoutTab1.setRowStretch(2, 1)
LayoutTab1.setColumnStretch(0, 1)
LayoutTab1.setColumnStretch(2, 1)
def creation_figure(self):
# Create figure (transparent background)
self.figure = plt.figure()
# self.figure.patch.set_facecolor('None')
self.canvas = FigureCanvas(self.figure)
self.canvas.setStyleSheet("background-color:transparent;")
# Adding one subplot for image
self.axe0 = self.figure.add_subplot(111)
self.axe0.get_xaxis().set_visible(False)
self.axe0.get_yaxis().set_visible(False)
# plt.tight_layout()
# Data for init image
self.imageInit = [[255] * 320 for i in range(240)]
self.imageInit[0][0] = 0
# Init image and add colorbar
self.image = self.axe0.imshow(self.imageInit, interpolation='none')
divider = make_axes_locatable(self.axe0)
cax = divider.new_vertical(size="5%", pad=0.05, pack_start=True)
self.colorbar = self.figure.add_axes(cax)
self.figure.colorbar(self.image, cax=cax, orientation='horizontal')
plt.subplots_adjust(left=0, bottom=0.05, right=1, top=1, wspace=0, hspace=0)
self.canvas.draw()
if __name__ == '__main__':
app = QApplication(sys.argv)
# QApplication.setStyle(QStyleFactory.create("plastique"))
form = FenetrePrincipale()
form.show()
sys.exit(app.exec_())

Just reverse the order you add things to the layout. Add the canvas first, then the button on top
LayoutForm.addWidget(canvas,1,0,1,6)
LayoutForm.addWidget(button,1,0,1,2)

Related

PyQt5 to make subplots (upto 10 graphs) in the same widget

I have developed an UI to read large CSV file and plot. The problem is I can only plot one graph at a time. I need to select upto 10 variable and plot it in the same widget.
My UI should look like this
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
from PyQt5 import QtCore, QtGui, QtWidgets
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('Qt5Agg')
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QFileDialog
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT as Navi
from matplotlib.figure import Figure
import seaborn as sns
import pandas as pd
import sip # can be installed : pip install sip
from datetime import datetime
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import *
import sys
# We require a canvas class
from mpldatacursor import datacursor
class MatplotlibCanvas(FigureCanvasQTAgg):
def __init__(self,parent=None, dpi = 120):
fig = Figure(dpi = dpi)
self.axes = fig.add_subplot(111)
super(MatplotlibCanvas,self).__init__(fig)
fig.tight_layout()
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1440, 1000)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.label_1 = QtWidgets.QLabel(self.centralwidget)
self.label_1.setObjectName("label_1")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setObjectName("label_3")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setObjectName("comboBox")
self.horizontalLayout.addWidget(self.comboBox)
self.comboBox_1 = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_1.setObjectName("comboBox_1")
self.comboBox_2 = QtWidgets.QComboBox(self.centralwidget)
self.ComboBox_2 = CheckableComboBox()
self.comboBox_2.setObjectName("comboBox_2")
self.comboBox_2.setEditable(True)
#self.comboBox_2.setFocusPolicy( Qt.StrongFocus )
#self.comboBox_2.lineEdit().setMaxLength(45)
#self.comboBox_2.setReadOnly(True)
#self.comboBox_2.textChanged.connect(self.doSomething)
self.radioButton = QtWidgets.QRadioButton(self.centralwidget)
self.radioButton.setObjectName("radioButton")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.horizontalLayout.addWidget(self.label_1)
self.horizontalLayout.addWidget(self.comboBox_1)
self.horizontalLayout.addWidget(self.label_2)
self.horizontalLayout.addWidget(self.comboBox_2)
self.horizontalLayout.addWidget(self.label_3)
self.horizontalLayout.addWidget(self.radioButton)
self.gridLayout.addLayout(self.horizontalLayout, 0, 0, 1, 1)
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.spacerItem1 = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(self.spacerItem1)
self.gridLayout.addLayout(self.verticalLayout, 1, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionOpen_csv_file = QtWidgets.QAction(MainWindow)
self.actionOpen_csv_file.setObjectName("actionOpen_csv_file")
self.actionExit = QtWidgets.QAction(MainWindow)
self.actionExit.setObjectName("actionExit")
self.menuFile.addAction(self.actionOpen_csv_file)
self.menuFile.addAction(self.actionExit)
self.menubar.addAction(self.menuFile.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.filename = ''
self.canv = MatplotlibCanvas(self)
self.df = []
self.toolbar = Navi(self.canv,self.centralwidget)
self.horizontalLayout.addWidget(self.toolbar)
self.themes = ['bmh', 'classic', 'dark_background', 'fast',
'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn-bright',
'seaborn-colorblind', 'seaborn-dark-palette', 'seaborn-dark',
'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook',
'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk',
'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'seaborn',
'Solarize_Light2', 'tableau-colorblind10']
self.comboBox.addItems(self.themes)
self.comboBox_1.addItems(['Select horizontal axis here'])
self.comboBox_2.addItems(['Select vertical axis here'])
self.pushButton.clicked.connect(self.getFile)
self.comboBox.currentIndexChanged['QString'].connect(self.Update)
self.comboBox_1.currentIndexChanged['QString'].connect(self.selectXaxis)
self.comboBox_2.currentIndexChanged['QString'].connect(self.selectYaxis)
self.actionExit.triggered.connect(MainWindow.close)
self.actionOpen_csv_file.triggered.connect(self.getFile)
self.radioButton.clicked.connect(self.vsAll)
self.dataset={}
self.x_axis_slt=None
self.y_axis_slt=None
self.vsall = False
def vsAll(self):
"""
This function will be called upon triggering the radio check button. If set to True, all the columsn in the csv
will be plotted against the x-axis column. Please note that vs all means versus all, so that whatever value is
selected as the x-axis, it wont be plotted against itself in this mode. Moreover, the time series data will be
dedicated for the datetime x-axis and it wont be displayed in the vs all contents.
"""
if self.vsall==False:
self.vsall=True
else:
self.vsall=False
self.Update(self.themes[0])
def selectXaxis(self,value):
"""
This function will update the plot according to the data of x axis selected from combo box
"""
self.x_axis_slt=value
self.Update(self.themes[0])
def selectYaxis(self,value):
"""
This function will update the plot according to the data of y axis selected from combo box
"""
self.y_axis_slt=value
self.Update(self.themes[0])
def Update(self,value):
"""
This function will input the value of theme and accordingly plot the data, if the data is relative, i.e., x verus y-axis
then the user can assign x and y axis from the combo box. If all data should be plotted in paraller then leave,
the combo boxes of axis selections to their default starting location.
"""
plt.clf()
plt.style.use(value)
try:
self.horizontalLayout.removeWidget(self.toolbar)
self.verticalLayout.removeWidget(self.canv)
sip.delete(self.toolbar)
sip.delete(self.canv)
self.toolbar = None
self.canv = None
self.verticalLayout.removeItem(self.spacerItem1)
except Exception as e:
print(e)
pass
self.canv = MatplotlibCanvas(self)
self.toolbar = Navi(self.canv,self.centralwidget)
self.horizontalLayout.addWidget(self.toolbar)
self.verticalLayout.addWidget(self.canv)
self.canv.axes.cla()
ax = self.canv.axes
try:
if self.vsall:
for k,v in self.dataset.items():
if k!=self.x_axis_slt and type(v[0])!=datetime:
lines = ax.plot(self.dataset[self.x_axis_slt],v,label=k)
datacursor(lines)
legend = ax.legend()
legend.set_draggable(True)
ax.set_xlabel(self.x_axis_slt)
ax.set_ylabel('ALL OTHERS')
ax.set_title(self.Title)
#plt.setp(ax.xaxis.get_majorticklabels(), rotation=25) # uncomment if you want the x-axis to tilt 25 degree
else:
lines = ax.plot(self.dataset[self.x_axis_slt],self.dataset[self.y_axis_slt],label=self.y_axis_slt)
datacursor(lines)
legend = ax.legend()
legend.set_draggable(True)
ax.set_xlabel(self.x_axis_slt)
ax.set_ylabel(self.y_axis_slt)
ax.set_title(self.Title)
#plt.setp(ax.xaxis.get_majorticklabels(), rotation=25) # uncomment if you want the x-axis to tilt 25 degree
except Exception as e:
print(e)
try:
lines = self.df.plot(ax = self.canv.axes)
datacursor(lines)
legend = ax.legend()
legend.set_draggable(True)
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_title(self.Title)
except Exception as e:
print(e)
pass
pass
self.canv.draw()
def getFile(self):
""" This function will get the address of the csv file location
also calls a readData function
"""
try:
self.filename = QFileDialog.getOpenFileName(filter = "csv (*.csv)")[0]
self.readData()
except Exception as e:
print(e)
pass
def getDataset(self,csvfilename):
"""
This function will convert csv file to a dictionary of dataset, with keys as the columns' names and
values as values. The datatime format should be one of the standard datatime formats. Before plottting
we need to convert the string of data time in the csv file values to datatime format.
"""
df = pd.read_csv(csvfilename,sep=';')
LIST_OF_X_COLUMNS = ['Time[s]']
LIST_OF_Y_COLUMNS = df.columns
dataset={}
# time_format = "%d/%m/%Y %H:%M:%S"
# time_format = "%m/%d/%Y"
# time_format = "%d/%m/%Y"
# time_format = "%m-%d-%Y"
# time_format = "%d-%m-%Y"
# time_format = "%H:%M:%S"
# time_format = "%M:%S"
# time_format = '%d/%m/%Y %H:%M%f'
#time_format = '%Y-%m-%d %H:%M:%S.%f'
for col in LIST_OF_Y_COLUMNS:
dataset[col] = df[col].iloc[0:].values
try:
dataset[col] = [datetime.strptime(i) for i in df[col].iloc[0:].values]
except Exception as e:
pass
print(e)
return dataset, LIST_OF_X_COLUMNS, LIST_OF_Y_COLUMNS
def readData(self):
""" This function will read the data using pandas and call the update
function to plot
"""
import os
self.dataset={}
self.x_axis_slt=None
self.y_axis_slt=None
base_name = os.path.basename(self.filename)
self.Title = os.path.splitext(base_name)[0]
self.dataset, LIST_OF_X_COLUMNS, LIST_OF_Y_COLUMNS = self.getDataset(self.filename)
chunksize = 10 ** 6
self.df = pd.read_csv(self.filename,sep=';',chunksize = chunksize)
self.Update(self.themes[0]) # lets 0th theme be the default : bmh
self.comboBox_1.clear()
#self.comboBox_2.clear()
self.comboBox_2.addItems(['Select vertical axis here'])
self.comboBox_1.addItems(LIST_OF_X_COLUMNS)
self.comboBox_2.addItems(LIST_OF_Y_COLUMNS)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Select Theme"))
self.label_1.setText(_translate("MainWindow", "X-axis"))
self.label_2.setText(_translate("MainWindow", "Y-axis"))
self.label_3.setText(_translate("MainWindow", "vs all"))
self.pushButton.setText(_translate("MainWindow", "Open"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.actionOpen_csv_file.setText(_translate("MainWindow", "Open csv file"))
self.actionExit.setText(_translate("MainWindow", "Exit"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
This is what I have and the output looks like. But, I need to get the output as shown in picture 1.
I'm very new to PyQt5. So, could anyone please help me with this. Thanks in advance.
Output from UI I have develop

Aligning QGridLayout rows in QScrollArea

I am trying to create a lot of rows in a PyQt5 grid widget, but they try to expand as much as they can. How can I set a fixed cell height? They are represented like this:
But I would like them to stick at the top, ordered like this:
Code:
name = QtWidgets.QLabel()
name.setText(str(ui.nombre.toPlainText()) + "({}, {}, {})".format(do, cota, alejamiento))
borrar = QtWidgets.QPushButton()
borrar.setText("X")
borrar.clicked.connect(self.borrar)
ui.elementos.addWidget(name, self.num_elementos, 0, 1, 1)
ui.elementos.addWidget(borrar, self.num_elementos, 1, 1, 1)
self.num_elementos += 1
self.update()
print(self.puntos)
And the elementos widget is created in other class:
self.scroll = QtWidgets.QScrollArea(self.gridLayoutWidget_2)
self.scroll_widget = QtWidgets.QWidget()
self.scroll_widget.resize(200, 700)
self.elementos = QtWidgets.QGridLayout()
self.scroll_widget.setLayout(self.elementos)
self.scroll.setWidget(self.scroll_widget)
self.Punto.addWidget(self.scroll, 4, 0, 1, 3)
You need to add a stretchable space beneath the rows of widgets, so that it pushes them all up to the top. One way to do this is to put another widget inside the scroll-widget, and then use a vertical layout to add the spacer. It will also help if you make the scroll-widget resizable, otherwise the rows will start to get squashed if too many are added.
Below is a demo that implements all that. Hopefully it should be clear how you can adapt this to work with your own code:
import sys
from PyQt5 import QtCore, QtWidgets
class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.scroll = QtWidgets.QScrollArea()
self.scroll.setWidgetResizable(True)
self.scroll_widget = QtWidgets.QWidget()
self.scroll_widget.setMaximumWidth(200)
self.elementos_widget = QtWidgets.QWidget()
vbox = QtWidgets.QVBoxLayout(self.scroll_widget)
vbox.setContentsMargins(0, 0, 0, 0)
vbox.addWidget(self.elementos_widget)
vbox.addStretch()
self.elementos = QtWidgets.QGridLayout()
self.elementos_widget.setLayout(self.elementos)
self.scroll.setWidget(self.scroll_widget)
self.button = QtWidgets.QPushButton('Add')
self.button.clicked.connect(self.crear_punto)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.scroll)
layout.addWidget(self.button)
def crear_punto(self):
num_elementos = self.elementos.rowCount()
name = QtWidgets.QLabel()
name.setText('FOO %s' % num_elementos)
borrar = QtWidgets.QPushButton()
borrar.setText('X')
self.elementos.addWidget(name, num_elementos, 0)
self.elementos.addWidget(borrar, num_elementos, 1)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setGeometry(500, 100, 300, 500)
window.show()
sys.exit(app.exec_())

Why Matplotlib.rcParams.update style do not update facecolor PyQt5?

I am using matplotlib styles and I try to change the styles dynamically when the style is chosen in the combobox. I am using Matplotlib 2.2.3, Python 3.6.6, PyQt5, Windows 10. But when I choose the dark_background style, the figure facecolor and the axes facecolor do not change. Here an animation:
This is the code:
File IHMDrawDates.py generated with pyuic5:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MplMainWindow(object):
def setupUi(self, MplMainWindow):
MplMainWindow.setObjectName("MplMainWindow")
MplMainWindow.resize(628, 416)
self.centralwidget = QtWidgets.QWidget(MplMainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout_2.setObjectName("gridLayout_2")
self.mpl = MplWidgetTest(self.centralwidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.mpl.sizePolicy().hasHeightForWidth())
self.mpl.setSizePolicy(sizePolicy)
self.mpl.setObjectName("mpl")
self.gridLayout_2.addWidget(self.mpl, 0, 0, 1, 1)
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setMaximumSize(QtCore.QSize(95, 16777215))
self.groupBox.setObjectName("groupBox")
self.gridLayout = QtWidgets.QGridLayout(self.groupBox)
self.gridLayout.setObjectName("gridLayout")
self.buttonDrawDate = QtWidgets.QPushButton(self.groupBox)
self.buttonDrawDate.setMaximumSize(QtCore.QSize(75, 16777215))
self.buttonDrawDate.setObjectName("buttonDrawDate")
self.gridLayout.addWidget(self.buttonDrawDate, 1, 0, 1, 1)
self.buttonErase = QtWidgets.QPushButton(self.groupBox)
self.buttonErase.setMaximumSize(QtCore.QSize(75, 16777215))
self.buttonErase.setObjectName("buttonErase")
self.gridLayout.addWidget(self.buttonErase, 2, 0, 1, 1)
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.gridLayout.addItem(spacerItem, 3, 0, 1, 1)
self.comboTema = QtWidgets.QComboBox(self.groupBox)
self.comboTema.setObjectName("comboTema")
self.gridLayout.addWidget(self.comboTema, 0, 0, 1, 1)
self.gridLayout_2.addWidget(self.groupBox, 0, 1, 1, 1)
MplMainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MplMainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 628, 21))
self.menubar.setObjectName("menubar")
MplMainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MplMainWindow)
self.statusbar.setObjectName("statusbar")
MplMainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MplMainWindow)
QtCore.QMetaObject.connectSlotsByName(MplMainWindow)
def retranslateUi(self, MplMainWindow):
_translate = QtCore.QCoreApplication.translate
MplMainWindow.setWindowTitle(_translate("MplMainWindow", "MainWindow"))
self.groupBox.setTitle(_translate("MplMainWindow", "GroupBox"))
self.buttonDrawDate.setText(_translate("MplMainWindow", "Draw"))
self.buttonErase.setText(_translate("MplMainWindow", "Erase"))
from mplwidgettest import MplWidgetTest
mplwidgettest.py file that contains the method 'setTema' to update matplotlib's style. When I print the variable rcParams it contains axes.facecolor: black but it does not apply
from PyQt5.QtWidgets import QSizePolicy, QWidget, QVBoxLayout
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import (
FigureCanvasQTAgg as FigureCanvas,
NavigationToolbar2QT as NavigationToolbar)
import matplotlib as mplib
class MplCanvas(FigureCanvas):
"""Class to represent the FigureCanvas widget"""
def __init__(self):
# setup Matplotlib Figure and Axis
mplib.rcParams.update(mplib.rcParamsDefault)
mplib.style.use('bmh')
self.fig = Figure()
self.ax = self.fig.add_subplot(111)
# initialization of the canvas
FigureCanvas.__init__(self, self.fig)
# we define the widget as expandable
FigureCanvas.setSizePolicy(self,
QSizePolicy.Expanding,
QSizePolicy.Expanding)
# notify the system of updated policy
FigureCanvas.updateGeometry(self)
def setTema(self, tema='classic'):
print(tema)
mplib.style.use(tema)
print(mplib.rcParams)
mplib.rcParams.update(mplib.rcParams)
class MplWidgetTest(QWidget):
"""Widget defined in Qt Designer"""
def __init__(self, parent=None):
# initialization of Qt MainWindow widget
QWidget.__init__(self, parent)
# set the canvas to the Matplotlib widget
self.canvas = MplCanvas()
# create a NavigatioToolbar
self.ntb = NavigationToolbar(self.canvas, self)
# create a vertical box layout
self.vbl = QVBoxLayout()
# add mpl widget to vertical box
self.vbl.addWidget(self.canvas)
# add NavigationToolBar to vertical box
self.vbl.addWidget(self.ntb)
# set the layout to th vertical box
self.setLayout(self.vbl)
mainMplWidget.py File that calls the two previous files, and contains the method 'cambiarTema' that changes the matplotlib's style
import sys
from IHMDrawDates import Ui_MplMainWindow
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.QtGui import QCursor
import numpy as np
class DesignerMainWindow(QMainWindow, Ui_MplMainWindow):
def __init__(self, parent=None):
super(DesignerMainWindow, self).__init__(parent)
self.setupUi(self)
self.posX = []
self.posY = []
temas = ['bmh', 'classic', 'grayscale', 'seaborn-bright',
'ggplot', 'dark_background']
self.comboTema.addItems(temas)
# connect the signals with the slots
self.buttonDrawDate.clicked.connect(self.drawDate)
self.buttonErase.clicked.connect(self.eraseDate)
self.comboTema.currentIndexChanged.connect(self.cambiarTema)
def cambiarTema(self):
tema = self.comboTema.currentText()
self.mpl.canvas.setTema(str(tema))
self.mpl.canvas.ax.clear()
self.mpl.canvas.draw()
self.mpl.canvas.flush_events()
def drawDate(self):
x = np.arange(0, 100, 0.1)
y = np.sin(x)
self.mpl.canvas.ax.plot(x, y)
self.mpl.canvas.ax.relim()
self.mpl.canvas.ax.autoscale(True)
self.mpl.ntb.update()
self.mpl.ntb.push_current()
self.mpl.canvas.draw()
def eraseDate(self):
self.mpl.canvas.ax.clear()
self.mpl.ntb.update()
self.mpl.ntb.push_current()
self.mpl.canvas.draw()
if __name__ == '__main__':
app = 0
app = QApplication(sys.argv)
dmw = DesignerMainWindow()
# show it
dmw.show()
sys.exit(app.exec_())
The following is an explanation why setting a new style will not update the existing figure.
The matplotlib.rcParams is essentially a dictionary which stores certain default parameters that are meant to be used when creating objects in matplotlib.
rcParams.update will update this dictionary; update is a method of the python dict object. matplotlib.style.use is a shortcut to updating the rcParams, it will load the respective parameters from a file or dictionary.
Those parameters are then used when a new object is created. In a simplified fashion this would look like
def create_object(arg1, parameter1=None):
if not parameter1:
# use the default from the rcParams
parameter1 = rcParams["parameter1"]
obj = MatplotlibObject(arg1, parameter1=parameter1)
return obj
Such function is used when creating objects. However, once such object is created, it will not be run again when something in rcParams changes.
obj = create_object(1)
rcParams.update({"parameter1" : "New Value"})
# at this point, obj would not know about the new parameter1 value
As commented already, there are two options.
Change the parameter manually. You may change the parameter manually after the object has been created.
obj = create_object(1)
obj.set_parameter1("New Value")
Recreate the object. You may remove the existing object, update the rcParams and recreate the object such that it'll use the new value.
obj = create_object(1)
del obj
rcParams.update({"parameter1" : "New Value"})
obj = create_object(1)
The only solution I have found so far is to add these two lines in the 'setTema' method of the file mplwidgettest.py
self.fig.set_facecolor(mplib.rcParams['figure.facecolor'])
self.ax.set_facecolor(mplib.rcParams['axes.facecolor'])
The complete class is:
class MplCanvas(FigureCanvas):
"""Class to represent the FigureCanvas widget"""
def __init__(self):
# setup Matplotlib Figure and Axis
mplib.rcParams.update(mplib.rcParamsDefault)
mplib.style.use('bmh')
self.fig = Figure()
self.ax = self.fig.add_subplot(111)
# initialization of the canvas
FigureCanvas.__init__(self, self.fig)
# we define the widget as expandable
FigureCanvas.setSizePolicy(self,
QSizePolicy.Expanding,
QSizePolicy.Expanding)
# notify the system of updated policy
FigureCanvas.updateGeometry(self)
def setTema(self, tema='classic'):
mplib.rcParams.update(mplib.rcParamsDefault)
mplib.style.use(tema)
self.fig.set_facecolor(mplib.rcParams['figure.facecolor'])
self.ax.set_facecolor(mplib.rcParams['axes.facecolor'])
An animation of the result:
However, I do not think that each of the attributes of the figure has to be changed, if they are supposed to be defined in the style. Blessed matplotlib :-(

PyQt5 fix Qlabel position

i am new to PyQt5 and I try to create a window with grid layout for buttons. But I want to add two labels at top left and bottom right corner of the window.
from PyQt5 import QtWidgets
from PyQt5 import QtCore
import sys
class PrettyWidget(QtWidgets.QWidget):
def __init__(self):
super(PrettyWidget, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(600,300, 1000, 600)
self.setWindowTitle('Program v1.0')
# Grid Layout
grid = QtWidgets.QGridLayout()
self.setLayout(grid)
self.lbl1 = QtWidgets.QLabel(self)
self.lbl1.setText('Author Information and Copy Right')
self.lbl1.adjustSize()
self.lbl1.move(588, 0)
# Label indicator
self.lbl2 = QtWidgets.QLabel(self)
self.lbl2.setText('Click import to start...')
self.lbl2.adjustSize()
self.lbl2.move(0, 0)
# Import data Button
btn1 = QtWidgets.QPushButton('Select Data', self)
btn1.resize(btn1.sizeHint())
btn1.clicked.connect(self.getData)
grid.addWidget(btn1, 0, 0)
# Import names Button
btn2 = QtWidgets.QPushButton('Select Names', self)
btn2.resize(btn2.sizeHint())
btn2.clicked.connect(self.getNames)
grid.addWidget(btn2, 0, 1)
# Run Button
btn3 = QtWidgets.QPushButton('Run', self)
btn3.resize(btn3.sizeHint())
btn3.clicked.connect(self.Run)
grid.addWidget(btn3, 1, 0)
# Save Button
btn4 = QtWidgets.QPushButton('Save',self)
btn4.resize(btn4.sizeHint())
btn4.clicked.connect(self.Save)
grid.addWidget(btn4, 1, 1)
self.show()
def getData(self):
self.lbl2.setText('Data selected!')
self.lbl2.adjustSize()
def getNames(self):
self.lbl2.setText('Names selected!')
self.lbl2.adjustSize()
def Run(self):
self.lbl2.setText('Done!')
self.lbl2.adjustSize()
def Save(self):
self.lbl2.setText('Saved!')
self.lbl2.adjustSize()
def main():
app = QtWidgets.QApplication(sys.argv)
w = PrettyWidget()
app.exec_()
if __name__ == '__main__':
main()
As you can see I use absolute position for two labels now. So when I maximize or change the window size, the label stays at the same position. How do I stick lbl1 at bottom right and lbl at top left as always?
Paste the labels into the layout.
Set the stretch factor of row row to stretch .
import sys
from PyQt5 import QtWidgets
from PyQt5 import QtCore
class PrettyWidget(QtWidgets.QWidget):
def __init__(self):
super(PrettyWidget, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(300,100, 1000, 600)
self.setWindowTitle('Program v1.0')
# Grid Layout
grid = QtWidgets.QGridLayout()
self.setLayout(grid)
self.lbl1 = QtWidgets.QLabel(self, alignment=QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter)
self.lbl1.setText('Author Information and Copy Right')
self.lbl1.adjustSize()
grid.addWidget(self.lbl1, 0, 0)
grid.setRowStretch(1, 1) # <---- Sets the stretch factor of row row to stretch .
# Label indicator
self.lbl2 = QtWidgets.QLabel(self, alignment=QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
self.lbl2.setText('Click import to start...')
self.lbl2.adjustSize()
grid.addWidget(self.lbl2, 5, 1)
# Import data Button
btn1 = QtWidgets.QPushButton('Select Data', self)
btn1.resize(btn1.sizeHint())
btn1.clicked.connect(self.getData)
grid.addWidget(btn1, 2, 0)
# Import names Button
btn2 = QtWidgets.QPushButton('Select Names', self)
btn2.resize(btn2.sizeHint())
btn2.clicked.connect(self.getNames)
grid.addWidget(btn2, 2, 1)
# Run Button
btn3 = QtWidgets.QPushButton('Run', self)
btn3.resize(btn3.sizeHint())
btn3.clicked.connect(self.Run)
grid.addWidget(btn3, 3, 0)
# Save Button
btn4 = QtWidgets.QPushButton('Save',self)
btn4.resize(btn4.sizeHint())
btn4.clicked.connect(self.Save)
grid.addWidget(btn4, 3, 1)
grid.setRowStretch(4, 1) # <---- Sets the stretch factor of row row to stretch .
self.show()
def getData(self):
self.lbl2.setText('Data selected!')
self.lbl2.adjustSize()
def getNames(self):
self.lbl2.setText('Names selected!')
self.lbl2.adjustSize()
def Run(self):
self.lbl2.setText('Done!')
self.lbl2.adjustSize()
def Save(self):
self.lbl2.setText('Saved!')
self.lbl2.adjustSize()
def main():
app = QtWidgets.QApplication(sys.argv)
w = PrettyWidget()
app.exec_()
if __name__ == '__main__':
main()

How to display multicursor on a QTabWidget?

The multicursor example
The question is : If I want the plot to be displayed on a tab of the QTabWidget,how to make the MultiCursor works?
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import matplotlib
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt
import numpy as np
import sys
from matplotlib.gridspec import GridSpec
from matplotlib.widgets import MultiCursor
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
class MainWindow(QMainWindow):
def __init__(self):
super().__init__(flags=Qt.Window)
self.setFont(QFont("Microsoft YaHei", 10, QFont.Normal))
self.setMinimumSize(1550, 950)
self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
centralwidget = QWidget(flags=Qt.Widget)
centralwidget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.setCentralWidget(centralwidget)
self.tabview = QTabWidget()
self.tabview.currentChanged.connect(self.onchange)
self.chart_widget0 = QWidget()
self.chart_widget1 = QWidget()
self.dc0 = my_Canvas(self.chart_widget0, width=20, height=8, dpi=100)
self.dc1 = my_Canvas(self.chart_widget1, width=20, height=8, dpi=100)
self.tabview.addTab(self.dc0, "MultiCursor")
self.tabview.addTab(self.dc1, "Cursor")
toplayout = QHBoxLayout()
toplayout.addWidget(self.tabview)
centralwidget.setLayout(toplayout)
def onchange(self,i):
if i == 0:
self.dc0.update_figure()
elif i == 1:
self.dc1.update_figure()
class my_Canvas(FigureCanvas):
def __init__(self, parent=None, width=10, height=7, dpi=100):
self.fig = plt.figure(figsize=(width, height), dpi=dpi)
gs = GridSpec(2, 1, height_ratios=[3, 1])
self.axes1 = plt.subplot(gs[0])
self.axes2 = plt.subplot(gs[1])
self.compute_initial_figure()
FigureCanvas.__init__(self, self.fig)
self.setParent(parent)
def compute_initial_figure(self):
self.axes1.cla()
self.axes2.cla()
def update_figure(self):
t = np.arange(0.0, 2.0, 0.01)
s1 = np.sin(2*np.pi*t)
s2 = np.sin(4*np.pi*t)
self.axes1.plot(t, s1)
self.axes2.plot(t, s2)
multi = MultiCursor(self.fig.canvas, (self.axes1, self.axes2), color='r', lw=1)
self.draw()
if __name__ == '__main__':
app = QApplication(sys.argv)
w1 = MainWindow()
w1.show()
sys.exit(app.exec_())
How to modify the code to make the MultiCursor works, and could I control the display of the cursor by key or mousebutton click?
Further more, how to display the coordinate with the cursor?
As the Multicursor documentation tells us,
For the cursor to remain responsive you must keep a reference to it.
The easiest way is to make it a class variable,
self.multi = MultiCursor(...)

Resources