温馨提示×

ubuntu如何用fortran画图

小樊
35
2026-09-28 09:52:54
栏目: 智能运维

在 Ubuntu 下用 Fortran 画图,常见做法有几种,从“简单可用”到“专业科研绘图”都有。下面按推荐程度给你梳理。


一、最简单:Fortran 输出数据 + 用 gnuplot 画图(最常用)

思路:Fortran 只负责算,把结果写到文件,gnuplot 负责画。

1. 安装 gnuplot

sudo apt update
sudo apt install gnuplot

2. Fortran 示例(写数据)

program plot_example
  implicit none
  integer :: i
  real :: x, y

  open(10, file='data.dat')
  do i = 1, 100
    x = i * 0.1
    y = sin(x)
    write(10, *) x, y
  end do
  close(10)
end program plot_example

编译运行:

gfortran plot_example.f90 -o plot_example
./plot_example

3. 用 gnuplot 画图

gnuplot

在 gnuplot 里:

plot 'data.dat' with lines

或直接一行:

gnuplot -e "plot 'data.dat' with lines"

✅ 优点:简单、稳定、科研最常用
❌ 缺点:不是“Fortran 内部直接画图”


二、Fortran 直接画图(不依赖外部软件)

方案 1:DISLIN(老牌 Fortran 绘图库)

非常适合 Fortran 用户。

安装

sudo apt install dislin

(若源里没有,可从官网手动安装)

示例

program dislin_demo
  use dislin
  implicit none
  real :: x(100), y(100)
  integer :: i

  do i = 1, 100
    x(i) = i * 0.1
    y(i) = sin(x(i))
  end do

  call disini()
  call graf(0.0, 10.0, 0.0, 1.0, -1.0, 1.0, -1.0, 1.0)
  call curve(x, y, 100)
  call disfin()
end program dislin_demo

编译:

gfortran demo.f90 -L/usr/lib -ldislin -o demo

✅ 真正“Fortran 内部画图”
❌ 库较老,文档偏旧


方案 2:PLplot(现代、支持 Fortran)

sudo apt install libplplot-dev plplot-driver-cairo

示例较复杂,适合需要出版级图形的用户。


三、进阶:Fortran + Python(matplotlib)

如果你熟悉 Python,这是最现代的方案。

方法

  1. Fortran 算数据
  2. 用 iso_c_binding 或写文件
  3. Python 读数据画图

或直接:

fortran -> data.csv -> python matplotlib

四、对比建议

方法 难度 推荐场景
Fortran + gnuplot ⭐ 科研、快速出图
DISLIN ⭐⭐ 纯 Fortran 项目
PLplot ⭐⭐⭐ 专业绘图
Fortran + Python ⭐⭐ 现代工作流

五、我可以继续帮你的方向

你可以告诉我:

  • 是 科研数据 还是 工程仿真?
  • 想画 2D / 3D / 动画?
  • 是否必须 Fortran 内部画图?

我可以给你更具体的示例代码。

0 踩