0%

通过调用PGSWIN来设置window属性。

1
PGSWIN (1975.0, 1984.0, 5.0, 20.0)

X轴从1975到1984

Y轴从5到20

PGPLOT 集大成函数PGENV

这里将上面几个小部分提到的函数全部展示一下。

其实调用PGPAGE、PGSVP、 PGSWIN和 PGBOX 是比较不方便的方法,虽然可以随心所欲的展示一些成果。

这四个函数也可用通过一个集成的函数来搞定,即PGENV (for PGPLOT ENVironment)

比如

  • PGENV (1975.0, 1984.0, 5.0, 20.0, 0, 0)

与下面四个函数的效果是相同的

  • CALL PGPAGE
  • CALL PGVSTD
  • CALL PGSWIN (1975.0, 1984.0, 5.0, 20.0)
  • CALL PGBOX (‘BCNST’, 0.0, 0, ‘BCNST’, 0.0, 0)

默认的视口输出居中并留有一定的余量用于注解标记。

但是如果想改变这些默认设置,可以使用pgvport(比例象限)、pgvsize(实际尺寸)等函数。这些函数都会在后面有详细介绍。

本教程既是PGPLOT的入门教程也是参考手册。

入门教程:介绍了如何使用PGPLOT

➢ 参考手册:关于PGPLOT的所有函数的定义和使用实例。
➢ 第二章:入门指导,列举了一个最简单的绘图程序
➢ 第三章:基本特性
➢ 第四章:介绍四种组成图像的基元:线条、图标记、文字、区域填充
➢ 第五章:如何改变基元的属性,比如颜色、线条类型、字体等
➢ 第六章:描述了一些经过包装的高级程序及函数
➢ 第七章:PGPLOT图像交互式的能力
➢ 第八章:图元文件的使用

 附录A:PGPLOT所有的函数原型
 附录B:PGPLOT注释可以用的字符和符号
 附录C:PGPLOT不同平台的安装
 附录D:PGPLOT支持的设备细节介绍
 附录E:扩展PGPLOT以支持其他设备的说明
 附录F:C语言中如何调用PGPLOT

PGPLOT设备系统详解

PGPLOT支持多种输出设备,每种设备都有其特定的用途和特点。理解设备系统是使用PGPLOT的基础,它决定了图形的输出方式和质量。

设备分类概述

PGPLOT设备可以分为两大类:

1. 硬拷贝输出设备

产生永久性文件输出的设备,如PostScript、PNG、PDF等格式文件。

2. 交互式显示设备

在显示器上实时显示图形的设备,如X11窗口、终端等。

设备命名规范

基本格式

1
device_name(file_name)/device_type

参数说明

  • device_name:设备名称标识符
  • file_name:输出文件名(可选)
  • device_type:设备类型说明

命名示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 基本设备选择
"/XWINDOW" // X11窗口显示
"/PS" // PostScript文件输出
"/PNG" // PNG图像文件输出

// 指定文件名
"/PS:my_plot.ps" // 输出到指定PostScript文件
"/PNG:graph.png" // 输出到指定PNG文件
"/PDF:report.pdf" // 输出到指定PDF文件

// 设备参数
"/PS:landscape" // 横向PostScript输出
"/PNG:transparent" // 透明背景PNG
"/XWINDOW:1" // 指定X11窗口编号

交互式设备详解

X11窗口设备

/XWINDOW

功能:标准的X11图形窗口
特点

  • 支持鼠标和键盘交互
  • 实时图形更新
  • 可调整窗口大小
  • 支持多窗口

使用示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cpgplot.h>

int main() {
// 启动X11窗口设备
if (cpgbeg(0, "/XWINDOW", 1, 1) != 1) {
printf("Failed to start X11 device\n");
return 1;
}

// 设置坐标轴
cpgenv(0.0, 10.0, 0.0, 10.0, 0, 1);

// 绘制一些数据
float x[] = {1, 2, 3, 4, 5};
float y[] = {1, 4, 9, 16, 25};
cpgline(5, x, y);

cpgend();
return 0;
}

