Gtk3-Matplotlib-Cookbook Documentation Release 0.1

Gtk3-Matplotlib-Cookbook Documentation Release 0.1

gtk3-matplotlib-cookbook Documentation Release 0.1 Tobias Schönberg February 03, 2017 Contents 1 Requirements 3 2 Recommended reading 5 3 News and Comments 7 3.1 2014-12-26................................................7 3.2 2014-07-26................................................7 3.3 2014-06-27................................................7 3.4 2014-06-19................................................7 4 Directory 9 4.1 Hello plot!................................................9 4.2 Matplotlib-Toolbar............................................ 17 4.3 Zooming in on data............................................ 24 4.4 Entering data............................................... 29 5 Indices and tables 33 i ii gtk3-matplotlib-cookbook Documentation, Release 0.1 Copyright GNU Free Documentation License 1.3 with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts This Cookbook explains how to embed Matplotlib into GTK3 using Python 3. The tutorials will start out simple, and slowly increase in difficulty. The examples will focus on education and application of mathematics and science. The goal is to make the reader a independent developer of scientific applications that process and graph data. If you would like to add your own examples to this tutorial, please create an issue or pull-request at the github repository (https://github.com/spiessbuerger/GTK3-Matplotlib-Cookbook). Contents 1 gtk3-matplotlib-cookbook Documentation, Release 0.1 2 Contents CHAPTER 1 Requirements To follow the examples you should have access to GTK+ 3.x, Python 3.x, Matplotlib 1.3.x and Glade 3.16.x or a more recent version. 3 gtk3-matplotlib-cookbook Documentation, Release 0.1 4 Chapter 1. Requirements CHAPTER 2 Recommended reading In order to follow along with the Cookbook it is recommended to go through a Python 3.x tutorial (e.g. https://docs.python.org/3.4/tutorial/) and a tutorial about the Python bindings of GTK 3.x (e.g. http://python-gtk-3- tutorial.readthedocs.org/). Additional reading: • Matplotlib – http://wiki.scipy.org/Cookbook/Matplotlib • Scientific Python – http://scipy-lectures.github.io/ • GTK3 Reference – https://lazka.github.io/pgi-docs/Gtk-3.0/index.html 5 gtk3-matplotlib-cookbook Documentation, Release 0.1 6 Chapter 2. Recommended reading CHAPTER 3 News and Comments 3.1 2014-12-26 I have been busy on some other projects lately. Revisiting this project, I had some trouble running my examples on Ubuntu 14.04 and the current master of Matplotlib (1.5.dev1). The problem seems to be in the gtk3agg backend. I get the following error: NotImplementedError: Surface.create_for_data: Not Imple- mented yet.. Not sure how many people have trouble with this, but it is mentioned in different places (e.g. http://matplotlib.1069221.n5.nabble.com/Matplotlib-py3-gtk3-td42953.html). Getting the plots to work again just re- quires you to switch rendering backends: from matplotlib.backends.backend_gtk3cairo import FigureCanvasGTK3Cairo as FigureCanvas 3.2 2014-07-26 The fourth chapter about entering data is finished. I also decided to start code reviewing with Landscape. The score was 63 % on standard settings and 23 % on high (meaning very strict code reviewing). The low score is mostly caused by the conf.py file from Sphinx, my usage of “old classes” and Landscape complaining about the Gtk specific code. I will try to deal with the issues first, before uploading more chapters. 3.3 2014-06-27 Third chapter about zooming closer to datapoints is done. Currently I am just adding examples when I’m pleased with them. I will reorganize the book once the content is done. The new example relies heavily on classes. Does anybody reading this think that the amount of explanation is sufficient? 3.4 2014-06-19 This Cookbook is not finished and will be gradually developed in the coming months. If you would like to participate you can mail me or fork the repository. I am a beginning programmer, so the presented code might not be optimal. If you find any sub-optimal sections please write me, or create an issue or pull request on Github. 7 gtk3-matplotlib-cookbook Documentation, Release 0.1 8 Chapter 3. News and Comments CHAPTER 4 Directory 4.1 Hello plot! The first chapter will explain how to open an empty GTK3-window and then how to embed Matplotlib into it. For small applications the GTK3-code can be easily integrated into the Python-code. Building the interface with Glade is a little more complicated in the beginning. With increasing size though, the usage of Glade will become more useful. 4.1.1 Empty GTK 3 window Let’s start with the code that will open an empty window. #!/usr/bin/python3 from gi.repository import Gtk myfirstwindow= Gtk.Window() myfirstwindow.connect("delete-event", Gtk.main_quit) myfirstwindow.show_all() Gtk.main() These are all the lines that are required for a fully functional window. This is what they do: The first line helps Unix operating systems to recognize the file format of a file. In this case we wan’t the operating system to know that the file should be excecuted with Python 3.x: #!/usr/bin/python3 Then the program needs to import the gui-framework (or gui-toolkit). Older Gtk 2.x applications used (import gtk), but for Python 3 and Gtk 3.x applications (i.e. this tutorial) we need: from gi.repository import Gtk We can then define an object for a Gtk.Window(), which can have any name: myfirstwindow= Gtk.Window() Next the we have to connect our program with the quit-button (x-button) of the window. Otherwise closing the window will not terminate the application: myfirstwindow.connect("delete-event", Gtk.main_quit) 9 gtk3-matplotlib-cookbook Documentation, Release 0.1 The next line ensures that the program window is shown. Excluding this line will mean that the program start, but no window is displayed: myfirstwindow.show_all() The last line starts the main program loop with all functions. Without this line no loop is started and the program will not do anything: Gtk.main() Empty window with Glade Opening an empty window with Glade takes a little more effort. First we need to open the Glade interface designer. Then drag a GtkWindow into the workspace. By default the window will be named “window1”, which we can keep. Then we have to set a signal for that window, so we can later close it. The signal we need is “GtkWidget –> destroy” and in the column process we can set “on_window1_destroy”. This will also be the name of the function in the Python code. Fig. 4.1: The steps in Glade are: Create a GtkWindow, then open the Signals tab and enter “on_window1_destroy” for “GtkWidget –> destroy”. This is all we need for an empty (and closable) window. Then we can save the file with the extension ”.glade”. The finished XML-code of that file looks like this and should be fairly easy to understand: <?xml version="1.0" encoding="UTF-8"?> <!-- Generated with glade 3.16.1 --> <interface> <requires lib="gtk+" version="3.10"/> <object class="GtkWindow" id="window1"> 10 Chapter 4. Directory gtk3-matplotlib-cookbook Documentation, Release 0.1 <property name="can_focus">False</property> <signal name="destroy" handler="on_window1_destroy" swapped="no"/> <child> <placeholder/> </child> </object> </interface> Now we have to create a separate file to hold the python code that will call the Glade-file. The finished code looks like this: #!/usr/bin/python3 from gi.repository import Gtk class Signals: def on_window1_destroy(self, widget): Gtk.main_quit() builder= Gtk.Builder() builder.add_objects_from_file('empty-window-glade.glade',('window1','')) builder.connect_signals(Signals()) myfirstwindow= builder.get_object('window1') myfirstwindow.show_all() Gtk.main() In comparison to the previous approach a few lines of code have changed. First we call the Gtk.Builder() function: builder= Gtk.Builder() Then we use the Gtk.Builder() to add the objects from the Glade-file. In the bracket we first need to specify the Glade- file, and then a list of objects even if we just want to call one object (Thankyou errol from http://www.gtkforums.com for this tip): builder.add_objects_from_file('empty-window-glade.glade',('window1','')) Next the builder needs to connect the signals that we defined in the Glade file. The easiest way of doing this is to place the Signals in their own Class. We only defined one signal in Glade which was “on_window1_destroy”: builder.connect_signals(Signals()) class Signals: def on_window1_destroy(self, widget): Gtk.main_quit() The last two lines of the program are the same as for the previous example. Further Reading • Python GTK+ 3 Tutorial: Getting started • GTK+ 3 Reference Manual • GTK+ 3 Reference Manual: GtkBuilder • Glade - A user interface designer 4.1. Hello plot! 11 gtk3-matplotlib-cookbook Documentation, Release 0.1 4.1.2 Embedding Matplotlib Now that we have an empty window we will learn how to place Matplotlib into it. The main differences are that we need to import Matplotlib-specific packages, insert our Matplotlib-code and place the resulting FigureCanvas in a Gtk.ScrolledWindow (which is a child-element of the Gtk.Window). We will look at an example that will produce a random radial plot on each application start (adapted from http://matplotlib.org/dev/examples/pie_and_polar_charts/polar_bar_demo.html). The finished Python-code is: #!/usr/bin/python3 from gi.repository import Gtk from matplotlib.figure import Figure from numpy import arange, pi, random, linspace import matplotlib.cm as cm #Possibly this rendering backend is broken currently #from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas from matplotlib.backends.backend_gtk3cairo

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    37 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us