Fortran(gnuplot) to python(matplotlib)

Objective:
I want to create a GUI desktop application using Python that integrates with a Fortran 90 command-line program via ctypes. My Fortran program generates diagrams using Gnuplot, but currently, Gnuplot opens the plot in a separate window.

Problem:

  • When running the Fortran program, Gnuplot pops up in a separate window to display the diagram.
  • This behavior will persist when I call the Fortran function in Python, and later in my GUI, Gnuplot will still open a new window.
  • I want the diagram to be displayed inside my GUI, not in an external window.

Requirements:
Ensure the generated diagram can be embedded directly into the GUI

Additional Info:

  • I am new to Fortran and not an advanced Python developer, so I need clear guidance.

What is the best approach to achieve this?

I suggest looking at the package

although some additional code may be needed to get what you want.

1 Like

Just write the generated plot to a file first, then load and display the image in your GUI, depending on the Toolkit you’re using. For Tkinter, you could select the Gnuplot terminal tkcanvas and load the source into Python as a Tk canvas:

from tkinter import *
from tkinter import font

root = Tk()
c = Canvas(root, width=800, height=600)
c.pack()

exec(open('plot.py').read())
gnuplot(c)
root.mainloop()
1 Like

FYI: gnuplot can connect to previously existing X11 windows. See “help terminal x11” in gnuplot.
There are a few tools around for reading canvas and svg output as well, which gnuplot can generate.

It’s easy to embed matplotlib windows into Python/Pyside6 GUIs so that may be another option if you wanted to switch to using matplotlib.

could you explain this command? What is ‘plot.py’ file? The output from gnuplot using tkcanvas terminal is not Python.

If you set the Gnuplot output terminal to tkcanvas python, like:

set terminal tkcanvas python

then Gnuplot will write the plot as Python source, for instance, to file plot.py. In your Python GUI, evaluate the generated script with exec(open('plot.py').read()) if plot.py is in the same directory, execute the procedure gnuplot(c), and show the Tkinter window with root.mainloop().

Example Gnuplot script plot.gp:

set terminal tkcanvas python
set output "plot.py"
set object 1 rectangle from screen 0,0 to screen 1,1 fillcolor rgbcolor "white" behind
set key fixed left top vertical Right noreverse enhanced autotitle box lt black linewidth 1.000 dashtype solid
set samples 200, 200
set title "Simple Plots"
set title font ",20" textcolor lt -1 norotate
set colorbox vertical origin screen 0.9, 0.2 size screen 0.05, 0.6 front  noinvert bdefault
plot [-3:5] asin(x),acos(x)

Creating the plot file plot.py

$ gnuplot plot.gp

Basic Tkinter script ui.py that imports plot.py:

#!/usr/bin/env python3

from tkinter import *
from tkinter import font

root = Tk()
c = Canvas(root, width=800, height=600)
c.pack()

exec(open('plot.py').read())
gnuplot(c)
root.mainloop()

Showing the window:

$ python3 ui.py

1 Like

WoW, probably I believe I am a bit qualified to answer this question based on my previous experience with the software I developed during my PhD that uses GUI for scientific plotting (that is, real-time plotting including line plot, surface mesh plot, and vector array plotting for 2D and 3D plotting) and user input and at the backend I used C++ based program (these days I am converting that c++ program to Fortran through stdlib ). Yes, you can do this.

For this, I developed pyqt for GUI front-end development and passed the argument to the C++ code. Once the process is completed, I use intermediate files that store the data that needs to be plotted. Let me know if you need more help.