Convert matplotlib graphs to emf file format

Convert matplotlib graphs to emf file format

Graph Excel data with matplotlib (1)

↑ As mentioned here, there was a problem that it could not be saved directly in the emf file format. I found my own solution, so I will summarize it here.

environment

・ Ubuntu 16.04 ・ Python3.5.2 ・ Matplotlib 2.0.0 ** ・ Inkscape 0.91 (used for eps → emf conversion) ** ← This must be installed

Method

(1)Graph drawn with matplotlibsvg formatSave with plt.savefig('sample.svg')

(2) From the command line Inkscape [source file name.svg] -M [file name.emf]

$ inkscape sample.svg -M sample.emf

If you do it from python in one shot

How can I run the command line in python? For the time being, I was able to do what I wanted to do.

sample.py


import subprocess
from matplotlib import pyplot as plt

fig, ax = plt.subplots()
ax.plot([1,2,3])

plt.savefig("sample.svg")
subprocess.call('inkscape sample.svg -M sample.emf',shell=True)
plt.show()

that's all

(memo) Format that can be saved by matplotlib 2.0.0

I investigated in this way

import matplotlib.pyplot as plt
fig = plt.figure()
print(fig.canvas.get_supported_filetypes())

The result is this list

'pdf': 'Portable Document Format', 
'ps': 'Postscript', 
'rgba': 'Raw RGBA bitmap', 
'pgf': 'PGF code for LaTeX',
'svgz': 'Scalable Vector Graphics', 
'svg': 'Scalable Vector Graphics', 
'raw': 'Raw RGBA bitmap', 
'eps': 'Encapsulated Postscript', 
'png': 'Portable Network Graphics'

(memo) inkscape0.91 command line options

Usage: inkscape [options ...] [files ...]

Valid options: -V, --version Show Inkscape version -z, --without-gui Do not use X server (only process files in console) Attempts to use the X server even if the -g, --with-gui $ DISPLAY variable is not set -f, --file = filename Open the specified document (Optional strings may be ignored) -p, --print = filename Print the document to the specified file (pipe with'| program') -e, --export-png = filename Export document to PNG file -d, --export-dpi = DPI Export to bitmap and PS / EPS / PDF Filter rasterization resolution (default is 90) -a, --export-area = x0: y0: x1: y1 SVG Per-user export area (default is page, 0,0 Is the lower left corner) -D, --export-area-drawing The export area is the entire drawing (not the page) -C, --export-area-page Export area is the entire page --export-margin = value For PS / EPS / PDF only, set the margin around the export area in mm (Default is 0) --export-area-snap SVG bitmaps outside the export area Snap to approximate integer value per user -w, --export-width = width Specify the generated bitmap width as a pixel value (Priority over export resolution) -h, --export-height = Height Specify the height of the generated bitmap as a pixel value (Priority over export resolution) -i, --export-id = ID ID of the object to export -j, --export-id-only export-id Export only the objects of, not all others (Only when export-id is specified) -t, --export-use-hints Export using filename or DPI hint (only if export-id is specified) -b, --export-background = Color Export bitmap background color (SVG supported color string) -y, --export-background-opacity = value Export bitmap transparency (0.0 to 1.0, or 1 to 255) -l, --export-plain-svg = filename Export to plain SVG file (Do not use sodipodi or inkscape namespace) -P, --export-ps = filename Export document to PS file -E, --export-eps = filename Export document to EPS file --export-ps-level = PS Level Select the PostScript level to use for the export. Level 2 (Default) and 3 are available. -A, --export-pdf = filename Export document to PDF file --export-pdf-version = PDF_VERSION Export with the specified version of PDF. (Hint: PDF Specify the exact string that you can see in the export dialog. Example: "PDF 1.4" conforms to PDF-a) --export-latex Export to PDF / PS / EPS except for text. LaTeX in addition to PDF / PS / EPS When you export the file, the text will be PDF / PS / EPS Is placed at the beginning of. LaTeX contains output like \ input {latexfile.tex}. -M, --export-emf = filename Export document to extended metafile (EMF) -m, --export-wmf = filename Export document to Windows Metafile (WMF) -T, --export-text-to-path Convert text objects to paths on export (PS, EPS, PDF, SVG) --export-ignore-filters Render filtered objects unfiltered instead of rasterizing (PS, EPS, PDF) -X, --query-x X for the entire drawing or, if specified, an object with --query-id Inquire coordinates -Y, --query-y Y for the entire drawing or, if specified, an object with --query-id Inquire coordinates -W, --query-width entire drawing or --query-id if specified Query the width of the object -H, --query-height entire drawing or --query-id if specified Inquire about the height of the object -S, --query-all List id, x, y, w, h of all objects -I, --query-id = ID ID of the object to query for position and size -x, --extension-directory Output the extension directory and exit --vacuum-defs Remove unused definitions from the defs section of the document --verb-list Lists the IDs of all VERBs in Inkscape --verb = VERB-ID VERB to call when Inkscape is opened --select = OBJECT-ID ID of the object to select when opening Inkscape --Launch shell Inkscape in interactive shell mode

Help options: -?, --help Show this help message --usage Display brief usage message

Recommended Posts

Convert matplotlib graphs to emf file format
How to convert Json file to CSV format or EXCEL format
Convert genbank file to gff file
Convert Pascal VOC format xml file to COCO format json file
[Caffe] Convert mean file from binary proto format to npy format
Easy to draw graphs with matplotlib
[Python] How to convert db file to csv
Convert xml format data to txt format data (yolov3)
How to easily convert format from Markdown
How to convert Python to an exe file
[Python] Convert csv file delimiters to tab delimiters
Convert psd file to png in Python
Convert PDF attached to email to text format
Convert Python date types to RFC822 format
How to convert DateTimeField format in Django
[Tensorflowjs_converter] How to convert Tensorflow model to Tensorflow.js format
Convert to HSV
[Python] How to draw multiple graphs with Matplotlib
Convert svg file to png / ico with Python
convert youtube playlist to local m3u format file for smplayer (by toy tool)
Convert multiple jpg files to one PDF file
Convert json format data to txt (using yolo)
Convert binary packages for windows to wheel format
Convert strings to character-by-character list format with python
Convert a text file with hexadecimal values to a binary file
Convert Qiita articles to Jekyll post format for backup
How to convert JSON file to CSV file with Python Pandas
How to convert a mel spectrogram back to a wav file
Convert / return class object to JSON format in Python
Convert translation resource files (.po) to XLIFF format (.xlf)
Convert kanji to kana
Convert jupyter to py
Convert keras-yolo3 to onnx
Reading pandas format file
How to use Matplotlib
Convert dict to array
Convert json to excel
[python] How to use the library Matplotlib for drawing graphs
Python / datetime> Implementation to convert YYYYMMDD format to YYYY / MM / DD
Linux script to convert Markdown files from JupyterLab format to Qiita format
Convert Excel file to text in Python for diff purposes