benschneider / PyGnuplot

Python interface to gnuplot
MIT License
45 stars 18 forks source link

Error saving a pdf (on MacOS) using pdfcario #11

Open Nebuchadrezzar opened 6 years ago

Nebuchadrezzar commented 6 years ago
>>> import PyGnuplot as gp
>>> gp.c('plot sin(x)')
>>> gp.pdf('graph.pdf')
>>>

gnuplot> set term pdf enhanced size 14cm, 9cm color solid fsize 12 fname 'Helvetica';
                                                          ^
         line 0: unrecognized terminal option

Note: I have x11, aqua, and pdfcario terminals but not plain pdf. It looks as though pdfcario can't handle the "fsize and fname" arguments. It does accept: "font 'Helvetica,12"

Not working:

gnuplot> set term pdf enhanced size 14cm, 9cm color solid fsize 12 fname 'Helvetica'
Terminal type is now 'pdfcairo'
                                                          ^
         unrecognized terminal option

gnuplot> set term pdf enhanced size 14cm, 9cm color solid fname 'Helvetica'
Terminal type is now 'pdfcairo'
                                                          ^
         unrecognized terminal option

gnuplot> set term pdf enhanced size 14cm, 9cm color solid
Terminal type is now 'pdfcairo'
Options are ' transparent enhanced fontscale 0.5 size 14.00cm, 9.00cm '

This works:

gnuplot> set term pdf enhanced size 14cm, 9cm color solid font 'Helvetica,12'

Terminal type is now 'pdfcairo'
Options are ' transparent enhanced font "Helvetica,12" fontscale 0.5 size 14.00cm, 9.00cm '
Nebuchadrezzar commented 6 years ago

Perhaps a new function:

def pdfcario(filename='tmp.pdf', width=14, height=9, fontsize=12, term=default_term):
# Script to make gnuplot print into a pdf file using pdfcario term
    c('set term pdf enhanced size ' + str(width) + 'cm, ' + str(height) + 'cm  color solid font ' + ' "Helvetica,' + str(fontsize) + "' ;")
    c('set out "' + filename + '";')
    c('replot;')
    c('set term ' + str(term) + '; replot')
benschneider commented 6 years ago

Ahh yes, ideally would be a function which autodetects the best available terminal and method. I don't know yet how to read out gnuplot defaults on all platforms equally. Searching for it.

Nebuchadrezzar commented 6 years ago

What about a cruder approach of just detecting to OS system with something like:

import platform platform.platform()

Then selecting an appropriate terminal given the feedback from platform()? For Mac and Windows, using pdfcario instead of pdf seems to make more sense.