/XSERVE

功能:持久性X11窗口
特点

  • 窗口在程序结束后保持显示
  • 支持多次绘图到同一窗口
  • 适合调试和演示

使用示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <cpgplot.h>

int main() {
// 启动持久性X11窗口
if (cpgbeg(0, "/XSERVE", 1, 1) != 1) {
printf("Failed to start XSERVE device\n");
return 1;
}

// 第一次绘图
cpgenv(0.0, 10.0, 0.0, 10.0, 0, 1);
cpgtext(5.0, 5.0, "First Plot");

// 程序结束,窗口保持显示
cpgend();
return 0;
}

终端设备

/TEXT

功能:文本终端输出
特点

  • 使用ASCII字符绘制图形
  • 适合无图形环境的系统
  • 支持键盘输入

/VTERM

功能:VT100兼容终端
特点

  • 支持ANSI转义序列
  • 彩色文本输出
  • 跨平台兼容性

文件输出设备详解

PostScript设备

/PS

功能:标准PostScript文件输出
特点

  • 高质量矢量图形
  • 适合打印和出版
  • 文件大小相对较小
  • 支持缩放而不失真

配置选项

1
2
3
4
5
6
7
8
9
10
11
// 基本PostScript输出
cpgbeg(0, "/PS", 1, 1);

// 指定文件名
cpgbeg(0, "/PS:my_plot.ps", 1, 1);

// 横向输出
cpgbeg(0, "/PS:landscape", 1, 1);

// 彩色输出
cpgbeg(0, "/CPS", 1, 1);

使用示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <cpgplot.h>

int main() {
// 启动PostScript设备
if (cpgbeg(0, "/PS:output.ps", 1, 1) != 1) {
printf("Failed to start PS device\n");
return 1;
}

// 设置纸张大小
cpgpap(8.0, 1.0); // 8英寸宽,1:1比例

// 绘制图形
cpgenv(0.0, 10.0, 0.0, 10.0, 0, 1);
cpgbox("BC", 0.0, 0, "BC", 0.0, 0);

// 添加标题
cpglab("X Axis", "Y Axis", "PostScript Output");

// 绘制数据
float x[] = {1, 2, 3, 4, 5};
float y[] = {1, 4, 9, 16, 25};
cpgline(5, x, y);

cpgend();
printf("PostScript file 'output.ps' created successfully\n");
return 0;
}

/VPS

功能:纵向PostScript输出
特点

  • 适合纵向打印
  • 标准A4纸张格式
  • 适合报告和论文

/CPS

功能:彩色PostScript输出
特点

  • 支持彩色图形
  • 适合彩色打印
  • 文件大小较大

图像文件设备

/PNG

功能:PNG图像文件输出
特点

  • 无损压缩格式
  • 支持透明背景
  • 适合网页和文档
  • 文件大小适中

配置选项

1
2
3
4
5
6
7
8
9
10
11
// 基本PNG输出
cpgbeg(0, "/PNG", 1, 1);

// 指定文件名
cpgbeg(0, "/PNG:graph.png", 1, 1);

// 透明背景
cpgbeg(0, "/TPNG", 1, 1);

// 指定分辨率
cpgbeg(0, "/PNG:high_res.png", 1, 1);

使用示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cpgplot.h>

int main() {
// 启动PNG设备
if (cpgbeg(0, "/PNG:output.png", 1, 1) != 1) {
printf("Failed to start PNG device\n");
return 1;
}

// 设置视图大小(像素)
cpgpap(800.0, 600.0); // 800x600像素

// 绘制图形
cpgenv(0.0, 10.0, 0.0, 10.0, 0, 1);
cpgbox("BC", 0.0, 0, "BC", 0.0, 0);

// 绘制彩色数据
cpgsci(2); // 红色
cpgline(5, (float[]){1,2,3,4,5}, (float[]){1,4,9,16,25});

cpgend();
printf("PNG file 'output.png' created successfully\n");
return 0;
}

