【问题标题】:Gnuplot multiplot: Convenient method for creating more complex layoutsGnuplot multiplot:创建更复杂布局的便捷方法
【发布时间】:2013-04-09 14:29:40
【问题描述】:

我想用gnuplot按以下方式放置几个图:

 +------------------------+
 |plot1                   |
 |                        |
 +------------------------+
 +----------++------------+
 |plot2     ||plot3       |
 |          ||            |
 +----------++------------+
 +----------++------------+
 |plot4     ||plot5       |
 |          ||            |
 +----------++------------+

可以通过set multiplot layout n,m 实现简单的nxm 布局(请参阅这些demos 以获取官方网站上的说明)。

matplotlib 提供了更多高级功能,您可以在文档中看到:Customizing Location of Subplot Using GridSpec

在 gnuplot 中,我使用了 set originset size 来实现这一点。但是,它相当繁琐,当您发现以后要更改布局时,需要重新计算大小和位置。

备注1: 通常,将边距设置为固定大小以实现正确的绘图大小也很有用。通过改变 xlabels 等进行的自动计算将使实现正确布局变得更加困难。

备注2: gnuplot 提供了另一种使用set [lrbt]margin <> at screen 设置绘图边界的可能性,而不是origin/size。用户必须确保标题和标签有足够的空间(请参阅demo)。仍然不是一个完美的解决方案,但有时更方便。

是否存在我不知道的可能性,或者是否存在用于创建布局的工具?

【问题讨论】:

  • 我会将绘图分别绘制到不同的文件中,然后使用不同的工具(例如 imagemagick)来创建布局。
  • 我不确定这是否真的是一个显着的改进,因为我必须通过set term ... size x,y 为我想要的布局调整至少所有大小的输出文件。改变布局也意味着重新调整 gnuplot 文件并再次运行 imagemagick, tex-file, ....
  • 是的,为重复的部分创建一个 Makefile :-)
  • 为了完整起见,给定布局的紧凑解决方案是使用 set multiplot layout 3,2 upwards 并仅设置最后一个绘图命令 (plot1) set size 1,1./3. 但我的问题当然是针对任意布局的。

标签: gnuplot


【解决方案1】:

评论中提到的给定布局的最小工作示例。

set term pngcairo size 5.0in,6.0in
set output "test_layout.png"
ROWS=3
COLS=2
set multiplot layout ROWS,COLS upwards
plot sin(4*x) # plot 4
plot sin(5*x) # plot 5
plot sin(2*x) # plot 2
plot sin(3*x) # plot 3
set size 1,(1.0/ROWS)
plot sin(1*x) # plot 1:
unset multiplot

只要第一(最后)行不同,此解决方案就可以工作,其他一切都遵循 nxm 方案。但是例如,您不能轻易地为第一行指定不同的高度。

$ gnuplot -d file.gp:

但是,我仍然对解决一般问题的其他方法感兴趣。

【讨论】:

    【解决方案2】:

    您可以从 matplotlib 获取几何图形。创建两个文件 subplot2grid2gnuplot.pysubplot2grid.gptest.gp 是一个使用示例。

    subplot2grid2gnuplot.py

    import matplotlib.pyplot as plt
    import sys
    
    if len(sys.argv)<6:
        sys.stderr.write("ERROR: subplot2grid2gnuplot.py needs 6 arguments\n")
        sys.exit(1)
    
    shape = (int(float(sys.argv[1])), int(float(sys.argv[2])))
    loc   = (int(float(sys.argv[3])), int(float(sys.argv[4])))
    colspan = int(float(sys.argv[5]))
    rowspan = int(float(sys.argv[6]))
    
    ax = plt.subplot2grid(shape, loc, colspan, rowspan)
    print "%f %f %f %f" % ax._position.bounds
    

    subplot2grid.gp

    # Return origin and size of the subplot
    # Usage:
    # call subplot2grid.gp shape1 shape1 loc1 loc2 colspan rowspan
    # Sets:
    # or1, or2, size1, size2
    
    aux_fun__(shape1, shape2, loc1, loc2, colspan, rowspan) = \
       system(sprintf("python subplot2grid2gnuplot.py  %i %i %i %i %i %i %i", shape1, shape2, loc1, loc2, colspan, rowspan))
    
    aux_string__= aux_fun__($0, $1, $2, $3, $4, $5)
    or1=word(aux_string__,1)
    or2=word(aux_string__,2)
    size1=word(aux_string__,3)
    size2=word(aux_string__,4)
    

    test.gp

    unset xtics
    unset ytics
    
    set multiplot
    
    call "subplot2grid.gp" 3 3 0 0 1 3
    set size size1, size2
    set origin or1, or2
    plot sin(x)
    
    call "subplot2grid.gp" 3 3 1 0 1 2
    set size size1, size2
    set origin or1, or2
    plot sin(x)
    
    call "subplot2grid.gp" 3 3 1 2 2 1
    set size size1, size2
    set origin or1, or2
    plot sin(x)
    
    call "subplot2grid.gp" 3 3 2 0 1 1
    set size size1, size2
    set origin or1, or2
    plot sin(x)
    
    call "subplot2grid.gp" 3 3 2 1 1 1
    set size size1, size2
    set origin or1, or2
    plot sin(x)
    
    unset multiplot
    

    【讨论】:

    • 非常感谢。我刚刚注意到我在 Ubuntu 10.04 下的 matplotlib 版本有点过时,至少需要 matplotlib 1.0(2011)版本。你能确认一下吗?
    • 两个问题:1)set size size1, size2; set origin or1, or2可以移动到subplot2grid.gp吗?我自己的快速测试似乎有效。 2) 远程运行脚本 ssh 需要 -X 选项,这似乎是不必要的:python traceback message says Traceback (most recent call last): File "subplot2grid2gnuplot.py", line 13, in &lt;module&gt; _tkinter.TclError: no display name and no $DISPLAY environment variable
    • 关于问题2)通过matplotlib.use('Agg')设置matplotlib后端解决了这个问题。
    • @Hotschke 我的 matplotlib 版本是 1.2.0。为了使它适用于不同的版本,我认为应该将 print "%f %f %f %f" % ax._position.bounds 行更改为更强大的内容。
    • @Hotschke 如果您确定不想调整坐标,可以移动set sizeset origin
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 2013-05-02
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多