在 Ubuntu 下用 Fortran 画图,常见做法有几种,从“简单可用”到“专业科研绘图”都有。下面按推荐程度给你梳理。
思路:Fortran 只负责算,把结果写到文件,gnuplot 负责画。
sudo apt update
sudo apt install gnuplot
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
gnuplot
在 gnuplot 里:
plot 'data.dat' with lines
或直接一行:
gnuplot -e "plot 'data.dat' with lines"
✅ 优点:简单、稳定、科研最常用
❌ 缺点:不是“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 内部画图”
❌ 库较老,文档偏旧
sudo apt install libplplot-dev plplot-driver-cairo
示例较复杂,适合需要出版级图形的用户。
如果你熟悉 Python,这是最现代的方案。
iso_c_binding 或写文件或直接:
fortran -> data.csv -> python matplotlib
| 方法 | 难度 | 推荐场景 |
|---|---|---|
| Fortran + gnuplot | ⭐ | 科研、快速出图 |
| DISLIN | ⭐⭐ | 纯 Fortran 项目 |
| PLplot | ⭐⭐⭐ | 专业绘图 |
| Fortran + Python | ⭐⭐ | 现代工作流 |
你可以告诉我:
我可以给你更具体的示例代码。