/GIF

功能:GIF图像文件输出
特点

  • 支持动画(多帧)
  • 文件大小较小
  • 适合网页使用
  • 颜色数量有限

/TPNG

功能:透明背景PNG输出
特点

  • 支持透明背景
  • 适合叠加到其他图像上
  • 适合网页设计

其他文件格式

/PDF

功能:PDF文档输出
特点

  • 现代文档标准
  • 支持矢量图形
  • 跨平台兼容
  • 适合电子文档

/LATEX

功能:LaTeX图形环境输出
特点

  • 适合学术论文
  • 与LaTeX文档集成
  • 支持数学公式
  • 高质量输出

特殊设备

/NULL

功能:空设备,无输出
用途

  • 性能测试
  • 调试模式
  • 批量处理

/MEM

功能:内存设备
用途

  • 程序内部图形处理
  • 图形数据提取
  • 自定义输出处理

设备配置和优化

纸张设置

设置纸张大小

1
2
3
4
5
6
7
8
9
#include <cpgplot.h>

void set_paper_size() {
// 设置纸张宽度(英寸)
cpgpap(8.5, 1.0); // 8.5英寸宽,1:1比例

// 或者设置高度
cpgpap(11.0, 8.5/11.0); // 11英寸高,8.5:11比例
}

设置视图大小

1
2
3
4
5
6
7
8
9
#include <cpgplot.h>

void set_viewport_size() {
// 设置视图(设备坐标)
cpgsvp(0.1, 0.9, 0.1, 0.9); // 留出10%边距

// 设置世界坐标窗口
cpgswin(0.0, 10.0, 0.0, 10.0);
}

设备特定配置

PostScript配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <cpgplot.h>

void configure_postscript() {
// 设置PostScript设备
if (cpgbeg(0, "/PS:output.ps", 1, 1) != 1) {
return;
}

// 设置纸张大小
cpgpap(8.5, 11.0/8.5); // 标准信纸大小

// 设置颜色
cpgsci(1); // 黑色

// 设置线宽
cpgslw(2); // 中等线宽
}

PNG配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <cpgplot.h>

void configure_png() {
// 设置PNG设备
if (cpgbeg(0, "/PNG:output.png", 1, 1) != 1) {
return;
}

// 设置分辨率
cpgpap(1024.0, 768.0); // 1024x768像素

// 设置背景色
cpgsci(0); // 黑色背景
cpgscr(0, 0.0, 0.0, 0.0); // 设置颜色0为黑色
}

设备选择和最佳实践

选择标准

用途考虑

  • 屏幕显示:使用 /XWINDOW/XSERVE
  • 打印输出:使用 /PS/PDF
  • 网页使用:使用 /PNG/GIF
  • 学术论文:使用 /PS/LATEX
  • 演示文稿:使用 /PNG/PDF

质量要求

  • 最高质量:PostScript或PDF
  • 中等质量:PNG
  • 快速预览:X11窗口
  • 文件大小:GIF或压缩PNG

性能优化

批量处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cpgplot.h>

void batch_plotting() {
// 启动设备
if (cpgbeg(0, "/PS:batch_output.ps", 1, 1) != 1) {
return;
}

// 批量绘制多个图形
for (int i = 0; i < 10; i++) {
// 绘制第i个图形
cpgpage(); // 新页面

// ... 绘图代码 ...

// 添加页码
char title[50];
sprintf(title, "Page %d", i + 1);
cpgtext(5.0, 9.5, title);
}

cpgend();
}

内存管理

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cpgplot.h>

void memory_efficient_plotting() {
// 使用较小的视图
cpgsvp(0.15, 0.85, 0.15, 0.85);

// 避免频繁的属性切换
cpgsci(1);
cpgslw(1);

// 批量绘制数据
// ... 绘图代码 ...
}

