Home > Software. > Gnuplot: SVG export and Inkscape — annoyances and workarounds

Gnuplot: SVG export and Inkscape — annoyances and workarounds

I’m making some data plots with gnuplot right now that will be fed to pdfLaTeX later. So I could just have gnuplot create .ps or .eps files, but for some reason it always gets the font completely wrong. When reverse engineering pdf files that have the font I want and feeding gnuplot with that exact font name, the postscript console crashes fatally. Bummer.

So I tried the SVG console. The fonts are OK and I can just use Inkscape to do all the post processing I want and export to PDF. BINGO – but that was too easy, Murphy’s Law is lurking somewhere.

1
2
3
4
5
6
7
8
9
10
11
12
13
set xlabel "x-axis label" font "Bitstream Vera Sans, 12"
set ylabel "y-axis label" font "Bitstream Vera Sans, 12"
set title "title for the plot" font "Bitstream Vera Sans, 12"
#  choose the SVG terminal
set terminal svg font "Bitstream Vera Sans, 12" linewidth 1
#  pipe the output to a file
set output "|cat >./data_plot.svg"
# position of the legend
set key 95,0.5
set logscale y
set yrange [0.007:1]
#  rescale the xy-data and plot it
plot "<awk '{print $1, -(200/1000000000)/log($2)*1000000}' ascii_data.raw" title "plot curve name" lt rgb "blue" w lp

To avoid having to type all of this again and again, you can just put it into a text file and call gnuplot like this:

1
gnuplot ./my_plot_commands.txt

The path to the data file to be plotted is relative to the current working directory. So this creates an SVG file, Inkscape loads it just fine, but there are quite a few annoyances:

  • data point marker (dots/circles/boxes) are invisible (but there)
  • xy-axes are invisible
  • data point markers get lost when copying to a new SVG document
  • can’t change the stroke width on markers
  • “fit page to selection” doesn’t work – bounding box error ?

It turns out that gnuplot’s SVG terminal uses the term “currentColor”, which seems to confuse Inkscape. This has been filed in the bugtracker before. A quick fix is to replace every single instance of “color: somecolor; stroke: currentColor” with “color: somecolor; stroke: somecolor”. This is not 100% perfect, but as I’m post processing the files with Inkscape anyway, I can change colors later if need be. Here’s a little Perl script that does the job:

1
2
3
4
5
6
7
#!/usr/bin/perl -T -W
print STDERR "\n\nDon't forget to use CLONE-->UNLINK CLONE on the data points\n\n";
while ( <STDIN> ) {
  my $line = $_;
  $line =~ s/(color:)(.*)(;\ * stroke:)currentColor/$1$2$3$2/;
  print $line;
}

It’s used like so: cat data_plot.svg | ./script.pl > ./fixed_data_plog.svg

So now all the axes and data point markers show up, but “fit page to selection” still doesn’t work. Why not just copy the plot to a new SVG document, maybe that takes care of that? But oh no, the data point markers are all gone now ;-( By looking at the SVG file I found that gnuplot tried to be clever. Instead of drawing new data point markers again and again, it _defined_ the object once in the “<defs>” section and accessed it by linking to the definition again and again.

1
2
3
4
5
6
7
8
9
<defs>
...
<path id='gpPt0' style='stroke-width:0.222' d='M-1,0 h2 M0,-1 v2'/>
...
</defs>
...
...
<use xlink:href='#gpPt0' transform='translate(545.0,304.1) scale(4.50)' style='color:rgb(  0,   0, 255)'/>
...

It just so happens, that when copying the data plot to a new SVG document, the <defs> section is _not_ copied. All the links point to nowhere and the data point markers are lost. Also it was not possible to change the stroke width of these markers. By a little bit of trial and error I found out that by applying the Inkscape command “Edit –> Clone –> Unlink Clone” to all the markers they are turned to individual objects and can now be copied and modified as desired. This took the better of one full working day and has been filed to the bugtracker. I hope this is fixed in the upcoming version 0.47 of Inkscape.

No related posts.

robert Software. , , , ,

  1. No comments yet.
  1. No trackbacks yet.