altair icon indicating copy to clipboard operation
altair copied to clipboard

Saving from the interface is different from Chart.save

Open mauruscz opened this issue 2 years ago • 5 comments

Hi,

After several trials I managed to set a "saving" framework, so I am now able to save my plots as svg/png. Nevertheless, when I save it from the interface (i.e. clicking "save as svg") the saved plot is the same as the one that I visualize in the jupyter cell, that is what I would expect. Instead, when I do it executing the chart.save() command, the resulting plot is smaller and I found no way for specifying a custom widht and height. Here it is an eample:

...
fig.title = "Plot number " +str(i)
fig.project(
        "transverseMercator",
        ).properties(
        width=1000,
        height=2000,
        ).configure_view(
        stroke=None
        )

fig.save("out/network_"+str(i)+".svg", engine = "vl-convert" )

I tryied adding a width/height parameter to the save() method, or doing something like

fig.properties(
        width=1000,
        height=2000,
)

But sill I obtain a really small and squared svg/png file.

Is it a bug or am I missing something?

Thank you very much

mauruscz avatar Sep 18 '23 11:09 mauruscz

Thanks for the report. I think what's happening is that Altair methods like chart.project() and chart.properties() return new copies of the chart with the changes applied. Then don't modify the chart that the method is called on.

So instead of something like this:

chart = alt.Chart(...).mark(...).encode(...)
chart.properties(...)
chart.save()

You want

chart = alt.Chart(...).mark(...).encode(...)
chart = chart.properties(...)
chart.save()

Does that work for you? If not, could you provide a full minimal example that can be copied and evaluated without modification?

jonmmease avatar Sep 18 '23 11:09 jonmmease

Thank you for your fast and precise answer. Indeed, it is working! I hope you can be able to fix the saving in pdf for the altair version >5 asap, because the library rocks!

mauruscz avatar Sep 18 '23 12:09 mauruscz

Great to hear it! And as it happens, PDF support recently landed in vl-convert, and so we should be able to get PDF support added for Altair 5.2. I'll try to remember to ping you on this thread when it's available.

Out of curiosity, what would you like to do with the PDF output? Is this for embedding in a larger LaTeX document?

jonmmease avatar Sep 18 '23 12:09 jonmmease

Yes, it is! I am a researcher within the human mobility analysis field (we also mantain a library), so the final goal would be to have an high resolution image ready for publications.

In this direction, it would be wonderful to have the possibility of drawing "edges" of a network, (graphically, drawing directed vectors).

mauruscz avatar Sep 18 '23 13:09 mauruscz

In terms of network drawings in altair, there used to be this project which seems to have halted now https://github.com/Zsailer/nx_altair. But maybe there is something that can still be used from there?

joelostblom avatar Sep 21 '23 02:09 joelostblom