常见问题和解决方案

设备启动失败

问题诊断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <cpgplot.h>

void diagnose_device_issues() {
// 尝试启动设备
int result = cpgbeg(0, "/XWINDOW", 1, 1);

switch (result) {
case 1:
printf("Device started successfully\n");
break;
case 0:
printf("Device not available\n");
break;
case -1:
printf("Device error occurred\n");
break;
default:
printf("Unknown error: %d\n", result);
break;
}
}

常见解决方案

  1. X11设备问题

    • 检查DISPLAY环境变量
    • 确认X11服务器运行
    • 检查权限设置
  2. 文件设备问题

    • 检查目录权限
    • 确认磁盘空间
    • 验证文件路径
  3. 库文件问题

    • 检查PGPLOT安装
    • 确认库文件路径
    • 验证依赖库

输出质量问题

PostScript优化

1
2
3
4
5
6
7
8
9
10
11
12
#include <cpgplot.h>

void optimize_postscript() {
// 使用高分辨率设置
cpgpap(8.5, 11.0/8.5);

// 设置高质量线型
cpgslw(1); // 细线

// 使用矢量字体
cpgscf(1); // 标准字体
}

PNG优化

1
2
3
4
5
6
7
8
9
10
11
12
#include <cpgplot.h>

void optimize_png() {
// 使用高分辨率
cpgpap(1200.0, 800.0);

// 设置抗锯齿
cpgslw(1);

// 优化颜色设置
cpgsci(1);
}

设备扩展和自定义

环境变量配置

设置默认设备

1
2
3
4
# 在 ~/.bashrc 或 ~/.zshrc 中添加
export PGPLOT_DEV=/XWINDOW
export PGPLOT_PS_DEV="my_plot.ps"
export PGPLOT_PNG_DEV="my_plot.png"

运行时设备选择

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cpgplot.h>
#include <stdlib.h>

void select_device_from_env() {
const char *device = getenv("PGPLOT_DEV");
if (!device) {
device = "/XWINDOW"; // 默认设备
}

if (cpgbeg(0, device, 1, 1) != 1) {
printf("Failed to start device: %s\n", device);
return;
}

// ... 绘图代码 ...
}

设备参数配置

自定义设备参数

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cpgplot.h>

void custom_device_config() {
// 启动设备并指定参数
if (cpgbeg(0, "/PS:custom.ps:landscape:color", 1, 1) != 1) {
return;
}

// 应用自定义设置
cpgpap(11.0, 8.5/11.0); // 横向纸张

// ... 绘图代码 ...
}

总结

PGPLOT的设备系统提供了灵活的输出选择,从交互式显示到高质量文件输出。选择合适的设备对于创建满足需求的图形至关重要。

关键要点

  1. 设备分类:理解交互式和文件输出设备的区别
  2. 命名规范:掌握设备命名格式和参数
  3. 配置优化:根据需求配置设备参数
  4. 性能考虑:选择适合的设备和优化策略
  5. 问题解决:掌握常见问题的诊断和解决方法

推荐实践

  • 开发阶段:使用 /XWINDOW 进行交互式调试
  • 最终输出:根据用途选择高质量文件格式
  • 批量处理:使用文件设备进行自动化绘图
  • 质量优先:重要图形使用PostScript或PDF格式
  • 兼容性:考虑目标平台和软件的支持情况

通过合理选择和配置设备,您将能够创建出满足各种需求的高质量图形输出。

PGPLOT环境配置详解

PGPLOT的正确运行需要适当的环境配置。本文档详细介绍如何设置环境变量、配置系统路径,以及解决常见的环境配置问题。

环境变量概述

核心环境变量

PGPLOT主要依赖以下环境变量:

