Saving high-resolution images with plotnine
Asked Answered
L

2

18

I'm trying to use plotnine to save a high-resolution png image.

With a test dataset, this looks like:

from plotnine import *
import pandas as pd
import numpy as np

df = pd.DataFrame()
df['x'] = np.arange(0,10,0.01)
df['y'] = np.sin(df['x'])

p = ggplot(df, aes(x='x',y='y')) + labs(x='x', y='y') + geom_point(size=0.1)
p.save(filename = 'test3.png', height=5, width=5, units = 'in', dpi=1000)

This produces a low-resolution .png file containing my plot, which is not improved when I increase the specified dpi.

I've also tried saving with:

ggsave(plot=p, filename='test.png', dpi=1000)

and replacing dpi=1000 with res=1000. This produces identical low-resolution png files.

How can I save my plot at the resolution I want?

Edit: This bug is resolved in plotnine version 0.3.0. and the above code works correctly.

Lorusso answered 18/1, 2018 at 23:32 Comment(5)
What do you mean by low-resolution? Note that with a dpi of 1000, what you are saying is that I want an image that is printable at 1000 dpi or looks good on hi-res monitor. Beyond some dpi (about 300), you will most likely be creating images specifically for print. Check out graphicdesign.stackexchange.com/questions/36683/….Overload
The images that are being saved are at around dpi 100, regardless of whether I specify 100, 300, or 2000. I'm looking for about 300 dpi for the final image.Lorusso
Update to the latest version of plotnine. You are experiencing a bug that was fixed.Overload
Resolved, thank you! I updated plotnine just before encountering this problem. I used: $ conda update plotnine Which installed version 0.2.1, and did not see version 0.3.0. Adding conda-forge: $ conda config --add channels conda-forge Allowed conda update to see and install the latest version of plotnine, which fixed the bug. I am now seeing beautiful, variable-resolution plots.Lorusso
SO considers a question "open" until an answer is either accepted or there's at least one up-voted answer. Since the above worked for you, you could answer your own question, or delete, since the bug is fixed so it's unlikely anyone else will have this question. :-)Anthropoid
O
15

Since this still isn't answered and I just got directed here, too...

According to @has2k1 (the author of plotnine), this was a bug and is now resolved. This commit looks like it might be the referenced fix.

To solve the issue, ensure you are using the git version or at least version 0.3.0.

Oversubtlety answered 26/8, 2018 at 16:31 Comment(0)
I
1

There is also the possibility to save the matplotlib figure

import plotnine as pn

fig, plot = (pn.ggplot()
 + ...

 + pn.theme(panel_background=pn.element_blank())
 + pn.theme(axis_title_y=pn.element_blank())
 + pn.theme(axis_ticks_major_y=pn.element_blank())
 + pn.theme(figure_size=(12, 8))
             ).draw(show=False, return_ggplot=True)

fig.savefig('image.png', dpi=300)

which worked ok for me.

Interoffice answered 22/1, 2022 at 21:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.