Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Plplot integration

jtappin edited this page · 7 revisions

Scientific plotting and gtk-fortran


Under construction


Introduction.

Since Fortran is primarily a scientific programming language, it is useful to have scientific data plotting tools available to draw into a gtk window created by gtk-fortran. The plplot library can potentially provide that functionality as it has a Fortran95 binding and also support for drawing to external cairo surfaces.

For this reason the necessary tools for integrating plplot with gtk-fortran are distributed as part of the gtk-fortran tree.

Requirements and installation.

In the following it will be assumed that the cmake-based build system is being used.

For most Linux distributions, plplot is distributed as a number of packages (the precise division depends on the distribution) however for most distributions if the -dev or -devel package and the cairo drivers are selected the other required packages will be installed as well.

The cmake scripts for gtk-fortran include a search for plplot's libraries and Fortran module files. If these are found, then the interface module and the examples will be build (Unless you explicitly prevent it by adding the -D EXCLUDE_PLPLOT=Y option to cmake.

The examples and module have mainly been tested using plplot version 5.9.9, but it is possible to get them to work with 5.9.7 by uncommenting the commented-out definitions in plplot_extra.f90. Version 5.9.5 (which is the version in Ubuntu 11.10) lacks a number of features needed to use the external cairo driver effectively.

The interface module.

Correctly configuring plplot's external cairo driver requires the use of the low-level pl_cmd routine to connect the cairo context to the driver. This routine is not available to the Fortran95 binding of plplot (at least in part because it requires Fortran 2003's c-binding capabilities, and also until gtk-fortran no-one had used cairo surfaces and contexts explicitly from Fortran).

The module plplot_extra provides an interface to the pl_cmd routine and also mnemonic definitions of the command codes.

The examples

Four examples from the plplot examples page have been adapted to work within gtk-fortran, these are, included in the plplot subdirectory of gtk-fortran.

  • Example 1: Four basic x-y plots in a 2x2 layout. These are embedded in a scrolled window. (hl_plplot1e.f90)
  • Example 8: A 3-D surface plot. With controls to set the options. The window can be resized. (hl_plplot8e.f90)
  • Example 17: A constantly updating strip plot. (hl_plplot17e.f90)
  • Example 30: A demonstration of gradients and transparency. (hl_plplot30.f90)

Elements of a plplot + gtk-fortran program

In the following section we will break down the first plplot example.

Globals

These variables are shared by all of the other modules and the main program.

module common_ex1
  use iso_c_binding      ! Enable the c-binding routines & constants.

  ! These are the gtk & glib routines used explicitly in the code.
  use gtk, only: gtk_button_new, gtk_container_add, gtk_drawing_area&
       &_new, gtk_main, gtk_main_quit, &
       & gtk_widget_show, gtk_widget_show_all, gtk_window_new, gtk_init
  use g, only: g_object_get_data

  ! These are the general high-level gtk routines and also the high-level
  ! drawing area modules.
  use gtk_draw_hl
  use gtk_hl

  ! This makes the low-level pl_cmd routine accessible
  use plplot_extra

  ! The size of the drawing area
  integer(kind=c_int) :: height, width

  ! The top-level window must be here as its destroy signal need not come from it.
  type(c_ptr) :: window
end module common_ex1

Main program

The main program creates the interface and does the initial draw of the plots.

program cairo_plplot_ex1

  use handlers_ex1              ! This gives the main program access to the signal handlers
  use plplot_code_ex1           ! This gives the main program access to the plotting code

  implicit none

  ! These widgets do not need to be explicitly accessed beyond the main program
  type(c_ptr) :: drawing, scroll_w, base, qbut

  ! Set the size of the drawing area (these are global variables)
  height = 1000
  width = 1200

  ! Initialize gtk
  call gtk_init()

  ! Create a top-level window and the pack a column box into it.
  window = hl_gtk_window_new("PLplot x01 / gtk-fortran (extcairo)"&
       & //c_null_char, &
       & delete_event = c_funloc(delete_cb))
  base = hl_gtk_box_new()
  call gtk_container_add(window, base)

  ! Create a drawing area, in a 600x500 scrolled window. 
  ! The high-level drawing area creator automatically adds a cairo surface as 
  ! backing store. Here we use the default expose/draw callback which 
  ! Just copies the backing store to the drawing surface.
  ! Pack it into the vertical box.
  drawing = hl_gtk_drawing_area_new(size=(/width, height/), &
       & has_alpha = FALSE, &
       & scroll = scroll_w, &
       & ssize=(/ 600, 500 /))
  call hl_gtk_box_pack(base, scroll_w)

  ! Add a quit button, and pack that into the box as well.
  qbut = hl_gtk_button_new("Quit"//c_null_char, clicked=c_funloc(quit_cb))
  call hl_gtk_box_pack(base, qbut, expand=FALSE)

  ! Display the widgets.
  call gtk_widget_show_all (window)

  ! Call the plotting routine.
  call x01f95(drawing)

  ! The event loop runs until it is exited.
  call gtk_main()

  print *, "All done"
end program cairo_plplot_ex1
Something went wrong with that request. Please try again.