How to create a multi-page PDF-file with Gnuplot?

百般思念 提交于 2019-12-06 02:19:15

问题


I make a dozen of plots with Gnuplot on Mac via ruby-gnuplot. If I re-run my ruby script, then the number of open windows with the plots doubles. If I could just output all these plots in a PDF opened in Preview, then the file would be automatically updated after every re-run and I don't need to bother closing the numerous windows.

Currently I can achieve this only with one plot per PDF-file:

Gnuplot.open do |gp|
  Gnuplot::Plot.new(gp) do |plot|
    plot.arbitrary_lines << "set terminal pdf \n set output 'figures.pdf'"
    # ...
  end
end

How can I make a single PDF with all my figures by Gnuplot?


回答1:


Hmm, at least on gnuplot for UN*x, multipage output for postscript and PDF always was the default - as long as you don't either change the terminal type nor reassign the output file, everything you plot ends up on a new page.

I.e. you do:

set terminal pdf
set output "multipageplot.pdf"
plot x, x*x
plot sin(x), cos(x)
set output ""

and you end up with two pages in the PDF file, one containing line/parabola, the other sine/cosine.

To clarify: The important thing is to issue all the plot commands in sequence, without changing the output file nor changing the terminal type. Gnuplot won't append to an existing PDF file.




回答2:


I make thousands of plots with ruby-gnuplot and use a gem called prawn to compile them into a pdf. The following is a code snippet using prawn, that includes some useful features:

require 'prawn'

def create_pdf
  toy_catalogue = @toy_catalogue
  full_output_filename ||= "#{output_path}/#{pre-specified_filename_string}"
  Prawn::Document.generate(full_output_filename, :page_layout => :portrait, :margin => 5, :skip_page_creation => false, :page_size => [595, 1000]) do
    toy_catalogue.each do |toy|
      start_new_page

      image toy[:plan_view], :at => [0,900], :width => 580

      image toy[:front_view], :at => [0, 500], :width => 585

      font_size(20) { draw_text toy[:name], :at => [5, 920] }

      draw_text "production_date = #{toy[:date]}", :at => [420, 930]
    end
  end
end

That should be easy enough to adapt to your purposes.



来源:https://stackoverflow.com/questions/4334301/how-to-create-a-multi-page-pdf-file-with-gnuplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!