变量名 用途 默认值 必需性
PGPLOT_DIR PGPLOT库文件路径 必需
PGPLOT_DEV 默认输出设备 推荐
LD_LIBRARY_PATH 动态库搜索路径 系统默认 必需
PGPLOT_FONT 字体文件路径 系统默认 可选
PGPLOT_DRIVER_PATH 设备驱动路径 系统默认 可选

系统特定变量

不同操作系统可能需要额外的环境变量:

系统 额外变量 说明
Linux LD_LIBRARY_PATH 动态库路径
macOS DYLD_LIBRARY_PATH 动态库路径
Windows PATH 可执行文件和库路径
Solaris LD_LIBRARY_PATH 动态库路径

详细配置说明

1. PGPLOT_DIR

作用:指定PGPLOT库文件和头文件的安装目录

设置方法

1
2
3
4
5
# 在 ~/.bashrc 或 ~/.zshrc 中添加
export PGPLOT_DIR=/usr/local/pgplot

# 或者在 ~/.profile 中添加
export PGPLOT_DIR=/usr/local/pgplot

常见路径

1
2
3
4
5
6
7
# 系统安装
export PGPLOT_DIR=/usr/lib/pgplot
export PGPLOT_DIR=/usr/local/lib/pgplot

# 用户安装
export PGPLOT_DIR=$HOME/pgplot
export PGPLOT_DIR=/opt/pgplot

验证设置

1
2
3
4
5
6
7
8
# 检查变量是否设置
echo $PGPLOT_DIR

# 检查目录是否存在
ls -la $PGPLOT_DIR

# 检查库文件
ls -la $PGPLOT_DIR/lib*

2. PGPLOT_DEV

作用:设置PGPLOT的默认输出设备

设置方法

1
2
3
4
5
6
# 设置默认设备
export PGPLOT_DEV=/XWINDOW

# 或者设置文件输出设备
export PGPLOT_DEV=/PS
export PGPLOT_DEV=/PNG

常用设备设置

1
2
3
4
5
6
7
8
9
10
11
# 开发环境
export PGPLOT_DEV=/XWINDOW

# 生产环境
export PGPLOT_DEV=/PS

# 网页输出
export PGPLOT_DEV=/PNG

# 学术论文
export PGPLOT_DEV=/PDF

3. LD_LIBRARY_PATH

作用:指定动态链接库的搜索路径

设置方法

1
2
3
4
5
# 添加到现有路径
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PGPLOT_DIR

# 或者完全替换
export LD_LIBRARY_PATH=$PGPLOT_DIR:$LD_LIBRARY_PATH

系统特定设置

Linux系统

1
2
3
4
5
6
# 在 ~/.bashrc 中添加
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/pgplot

# 或者添加到系统级配置
echo "/usr/local/pgplot" | sudo tee -a /etc/ld.so.conf.d/pgplot.conf
sudo ldconfig

macOS系统

1
2
3
# 在 ~/.zshrc 或 ~/.bash_profile 中添加
export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:/usr/local/pgplot
export DYLD_FRAMEWORK_PATH=$DYLD_FRAMEWORK_PATH:/usr/local/pgplot

Windows系统

1
2
3
# 在系统环境变量中添加
set PATH=%PATH%;C:\pgplot
set PGPLOT_DIR=C:\pgplot

完整配置示例

基础配置

在 ~/.bashrc 中的配置

1
2
3
4
5
6
7
8
# PGPLOT环境配置
export PGPLOT_DIR=/usr/local/pgplot
export PGPLOT_DEV=/XWINDOW
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PGPLOT_DIR

# 可选配置
export PGPLOT_FONT=$PGPLOT_DIR/fonts
export PGPLOT_DRIVER_PATH=$PGPLOT_DIR/drivers

在 ~/.zshrc 中的配置

1
2
3
4
5
6
7
8
# PGPLOT环境配置
export PGPLOT_DIR=/usr/local/pgplot
export PGPLOT_DEV=/XWINDOW
export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:$PGPLOT_DIR

