14

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.

Richard
  • 56,349
  • 34
  • 180
  • 251
Blizzard
  • 198
  • 1
  • 10
  • 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 https://graphicdesign.stackexchange.com/questions/36683/how-does-size-work-with-png-8-and-png-24-images. – has2k1 Jan 21 '18 at 17:51
  • 1
    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. – Blizzard Jan 22 '18 at 00:26
  • 3
    Update to the latest version of plotnine. You are experiencing a bug that was fixed. – has2k1 Jan 22 '18 at 00:57
  • 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. – Blizzard Jan 22 '18 at 03:17
  • 3
    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. :-) – Richard Feb 08 '18 at 00:59

2 Answers2

13

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.

Hendy
  • 10,182
  • 15
  • 65
  • 71
0

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.

karpan
  • 421
  • 1
  • 5
  • 13