DMelt:Plots/Export to Vector Graphics

From HandWiki
Member

Export to vector graphics

Any graphical canvas can be exported to high-quality image files. Publication-quality graphics can be achieved using Vector graphics output supported by DataMelt.

There are several ways to save image canvases to SVG files. For example, use the method "export" of the jhplot.HPlot jhplot.HPlot class.

from jhplot  import *
c1 = HPlot("Canvas")
c1.visible()
c1.setAutoRange()
f1 = F1D("2*exp(-x*x/50)+sin(pi*x)/x", -2.0, 5.0)
f1.setPenDash(4)
c1.draw(f1)
c1.export("example.svg")

This will create SVG image showing the function "2*exp(-x*x/50)+sin(pi*x)/x". The execution of this script will pop-up the canvas itself. If you run this script not inside DataMelt IDE, but using the batch (using dmelt_batch.sh) method, you can set:

c1.visible(False)

This will quietly generates the file example.svg. How to create EPS and PDF files? Generally, you can do it using the file extensions:

c1.export("example.pdf")  # PDF output file
c1.export("example.eps")  # EPS output file

You can also export files to raster formats, using the extensions ".png" and ".jpg". This will create good-quality images with support of transparency. But you cannot resize such plots.

Creating EPS and PDF files

For inclusion of files generated by DataMelt to Software:LaTeX documents, you should make EPS or PDF files. The quality of figures for publications should be very high, and file sizes should be optimized. Therefore, the recommendation is to create first SVG file, and then convert it to EPS or PDF. It is also recommended to do the same if you need a high-quality PNG image.

This can be done on Linux and MAC OS computers using external programs. For example, this is an example of creating high-quality PDF and EPS files from SVG format:

inkscape --without-gui --export-pdf=example.pdf  example.svg # convert to PDF
inkscape --without-gui --export-eps=example.eps  example.svg # convert to EPS
inkscape --without-gui --export-png=example.png  example.svg # convert to PNG

You need to install the "inkscape" program for vector graphics. This is another useful command to create high-quality PNG image with the size 200x120 px.

inkscape --without-gui --export-png  example.png -w 200 -h 120 example.svg

Native creation of EPS and PDF

You can further increase the quality of output images when using DataMelt directly without external programs. In this approach, you can create publishable-quality EPS and PDF images on Windows. A high-quality, optimized PDF or EPS file will be created directly by the Jython/Java program where graphics canvas is defined.

Here is an example of making EPS file from SVG using DMelt Java library:

c1.export("example.svg")
c1.convertSVG("example.svg","example.eps") # convert to EPS

Similarly, you can create PDF file from SVG:

c1.export("example.svg")
c1.convertSVG("example.svg","example.pdf") # convert to PDF

It is useful to convert SVGZ to compressed SVGZ format as:

c1.export("example.svg")
c1.convertSVG("example.svg","example.svgz") # convert to SVGZ

In addition, you can convert any SVG file to PDF, EPS, SVGZ, PNG and JPG as:

from  jhplot.io.images import  ConvertSVG
ConvertSVG.SVGTo("input.svg", "ouput.pdf")

(the output is a PDF file). By defalt, the original image is kept. IF you run:

from  jhplot.io.images import  ConvertSVG
ConvertSVG.SVGTo("input.svg", "ouput.pdf",true)

The original image will be removed.