# 可选配置
export PGPLOT_FONT=$PGPLOT_DIR/fonts
export PGPLOT_DRIVER_PATH=$PGPLOT_DIR/drivers

高级配置

多版本PGPLOT支持

1
2
3
4
5
6
7
8
9
10
# 支持多个PGPLOT版本
export PGPLOT_DIR_5_2=/usr/local/pgplot-5.2
export PGPLOT_DIR_5_3=/usr/local/pgplot-5.3

# 默认使用最新版本
export PGPLOT_DIR=$PGPLOT_DIR_5_3

# 快速切换版本
alias pgplot52='export PGPLOT_DIR=$PGPLOT_DIR_5_2'
alias pgplot53='export PGPLOT_DIR=$PGPLOT_DIR_5_3'

开发环境配置

1
2
3
4
5
6
7
8
# 开发环境配置
export PGPLOT_DEV=/XWINDOW
export PGPLOT_DEBUG=1
export PGPLOT_LOG_LEVEL=DEBUG

# 生产环境配置
alias pgplot-prod='export PGPLOT_DEV=/PS && export PGPLOT_DEBUG=0'
alias pgplot-dev='export PGPLOT_DEV=/XWINDOW && export PGPLOT_DEBUG=1'

配置文件管理

系统级配置

/etc/profile 或 /etc/environment

1
2
3
# 系统级PGPLOT配置
export PGPLOT_DIR=/usr/local/pgplot
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PGPLOT_DIR

/etc/ld.so.conf.d/

1
2
3
4
5
6
7
# 创建配置文件
sudo tee /etc/ld.so.conf.d/pgplot.conf << EOF
/usr/local/pgplot
EOF

# 更新库缓存
sudo ldconfig

用户级配置

创建专用配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 创建 ~/.pgplotrc 文件
cat > ~/.pgplotrc << 'EOF'
# PGPLOT用户配置文件
export PGPLOT_DIR=$HOME/pgplot
export PGPLOT_DEV=/XWINDOW
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PGPLOT_DIR

# 自定义设置
export PGPLOT_COLOR_SCHEME=default
export PGPLOT_DEFAULT_FONT=1
EOF

# 在 ~/.bashrc 中加载
echo "source ~/.pgplotrc" >> ~/.bashrc

使用别名和函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 在 ~/.bashrc 中添加
pgplot-env() {
export PGPLOT_DIR=/usr/local/pgplot
export PGPLOT_DEV=/XWINDOW
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PGPLOT_DIR
echo "PGPLOT environment configured"
}

pgplot-check() {
echo "PGPLOT_DIR: $PGPLOT_DIR"
echo "PGPLOT_DEV: $PGPLOT_DEV"
echo "LD_LIBRARY_PATH: $LD_LIBRARY_PATH"
echo "Library files:"
ls -la $PGPLOT_DIR/lib* 2>/dev/null || echo "No library files found"
}

环境验证和测试

基本验证

检查环境变量

1
2
3
4
5
6
7
8
9
10
# 检查所有PGPLOT相关变量
env | grep PGPLOT

# 检查库路径
echo $LD_LIBRARY_PATH
echo $DYLD_LIBRARY_PATH

# 检查PGPLOT目录
echo $PGPLOT_DIR
ls -la $PGPLOT_DIR

验证库文件

1
2
3
4
5
6
7
8
9
10
11
# 检查库文件是否存在
ls -la $PGPLOT_DIR/libpgplot*
ls -la $PGPLOT_DIR/libcpgplot*

# 检查库文件权限
file $PGPLOT_DIR/libpgplot.so
file $PGPLOT_DIR/libcpgplot.so

# 检查库文件依赖
ldd $PGPLOT_DIR/libpgplot.so
ldd $PGPLOT_DIR/libcpgplot.so

功能测试

编译测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 创建测试程序
cat > test_pgplot.c << 'EOF'
#include <cpgplot.h>
#include <stdio.h>

int main() {
printf("PGPLOT header found successfully\n");
return 0;
}
EOF

