Customizing the Navigation toolbar

Hi all,

How can I add a printer button to the Navigation toolbar http://matplotlib.sourceforge.net/users/navigation_toolbar.html ?

A small example for QT would be awesome.

Thanks in advance.

  Nils

How can I adapt the example

http://matplotlib.sourceforge.net/examples/user_interfaces/printing_in_wx.html

for QT ?

Nils

···

On Thu, 29 Sep 2011 14:56:21 +0200 "Nils Wagner" <nwagner@...1052...> wrote:

Hi all,

How can I add a printer button to the Navigation toolbar http://matplotlib.sourceforge.net/users/navigation_toolbar.html ?

A small example for QT would be awesome.

Thanks in advance.

Nils

Hi Nils.

The Qt based Navigation toolbar is just a Qt Widget with a proper layout already set. So you should be able to add any Qt widget to the toolbar using its addWidget method.
I was able to add a simple line edit (without any use) like so:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar2

class ViewWidget(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
    
        # create a simple main widget to keep the figure
        self.mainWidget = QWidget()
        self.setCentralWidget(self.mainWidget)

        layout = QVBoxLayout()
        self.mainWidget.setLayout(layout)

        # create a figure
        self.figure_canvas = FigureCanvas(Figure())
        layout.addWidget(self.figure_canvas, 10)

        # and the axes for the figure
        self.axes = self.figure_canvas.figure.add_subplot(111)

        # add a navigation toolbar
        self.navigation_toolbar = NavigationToolbar2(self.figure_canvas, self)
        layout.addWidget(self.navigation_toolbar, 0)

        # create a simple widget to extend the navigation toolbar
        anotherWidget=QLineEdit()
        # add the new widget to the existing navigation toolbar
        self.navigation_toolbar.addWidget(anotherWidget)
        
if __name__=="__main__":
    app=QApplication(sys.argv)
    mw=ViewWidget()
    mw.show()
    sys.exit(app.exec_())

Hope that helps ...

Best regards

Jens

···

-----Original Message-----
From: Nils Wagner [mailto:nwagner@…1052…]
Sent: Thursday, September 29, 2011 2:56 PM
To: matplotlib-users@lists.sourceforge.net
Subject: [Matplotlib-users] Customizing the Navigation toolbar

Hi all,

How can I add a printer button to the Navigation toolbar http://matplotlib.sourceforge.net/users/navigation_toolbar.html
?

A small example for QT would be awesome.

Thanks in advance.

  Nils

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure contains a definitive record of customers, application performance, security threats, fraudulent activity and more. Splunk takes this data and makes sense of it. Business sense. IT sense. Common sense.
http://p.sf.net/sfu/splunk-d2dcopy1
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hi Jens,

Thank you very much for your response.
Hence I have tried

import sys
import numpy as np
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar2

class ViewWidget(QMainWindow):
     def __init__(self):
         QMainWindow.__init__(self)

         # create a simple main widget to keep the figure
         self.mainWidget = QWidget()
         self.setCentralWidget(self.mainWidget)

         layout = QVBoxLayout()
         self.mainWidget.setLayout(layout)

         # create a figure
         self.figure_canvas = FigureCanvas(Figure())
         layout.addWidget(self.figure_canvas, 10)

         # and the axes for the figure
         self.axes = self.figure_canvas.figure.add_subplot(111)
         x = np.linspace(0.,2*np.pi,100)
         self.axes.plot(x,np.sin(x))
         # add a navigation toolbar
         self.navigation_toolbar = NavigationToolbar2(self.figure_canvas, self)
         layout.addWidget(self.navigation_toolbar, 0)

         # create a simple widget to extend the navigation toolbar
# anotherWidget=QLineEdit()
         printer = QPrinter()
         anotherWidget= QPrintDialog(printer,self)

         # add the new widget to the existing navigation toolbar
         self.navigation_toolbar.addWidget(anotherWidget)

if __name__=="__main__":
     app=QApplication(sys.argv)
     mw=ViewWidget()
     mw.show()
     sys.exit(app.exec_())

How do I connect the plot with the printer ?
How can I replace the printer dialog by a small icon ?

I found the lines
         a = self.addAction(self._icon('filesave.svg'), 'Save',
                 self.save_figure)

in backend_qt4.py

Nils

···

On Tue, 4 Oct 2011 10:09:57 +0200 Jens Nie <JNie@...3256...> wrote:

Hi Nils.

The Qt based Navigation toolbar is just a Qt Widget with a proper layout already set. So you should be able to add any Qt widget to the toolbar using its addWidget method.
I was able to add a simple line edit (without any use) like so:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar2

class ViewWidget(QMainWindow):
   def __init__(self):
       QMainWindow.__init__(self)
          # create a simple main widget to keep the figure
       self.mainWidget = QWidget()
       self.setCentralWidget(self.mainWidget)

       layout = QVBoxLayout()
       self.mainWidget.setLayout(layout)

       # create a figure
       self.figure_canvas = FigureCanvas(Figure())
       layout.addWidget(self.figure_canvas, 10)

       # and the axes for the figure
       self.axes = self.figure_canvas.figure.add_subplot(111)

       # add a navigation toolbar
       self.navigation_toolbar = NavigationToolbar2(self.figure_canvas, self)
       layout.addWidget(self.navigation_toolbar, 0)

       # create a simple widget to extend the navigation toolbar
       anotherWidget=QLineEdit()
       # add the new widget to the existing navigation toolbar
       self.navigation_toolbar.addWidget(anotherWidget)
       if __name__=="__main__":
   app=QApplication(sys.argv)
   mw=ViewWidget()
   mw.show()
   sys.exit(app.exec_())

Hope that helps ...

Best regards

Jens

Hi Nils.

I have never tried to do a real printout, so I am no expert here. My use case was almost always to directly create pdf or ps documents, which can be done nicely using the several print_* methods of the figurecanvas.

You can use Actions for your case instead of a widget. Actually they should considered to be recommended when using Qt4. Actions are a very elegant way to attach functionality to a program.

So define an action like you suggested, and add it to the toolbar using the addAction method of the toolbar.
Then you might want to implement a slot, that you in turn connect to the "triggered" signal of your action, which implements the real printing.
I can't really help you with the printing itself, but have found an implemented strategy after a bit googling. That might be a start ...

http://gotm.git.sourceforge.net/git/gitweb.cgi?p=gotm/gotm;a=blob;f=gui.py/xmlplot/gui_qt4.py;h=6e0239a958580ac2a35cd26653d83f26909fda24;hb=163207fa3fcc86f91008056c77e9c31fd6a9e246

Have a look at the onPrint method here.

Hoping again that this was helpful.

Jens

···

-----Original Message-----
From: Nils Wagner [mailto:nwagner@…1052…]
Sent: Tuesday, October 04, 2011 11:08 AM
To: Jens Nie
Cc: matplotlib-users@lists.sourceforge.net
Subject: Re: [Matplotlib-users] Customizing the Navigation toolbar

On Tue, 4 Oct 2011 10:09:57 +0200 Jens Nie <JNie@...3256...> wrote:

Hi Nils.

The Qt based Navigation toolbar is just a Qt Widget with a proper
layout already set. So you should be able to add any Qt widget to the
toolbar using its addWidget method.
I was able to add a simple line edit (without any use) like so:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as
FigureCanvas from matplotlib.backends.backend_qt4agg import
NavigationToolbar2QT as NavigationToolbar2

class ViewWidget(QMainWindow):
   def __init__(self):
       QMainWindow.__init__(self)
   
       # create a simple main widget to keep the figure
       self.mainWidget = QWidget()
       self.setCentralWidget(self.mainWidget)

       layout = QVBoxLayout()
       self.mainWidget.setLayout(layout)

       # create a figure
       self.figure_canvas = FigureCanvas(Figure())
       layout.addWidget(self.figure_canvas, 10)

       # and the axes for the figure
       self.axes =
self.figure_canvas.figure.add_subplot(111)

       # add a navigation toolbar
       self.navigation_toolbar =
NavigationToolbar2(self.figure_canvas, self)
       layout.addWidget(self.navigation_toolbar, 0)

       # create a simple widget to extend the navigation toolbar
       anotherWidget=QLineEdit()
       # add the new widget to the existing navigation toolbar
       self.navigation_toolbar.addWidget(anotherWidget)
       
if __name__=="__main__":
   app=QApplication(sys.argv)
   mw=ViewWidget()
   mw.show()
   sys.exit(app.exec_())

Hope that helps ...

Best regards

Jens

Hi Jens,

Thank you very much for your response.
Hence I have tried

import sys
import numpy as np
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar2

class ViewWidget(QMainWindow):
     def __init__(self):
         QMainWindow.__init__(self)

         # create a simple main widget to keep the figure
         self.mainWidget = QWidget()
         self.setCentralWidget(self.mainWidget)

         layout = QVBoxLayout()
         self.mainWidget.setLayout(layout)

         # create a figure
         self.figure_canvas = FigureCanvas(Figure())
         layout.addWidget(self.figure_canvas, 10)

         # and the axes for the figure
         self.axes =
self.figure_canvas.figure.add_subplot(111)
         x = np.linspace(0.,2*np.pi,100)
         self.axes.plot(x,np.sin(x))
         # add a navigation toolbar
         self.navigation_toolbar =
NavigationToolbar2(self.figure_canvas, self)
         layout.addWidget(self.navigation_toolbar, 0)

         # create a simple widget to extend the navigation toolbar
# anotherWidget=QLineEdit()
         printer = QPrinter()
         anotherWidget= QPrintDialog(printer,self)

         # add the new widget to the existing navigation toolbar
         self.navigation_toolbar.addWidget(anotherWidget)

if __name__=="__main__":
     app=QApplication(sys.argv)
     mw=ViewWidget()
     mw.show()
     sys.exit(app.exec_())

How do I connect the plot with the printer ?
How can I replace the printer dialog by a small icon ?

I found the lines
         a = self.addAction(self._icon('filesave.svg'),
'Save',
                 self.save_figure)

in backend_qt4.py

Nils

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure contains a definitive record of customers, application performance, security threats, fraudulent activity and more. Splunk takes this data and makes sense of it. Business sense. IT sense. Common sense.

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net