Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Graphs - Navigation Tool Bar Not Showing Up

Status
Not open for further replies.

sticksandtriangles

Structural
Apr 7, 2015
476
Finally starting to get around to making GUIs/graphs in python and I want to bring in the nice navigation tool bar that i have seen in the matplotlib "pacakge" (not sure if package is the right term?).

My code looks like the following taken from an example online with small modifications to bring in the navigation toolbar.

Python:
import matplotlib.pyplot as plt 
import random
 
class App(QMainWindow):
 
    def __init__(self):
        super().__init__()
        self.left = 100
        self.top = 100
        self.title = 'PyQt5 matplotlib example - pythonspot.com'
        self.width = 640
        self.height = 400
        self.initUI() #not sure what this line of code does
[COLOR=#EF2929]        self.fig=Figure()
        self.canvas=FigureCanvas(self.fig)
        #add plot toolbar from matplotlib
        self.toolbar = NavigationToolbar(self.canvas, self)[/color]
 
    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
 
        m = PlotCanvas(self, width=5, height=4)
        m.move(0,0)
 
        button = QPushButton('PyQt5 buttons', self)
        button.setToolTip('This is an example button')
        button.move(500,0)
        button.resize(140,100)
 
        self.show()
 
 
class PlotCanvas(FigureCanvas):
 
    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)
 
        FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding,QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)
        self.plot()
 
 
 
    def plot(self):
        data = [random.random() for i in range(25)]
        ax = self.figure.add_subplot(111)
        ax.plot(data, 'r-')
        ax.set_title('PyQt Matplotlib Example')
        self.draw()
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

Above, I've highlighted in red the lines of code I've added. My graph looks like the following:
image_2_furaum.png


The navigation bar, from my understanding, would be at the top of the graph and look like:
image_3_i8etfy.png


Any ideas on where I went wrong?

Thanks!



S&T
 
Replies continue below

Recommended for you

Well, the first red flag is you are lacking a majority of import statements to use the modules.

This is the source of your code?
You need sys, PyQt, other matplotlib modules, etc.

As I look at the PyQt module, it seems to a require an account in order to install, which I am not willing to do. If you have that account, with all the appropriate modules installed, just double check your imports and go from there.
 
sorry, missed the top part of my script:

Python:
import sys
 
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QVBoxLayout, QSizePolicy, QMessageBox, QWidget, QPushButton
from PyQt5.QtGui import QIcon
 
 
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt 
import random
 
class App(QMainWindow):
 
    def __init__(self):
        super().__init__()
        self.left = 100
        self.top = 100
        self.title = 'PyQt5 matplotlib example - pythonspot.com'
        self.width = 640
        self.height = 400
        self.initUI() #not sure what this line of code does
        self.fig=Figure()
        self.canvas=FigureCanvas(self.fig)
        #add plot toolbar from matplotlib
        self.toolbar = NavigationToolbar(self.canvas, self)
 
    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
 
        m = PlotCanvas(self, width=5, height=4)
        m.move(0,0)
 
        button = QPushButton('PyQt5 buttons', self)
        button.setToolTip('This is an example button')
        button.move(500,0)
        button.resize(140,100)
 
        self.show()
 
 
class PlotCanvas(FigureCanvas):
 
    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)
 
        FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding,QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)
        self.plot()
 
 
 
    def plot(self):
        data = [random.random() for i in range(25)]
        ax = self.figure.add_subplot(111)
        ax.plot(data, 'r-')
        ax.set_title('PyQt Matplotlib Example')
        self.draw()
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

I have all the front end stuff and it runs with no errors. I'll try out your link when I get back home tonight Celt.

S&T
 
for whats it's worth I found embedding Matplotlib into a GUI looked wonderful but for what I was doing had some short comings on speed and I always ended up with hugely bloated compiled files because of all the libraries being pulled in by Matplotlib (probably more tinkering with compile options could clean this up).

For the speed thing if you plan to keep plotting different data with the same number of points, like a beam shear diagram, I found populating the graph with a dummy data set and changing the x,y values of the set to be much faster than wiping out the data-set and creating a new one.

Open Source Structural Applications:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor