You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用gnuplot将向量(1×N或N×1矩阵)绘制成图像?

gnuplot绘制N×1或1×N矩阵图像的解决方法

问题背景

可正常绘制2×2矩阵图像,代码如下:

plot '-' matrix with image
1 2 
3 4 
e

但绘制2×1或1×2向量时,执行以下代码会失败:

# 2×1向量示例
plot '-' matrix with image
1 
3 
e

# 1×2向量示例
plot '-' matrix with image
1 2
e

会收到警告:

Image grid must be at least 4 points (2 x 2).

且无图像渲染。

解决方法

方法1:补全为2×2矩阵(简单快速)

通过重复行/列的方式,将向量补全为gnuplot要求的最小2×2矩阵,视觉上仍呈现一维色块带:

  • 1×2向量补全示例:
plot '-' matrix with image
1 2
1 2  # 重复第一行,凑成2×2矩阵
e
  • 2×1向量补全示例:
plot '-' matrix with image
1 1  # 重复第一列,凑成2×2矩阵
3 3
e

方法2:手动指定坐标绘制(灵活可控)

放弃matrix模式,直接通过using子句定义每个色块的坐标范围,用with image或with boxes实现相同视觉效果:

  • 1×2向量用with image绘制:
set xrange [0:2]
set yrange [0:1]
# 格式:x起始, y起始, x结束, y结束, 颜色值
plot '-' using 1:2:3:4:5 with image
0 0 1 1 1
1 0 2 1 2
e
  • 2×1向量用with image绘制:
set xrange [0:1]
set yrange [0:2]
plot '-' using 1:2:3:4:5 with image
0 0 1 1 1
0 1 1 2 3
e
  • 用with boxes简化一维向量绘制:
# 1×2向量示例
set boxwidth 1
plot '-' using 1:2 with boxes lc palette
1 1
2 2
e

内容的提问来源于stack exchange,提问作者user16372530

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 05:33:23