# 编译测试
gcc -o test_pgplot test_pgplot.c -lcpgplot -lpgplot -lX11

# 检查编译结果
if [ $? -eq 0 ]; then
echo "Compilation successful"
else
echo "Compilation failed"
fi

运行时测试

1
2
3
4
5
6
7
8
# 运行测试程序
./test_pgplot

# 检查库链接
ldd ./test_pgplot

# 检查符号解析
nm ./test_pgplot | grep cpg

常见问题和解决方案

1. 找不到库文件

错误信息

1
error while loading shared libraries: libpgplot.so: cannot open shared object file

解决方案

1
2
3
4
5
6
7
8
9
10
# 检查库文件路径
find /usr -name "libpgplot.so" 2>/dev/null
find /usr/local -name "libpgplot.so" 2>/dev/null

# 更新库缓存
sudo ldconfig

# 检查环境变量
echo $LD_LIBRARY_PATH
echo $PGPLOT_DIR

2. 找不到头文件

错误信息

1
fatal error: cpgplot.h: No such file or directory

解决方案

1
2
3
4
5
6
7
8
9
10
11
# 查找头文件
find /usr -name "cpgplot.h" 2>/dev/null
find /usr/local -name "cpgplot.h" 2>/dev/null

# 安装开发包
sudo apt-get install libpgplot5-dev # Ubuntu/Debian
sudo yum install pgplot-devel # CentOS/RHEL

# 设置包含路径
export C_INCLUDE_PATH=$C_INCLUDE_PATH:$PGPLOT_DIR/include
export CPP_INCLUDE_PATH=$CPP_INCLUDE_PATH:$PGPLOT_DIR/include

3. 设备启动失败

错误信息

1
Failed to start PGPLOT device

解决方案

1
2
3
4
5
6
7
8
9
# 检查设备支持
ls $PGPLOT_DIR/drivers/

# 检查环境变量
echo $PGPLOT_DEV
echo $DISPLAY

# 测试设备
cpgdemo # 如果可用

4. 权限问题

错误信息

1
Permission denied

解决方案

1
2
3
4
5
6
7
8
9
10
# 检查文件权限
ls -la $PGPLOT_DIR

# 修复权限
sudo chmod 755 $PGPLOT_DIR
sudo chmod 644 $PGPLOT_DIR/lib*

# 检查用户权限
whoami
groups

性能优化配置

库路径优化

优化库搜索顺序

1
2
3
4
5
# 将最常用的路径放在前面
export LD_LIBRARY_PATH=/usr/local/pgplot:/usr/lib:$LD_LIBRARY_PATH

# 使用绝对路径
export LD_LIBRARY_PATH=/usr/local/pgplot:/usr/lib:/usr/local/lib

缓存优化

1
2
3
4
5
# 更新库缓存
sudo ldconfig

# 检查缓存状态
ldconfig -p | grep pgplot

环境变量优化

减少环境变量查找

1
2
3
4
5
# 使用绝对路径
export PGPLOT_DIR=/usr/local/pgplot

# 避免相对路径
# export PGPLOT_DIR=../pgplot # 不推荐

批量设置

1
2
3
4
5
6
7
8
# 一次性设置所有变量
pgplot-setup() {
export PGPLOT_DIR=/usr/local/pgplot
export PGPLOT_DEV=/XWINDOW
export LD_LIBRARY_PATH=$PGPLOT_DIR:$LD_LIBRARY_PATH
export PGPLOT_FONT=$PGPLOT_DIR/fonts
export PGPLOT_DRIVER_PATH=$PGPLOT_DIR/drivers
}

跨平台配置

Linux配置

1
2
3
4
# 在 ~/.bashrc 中
export PGPLOT_DIR=/usr/local/pgplot
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PGPLOT_DIR
export PGPLOT_DEV=/XWINDOW

macOS配置

