plotly / orca

Command line application for generating static images of interactive plotly charts
MIT License
294 stars 40 forks source link

Vector format size #205

Open CarlAndersson opened 5 years ago

CarlAndersson commented 5 years ago

Exporting figures (from python) using the vector formats gives some (to me) strange behaviour regards to size of the resulting image.

When I set the figure width to 330 and export as pdf the resulting figure (in Latex) is roughly 8.6 cm wide. Exporting the same figure in a raster format 250 px wide I also get a figure of ~8.6 cm.

If I insect the pdf with finder on mac os it reports 251 px wide. If i inspect the file using 'pdfinfo' form poppler i get a width of 251 pts. If I inspect using spis i get 523 pixels wide @ 150 dpi.

TLDR: exported pdfs with 330 as width behaves as 250 in width??

NB: What I really would like is to set the width in cm which imho is a more reasonable unit for vector graphics…

etpinard commented 5 years ago

Exporting figures (from python) using the vector formats

Could share the commend you're using to help us debug?

If I inspect the pdf with finder on mac os it reports 251 px wide.

I suspect this is related to https://github.com/plotly/orca/issues/12 - thanks for reporting!

CarlAndersson commented 5 years ago

Hi again, Sorry for the delay, I had to finish my paper...

Here's a MWE in python showing the issue with pdf vs. jpeg.

import plotly.io as pio
x = list(range(-10, 11))
y = [v**2 for v in x]
data = [{'type': 'scatter', 'x': x, 'y': y}]
layout = dict(
    width=330,
    height=200,
)
pio.write_image({'data': data, 'layout': layout}, 'testfig.pdf')
pio.write_image({'data': data, 'layout': layout}, 'testfig.jpg')

and some tex code

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\par\noindent
\includegraphics{testfig.jpg}
\par\noindent
\includegraphics{testfig.pdf}
\end{document}

Compiling gives me a pdf like this one, where the size difference is obvious. plotly_orca_mwe.pdf

jonmmease commented 5 years ago

Looking at...

https://github.com/plotly/orca/blob/4b2310dc7a82f13434e082934dbf3ef69b1e7cb5/src/component/plotly-graph/render.js#L128-L152

and with some experimentation, I found a formula for exporting to an exact size in inches.

import plotly.io as pio
x = list(range(-10, 11))
y = [v**2 for v in x]
data = [{'type': 'scatter', 'x': x, 'y': y}]

# Constants factors
marginInches = 1/18
ppi = 96
width_inches = 6
height_inches = 4

# Write figure with dimensions in inches
pio.write_image({'data': data}, 'testfig.pdf',
                width=(width_inches - marginInches)*ppi,
                height=(height_inches  - marginInches)*ppi)

testfig.pdf

One oddity is that when I export figure where the height exceeds the width (i.e. portrait mode), the resulting PDF sometimes overflows to 2 pages. e.g. with width_inches = 6 and height_inches = 4

testfig.pdf

@etpinard @antoinerg Do you think we could have orca accept dimensions in inches/cm and perform these scaling/offset conversions internally?