# Fortran(gnuplot) to python(matplotlib)

**URL:** <https://fortran-lang.discourse.group/t/fortran-gnuplot-to-python-matplotlib/9489>\
**Category:** Help\
**Created:** [March 26, 2025, 1:22pm UTC](https://fortran-lang.discourse.group/t/fortran-gnuplot-to-python-matplotlib/9489 "2025-03-26T13:22:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ali\_Hasan](https://avatars.discourse-cdn.com/v4/letter/a/bc8723/32.png) [@Ali\_Hasan](https://fortran-lang.discourse.group/u/Ali_Hasan)\
**Post date:** [March 26, 2025, 1:22pm UTC](https://fortran-lang.discourse.group/t/fortran-gnuplot-to-python-matplotlib/9489/1 "2025-03-26T13:22:17Z")

</div>

**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?

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [March 26, 2025, 2:13pm UTC](https://fortran-lang.discourse.group/t/fortran-gnuplot-to-python-matplotlib/9489/2 "2025-03-26T14:13:25Z")

</div>

I suggest looking at the package

> **[GitHub - jacobwilliams/pyplot-fortran: For generating plots from Fortran using Python's...](https://github.com/jacobwilliams/pyplot-fortran)**
>
> For generating plots from Fortran using Python's matplotlib.pyplot 📈

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

---

<div class="post-metadata">

**Author:** ![interkosmos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/interkosmos/32/296_2.png) [@interkosmos](https://fortran-lang.discourse.group/u/interkosmos)\
**Post date:** [March 26, 2025, 2:32pm UTC](https://fortran-lang.discourse.group/t/fortran-gnuplot-to-python-matplotlib/9489/3 "2025-03-26T14:32:49Z")

</div>

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](http://gnuplot.info/docs/loc22820.html) and load the source into Python as a Tk canvas:

```python
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()

```

---

<div class="post-metadata">

**Author:** ![urbanjost](https://avatars.discourse-cdn.com/v4/letter/u/0ea827/32.png) [@urbanjost](https://fortran-lang.discourse.group/u/urbanjost)\
**Post date:** [March 26, 2025, 9:40pm UTC](https://fortran-lang.discourse.group/t/fortran-gnuplot-to-python-matplotlib/9489/4 "2025-03-26T21:40:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jacobwilliams](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/jacobwilliams/32/10_2.png) [@jacobwilliams](https://fortran-lang.discourse.group/u/jacobwilliams)\
**Post date:** [March 26, 2025, 9:46pm UTC](https://fortran-lang.discourse.group/t/fortran-gnuplot-to-python-matplotlib/9489/5 "2025-03-26T21:46:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [April 1, 2025, 10:23am UTC](https://fortran-lang.discourse.group/t/fortran-gnuplot-to-python-matplotlib/9489/6 "2025-04-01T10:23:45Z")

</div>

> [@interkosmos](#):
>
> `exec(open('plot.py').read())`

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

---

<div class="post-metadata">

**Author:** ![interkosmos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/interkosmos/32/296_2.png) [@interkosmos](https://fortran-lang.discourse.group/u/interkosmos)\
**Post date:** [April 1, 2025, 5:45pm UTC](https://fortran-lang.discourse.group/t/fortran-gnuplot-to-python-matplotlib/9489/7 "2025-04-01T17:45:14Z")

</div>

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

```auto
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`:

```auto
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`

```auto
$ gnuplot plot.gp

```

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

```python
#!/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:

```auto
$ python3 ui.py

```

 ![gnuplot](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/1/15dd57f4a90a697582e5ecd475ed29d4cc317cd4.png)

---

<div class="post-metadata">

**Author:** ![Bhanu](https://avatars.discourse-cdn.com/v4/letter/b/eb9ed0/32.png) [@Bhanu](https://fortran-lang.discourse.group/u/Bhanu)\
**Post date:** [April 1, 2025, 6:14pm UTC](https://fortran-lang.discourse.group/t/fortran-gnuplot-to-python-matplotlib/9489/8 "2025-04-01T18:14:39Z")

</div>

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.