1
2
3
4
# 在 ~/.zshrc 中
export PGPLOT_DIR=/usr/local/pgplot
export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:$PGPLOT_DIR
export PGPLOT_DEV=/XWINDOW

Windows配置

1
2
3
4
# 在系统环境变量中
set PGPLOT_DIR=C:\pgplot
set PATH=%PATH%;%PGPLOT_DIR%
set PGPLOT_DEV=/XWINDOW

最佳实践

1. 配置管理

  • 使用版本控制管理配置文件
  • 为不同环境创建不同的配置
  • 定期备份和更新配置

2. 环境隔离

  • 使用虚拟环境隔离依赖
  • 避免全局环境变量污染
  • 使用项目特定的配置

3. 错误处理

  • 在脚本中检查环境变量
  • 提供清晰的错误信息
  • 实现自动修复机制

4. 文档化

  • 记录所有环境变量设置
  • 说明配置的目的和影响
  • 提供配置示例和模板

总结

正确的环境配置是PGPLOT成功运行的基础。通过合理设置环境变量、配置库路径,并遵循最佳实践,您可以确保PGPLOT在各种环境下都能正常工作。

关键要点

  1. 必需变量PGPLOT_DIRLD_LIBRARY_PATH 是必需的
  2. 推荐变量PGPLOT_DEV 可以简化设备选择
  3. 系统差异:不同操作系统需要不同的配置方法
  4. 验证测试:配置完成后要进行编译和运行测试
  5. 问题诊断:掌握常见问题的诊断和解决方法

配置检查清单

  • 设置 PGPLOT_DIR 环境变量
  • 配置 LD_LIBRARY_PATHDYLD_LIBRARY_PATH
  • 设置 PGPLOT_DEV 默认设备
  • 验证库文件和头文件路径
  • 测试编译和运行
  • 检查设备支持
  • 配置字体和驱动路径(可选)

通过系统性的环境配置,您将能够充分利用PGPLOT的强大功能,创建出高质量的科学图表和可视化应用。

本章节介绍了用PGPLOT创建一个图像的基础子程序,并有一些示例程序。

一个图像主要有下面几个部分组成:

  1. 坐标轴
  2. 标记
  3. 点或线

在使用PGPLOT画图的时候,至少要用到4个PGPLOT的子程序。

  1. PGBEGIN:开启PGPLOT并制定输出设备
  2. PGENV:设定图像的坐标轴范围、标签等
  3. PGPOINT/PGLINE:画点或线
  4. PGEND:关闭画图程序

当然,如果在一个设备上想画很多的图形,只需要重复23即可。而对于14,除非想在不同的设备上输出,否则只调用一次就够了。

编译并运行程序

在编译并链接好程序后,执行程序是会提示:

Graphics device/type (? to see list, default /Xserve):

这里我们输入“?”,查看一下可用的设备,我的如下所示,这个设备列表与安装时修改的drives.list相关联。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Graphics device/type (? to see list, default /Xserve): ?
PGPLOT v5.2.2 Copyright 1997 California Institute of Technology
Interactive devices:
/XWINDOW (X window window@node:display.screen/xw)
/XSERVE (A /XWINDOW window that persists for re-use)
Non-interactive file formats:
/GIF (Graphics Interchange Format file, landscape orientation)
/VGIF (Graphics Interchange Format file, portrait orientation)
/LATEX (LaTeX picture environment)
/NULL (Null device, no output)
/PNG (Portable Network Graphics file)
/TPNG (Portable Network Graphics file - transparent background)
/PS (PostScript file, landscape orientation)
/VPS (PostScript file, portrait orientation)
/CPS (Colour PostScript file, landscape orientation)
/VCPS (Colour PostScript file, portrait orientation)
Graphics device/type (? to see list, default /Xserve):

默认输出之屏幕,如上述图像所有,然后我选择/PS,就会发现在可执行程序相同目录中多了一个文件PGPLOT.ps文件(这个对于科研人员使用Latex调用图比较方便)。