How to display multicursor on a QTabWidget? - python-3.x

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(...)

Related

Displaying plot in Tkinter GUI frame

I want to show the plot canvas in the top right side of UI (using Tkinter python3). I wrote the following code but I got the following error:
canvas = FigureCanvasTkAgg(fig, master=root) NameError: name 'root' is not defined
My code is:
import tkinter as tk
import tkinter.ttk as ttk
from tkinter import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import sys
class GUI(tk.Frame):
def __init__(self, master = None):
self.root = tk.Tk()
self.root.geometry("500x500")
tk.Frame.__init__(self, master)
self.createWidgets()
def start(self):
self.root.mainloop()
def createWidgets(self):
fig = plt.figure(figsize=(8, 8))
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().grid(row=0, column=1)
canvas.show()
def main():
appstart = GUI()
appstart.start()
if __name__ == "__main__":
main()
I made some changes to your code, deleting some unnecessary
references such as:
import tkinter.ttk as ttk
from tkinter import *
from matplotlib.figure import Figure
import sys
self.root = tk.Tk()
tk.Frame.__init__(self, master)
that you dont' use and I think only confuse yourself.
I've even simplified your script and add this code below to show to you wath is "self"
print(type(self))
for item in dir(self):
print(type(item),item)
I've add even a toolbar to the plot and some data to plot something.
Regards
import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
import numpy as np
class GUI(tk.Tk):
def __init__(self):
super().__init__()
self.title("Hello World")
self.geometry("500x500")
self.createWidgets()
print(type(self))
for item in dir(self):
print(type(item),item)
def createWidgets(self):
t = np.arange(0, 3, .01)
f0 = tk.Frame()
fig = plt.figure(figsize=(8, 8))
fig.add_subplot(111).plot(t, 2 * np.sin(2 * np.pi * t))
canvas = FigureCanvasTkAgg(fig, f0)
toolbar = NavigationToolbar2Tk(canvas, f0)
toolbar.update()
canvas._tkcanvas.pack(fill=tk.BOTH, expand=1)
f0.pack(fill=tk.BOTH, expand=1)
def main():
appstart = GUI()
appstart.mainloop()
if __name__ == "__main__":
main()
In your code you should use self.root if your intent is to use the root window:
canvas = FigureCanvasTkAgg(fig, master=self.root)
... or maybe just self, if your intent is to have it appear inside the frame. I'm not entirely sure what you are intending to do
canvas = FigureCanvasTkAgg(fig, master=self)
Somewhat unrelated, if you're explicitly creating a root window, you should be passing that to tk.Frame.__init__ rather than master.
tk.Frame.__init__(self, self.root)

Using matplotlib checkbuttons in PyQT5

Is it possible to use matplotlib checkbuttons in a plot embedded in PyQT5? The code is below, the plot works and it is embedded in a PyQT window but the checkbuttons do not add or remove the series as they should. Code works fine when taken out of PyQT.
import numpy as np
import sys
from matplotlib.widgets import CheckButtons
from PyQt5.QtWidgets import QDialog, QApplication, QPushButton, QVBoxLayout
from PyQt5 import QtCore
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt
class Window(QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.figure = plt.figure()
t = np.arange(0.0, 2.0, 0.01)
s0 = np.sin(2*np.pi*t)
s1 = np.sin(4*np.pi*t)
s2 = np.sin(6*np.pi*t)
self.figure, ax = plt.subplots()
l0, = ax.plot(t, s0, visible=False, lw=2, color='k', label='2 Hz')
l1, = ax.plot(t, s1, lw=2, color='r', label='4 Hz')
l2, = ax.plot(t, s2, lw=2, color='g', label='6 Hz')
plt.subplots_adjust(left=0.2)
self.lines = [l0, l1, l2]
# Make checkbuttons with all plotted lines with correct visibility
rax = plt.axes([0.05, 0.4, 0.1, 0.15])
self.labels = [str(line.get_label()) for line in self.lines]
visibility = [line.get_visible() for line in self.lines]
check = CheckButtons(rax, self.labels, visibility)
check.on_clicked(self.b)
## print('showing')
## plt.show()
self.canvas = FigureCanvas(self.figure)
layout = QVBoxLayout()
## layout.addWidget(self.toolbar)
layout.addWidget(self.canvas)
# layout.addWidget(self.button)
self.setLayout(layout)
self.canvas.draw()
## print('done')
## plt.show()
def b(self,label):
index = self.labels.index(label)
self.lines[index].set_visible(not self.lines[index].get_visible())
plt.draw()
if __name__ == '__main__':
app = QApplication(sys.argv)
main = Window()
main.show()
sys.exit(app.exec_())
If you are going to use matplotlib with Qt then you should not use pyplot but the Figure that is set on the canvas. Also "check" must be a member of the class.
Considering the above, the solution is:
from PyQt5.QtWidgets import QApplication, QDialog, QVBoxLayout
import numpy as np
from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.figure import Figure
from matplotlib.widgets import CheckButtons
class Window(QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.canvas = FigureCanvas(Figure(figsize=(5, 3)))
t = np.arange(0.0, 2.0, 0.01)
s0 = np.sin(2 * np.pi * t)
s1 = np.sin(4 * np.pi * t)
s2 = np.sin(6 * np.pi * t)
ax = self.canvas.figure.subplots()
(l0,) = ax.plot(t, s0, visible=False, lw=2, color="k", label="2 Hz")
(l1,) = ax.plot(t, s1, lw=2, color="r", label="4 Hz")
(l2,) = ax.plot(t, s2, lw=2, color="g", label="6 Hz")
self.canvas.figure.subplots_adjust(left=0.2)
self.lines = [l0, l1, l2]
rax = self.canvas.figure.add_axes([0.05, 0.4, 0.1, 0.15])
self.labels = [str(line.get_label()) for line in self.lines]
visibility = [line.get_visible() for line in self.lines]
self.check = CheckButtons(rax, self.labels, visibility)
self.check.on_clicked(self.on_clicked)
lay = QVBoxLayout(self)
lay.addWidget(self.canvas)
self.resize(640, 480)
def on_clicked(self, label):
index = self.labels.index(label)
self.lines[index].set_visible(not self.lines[index].get_visible())
self.canvas.draw()
def main():
import sys
app = QApplication(sys.argv)
main = Window()
main.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()

real-time plotting to a TK window using python3

I am trying to plot in real-time to a tkinter window in python3.
I am attempting to wrap my window in a class.
my code shows the graph, but data is not being plotted. Here is the code:
#! /usr/bin/python3
# -*- coding: utf-8 -*-
import sys
import matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import matplotlib.animation as animation
import tkinter
import time
import numpy as np
matplotlib.use("TkAgg")
class Window(tkinter.Frame):
def __init__(self, master = None):
tkinter.Frame.__init__(self, master)
self.master = master
self.master.grid()
self.fig = Figure(figsize=(5,5), dpi=100)
self.ax = self.fig.add_subplot(111)
self.t, self.y = [], []
self.init_window()
self.ani = animation.FuncAnimation(self.fig, self.animate, interval=1000)
def init_window(self):
# set window title
self.master.title("PlotSomething")
# run show_graph
self.show_graph()
def animate(self):
self.t.append(time.time())
self.y.append(np.random.random())
self.ax.clear()
self.ax.plot(self.t,self.y)
def show_graph(self):
canvas = FigureCanvasTkAgg(self.fig, self.master)
canvas.get_tk_widget().grid(row=1, column=1)
canvas.draw()
def client_exit(self):
sys.exit()
def main(args):
root = tkinter.Tk()
app = Window(root)
root.mainloop()
return 0
if __name__=="__main__":
sys.exit(main(sys.argv[1:]))
Please let me know what the issue is.
Bonus if you can help me get the real-time plot stuff into a separate class that I can call with my tkinter window.
I obviously need some learning on how to create and call classes using tkinter.
As always thank you for your support!

Python Change between matplotlib graphic in pyqt

Is there a way to change the matplotlib display in a pyqt application ?
I made a destopapplication with 2 radiobuttons, dependinging which button is pressed, i want on radioButton_p graphic1 to be shown and on radioButton_b the graphic2.
I am able to change it from the first button which is pressed to the second, but if the first button is pressed again it will not change back to the graphic of the first button.
Here is my code so far:
from PyQt5 import QtWidgets, QtCore, QtPrintSupport, QtGui
import matplotlib
matplotlib.use("Qt5Agg")
from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QVBoxLayout, QSizePolicy, QMessageBox, QWidget
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from PyQt5.QtWidgets import *
found_words = ["a", "b", "c", "d", "e", "f", "g", "h"]
cound_words = ['1','2','3','4','5','6','7','8']
found_pos = [ 'aaa','ssss']
count_pos = ['2','3']
class MyMplCanvas_begr(FigureCanvas):
"""Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
# self.compute_initial_figure()
self.axes.bar(found_words, cound_words, color=['r', 'b', 'k', 'y', 'g'])
self.axes.set_ylabel('Anzahl')
self.axes.set_xlabel('Begriffe')
self.axes.set_title('hi')
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self,
QSizePolicy.Expanding,
QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
class MyMplCanvas_pos(FigureCanvas):
"""Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
FigureCanvas.__init__(self, fig)
self.setParent(parent)
self.plot()
def plot(self):
colors = ['yellowgreen', 'lightcoral']
explode = (0.1, 0) # explode 1st slice
ax = self.figure.add_subplot(111)
ax.pie(count_pos, explode=explode, labels=found_pos, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=140)
class main_statistic(QtWidgets.QWidget):
def __init__(self, parent=None):
super(main_statistic, self).__init__(parent)
self.radioButton_p = QRadioButton('Pos', self)
self.radioButton_p.setGeometry(QtCore.QRect(100, 10, 95, 20))
self.radioButton_p.setCheckable(True)
self.radioButton_p.toggled.connect(self.clicked_pos)
self.radioButton_b = QRadioButton('Word', self)
self.radioButton_b.setGeometry(QtCore.QRect(200, 10, 95, 20))
self.radioButton_b.setCheckable(True)
self.radioButton_b.toggled.connect(self.clicked_begr)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.main_widget = QWidget(self)
self.main_widget.setGeometry(QtCore.QRect(100, 100, 700, 500))
self.main_widget1 = QWidget(self)
self.main_widget1.setGeometry(QtCore.QRect(100, 100, 700, 500))
def clicked_pos(self, down):
if down:
l = QVBoxLayout(self.main_widget)
dc = MyMplCanvas_pos(self.main_widget, width=5, height=4, dpi=100)
l.addWidget(dc)
else:
print('not prssed')
def clicked_begr(self, down):
if down:
l1 = QVBoxLayout(self.main_widget1)
dc1 = MyMplCanvas_begr(self.main_widget1, width=5, height=4, dpi=100)
l1.addWidget(dc1)
else:
print('not prssed2')
Instead of making 2 separate widgets you can add both of the graph widgets to the same layout and hide the one you don't want displayed (this is what i did bellow). Alternatively, you could make a layout and add or remove the widget you want displayed.
working code
from PyQt5 import QtWidgets, QtCore, QtPrintSupport, QtGui
import matplotlib
matplotlib.use("Qt5Agg")
from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QVBoxLayout, QSizePolicy, QMessageBox, QWidget, QRadioButton
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from PyQt5.QtWidgets import *
found_words = ["a", "b", "c", "d", "e", "f", "g", "h"]
cound_words = ['1','2','3','4','5','6','7','8']
found_pos = [ 'aaa','ssss']
count_pos = ['2','3']
class MyMplCanvas_begr(FigureCanvas):
"""Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
# self.compute_initial_figure()
self.axes.bar(found_words, cound_words, color=['r', 'b', 'k', 'y', 'g'])
self.axes.set_ylabel('Anzahl')
self.axes.set_xlabel('Begriffe')
self.axes.set_title('hi')
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self,
QSizePolicy.Expanding,
QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
class MyMplCanvas_pos(FigureCanvas):
"""Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
FigureCanvas.__init__(self, fig)
self.setParent(parent)
self.plot()
def plot(self):
colors = ['yellowgreen', 'lightcoral']
explode = (0.1, 0) # explode 1st slice
ax = self.figure.add_subplot(111)
ax.pie(count_pos, explode=explode, labels=found_pos, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=140)
class main_statistic(QtWidgets.QWidget):
def __init__(self, parent=None):
super(main_statistic, self).__init__(parent)
self.radioButton_p = QRadioButton('Pos', self)
self.radioButton_p.setGeometry(QtCore.QRect(100, 10, 95, 20))
self.radioButton_p.setCheckable(True)
self.radioButton_p.toggled.connect(self.clicked_pos)
self.radioButton_b = QRadioButton('Word', self)
self.radioButton_b.setGeometry(QtCore.QRect(200, 10, 95, 20))
self.radioButton_b.setCheckable(True)
self.radioButton_b.toggled.connect(self.clicked_begr)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.main_widget = QWidget(self)
self.main_widget.setGeometry(QtCore.QRect(100, 100, 700, 500))
#self.editmenu.setHidden(True)
#self.sideMenu.setHidden(False)
l = QVBoxLayout()
self.dc = MyMplCanvas_pos( width=5, height=4, dpi=100)
self.dc.setHidden(True)
l.addWidget(self.dc)
self.dc1 = MyMplCanvas_begr( width=5, height=4, dpi=100)
self.dc1.setHidden(True)
l.addWidget(self.dc1)
self.main_widget.setLayout(l)
def clicked_pos(self):
self.dc.setHidden(False)
self.dc1.setHidden(True)
def clicked_begr(self):
self.dc.setHidden(True)
self.dc1.setHidden(False)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
mainWin = main_statistic()
mainWin.show()
sys.exit(app.exec_())

QWidget raise above matplotlib canvas

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)

Resources