3

While this part works

import matplotlib.pyplot as plt
from ggplot import *
import numpy as np
import pandas as pd

df = pd.DataFrame({'Height':np.random.randn(10),
                   'Weight':np.random.randn(10),
                   'Gender': ["Male","Male","Male","Male","Male",
                              "Female","Female","Female","Female","Female"]})
p=ggplot(aes(x='Height', y='Weight', color='Gender'), data=df)  + geom_point()
p.make()
fig = plt.gcf()
ax = plt.gca()
fig.set_figwidth(25, forward=True)
plt.show()

when I try to save the image, it fails miserably generating a blank image.

plt.savefig("image.tiff", dpi=300)

Any ideas? Thanks!

teddy browns
  • 141
  • 1
  • 6
  • try plt.savefig("image.tiff", bbox_inches='tight') and try to use the sequence plt.savefig("image.tiff", bbox_inches='tight') plt.show() plt.close() – user2510479 Apr 15 '17 at 15:47
  • This could be caused by [this issue](https://github.com/python-pillow/Pillow/issues/1524), which is referenced [here](http://stackoverflow.com/questions/35092163/error-in-saving-matplotlib-plot-to-tiff). A workaround is always to save as png and convert it to tiff with an external program. – ImportanceOfBeingErnest Apr 15 '17 at 16:19

3 Answers3

0

did you try with ggsave(plot = p, filename = 'image.tiff') or p.save('image.tiff')?

EDIT ggsave has been removed from ggplot,use the second solution instead.

Astrom
  • 767
  • 5
  • 20
  • ggsave is not available in ggplot, while p.save ignores the new width I specified with fig.set_figwidth(25, forward=True) – teddy browns Apr 15 '17 at 16:20
  • `p.save(filename, width=None, height=None, dpi=180)` – Astrom Apr 15 '17 at 16:30
  • Thank you. What I'd like to have is consistent "on screen" and "on file" image size. With p.save I can save the image with the desired width, issue is that on display that image will still remain in a tiny box 4x4. – teddy browns Apr 15 '17 at 16:43
  • could you try with this? `import matplotlib as mpl` and then `mpl.rcParams["figure.figsize"] = "11, 8"` – Astrom Apr 15 '17 at 20:07
0

I successfully saved the figure to file using the ggplot object save method. Here is an example:

p.save('image.tiff', width=12, height=8, dpi=144)

For more info, see the comment by @Johan in this SO response, the source code found here, or the example in the ggplot | docs found here.

In terms of setting the on screen size, it doesn't seem like it's possible with the current API. A possible hack is to clone the repo and modify line 624 to be the following:

self.fig, self.subplots = plt.subplots(subplot_kw=subplot_kw, figsize=(my_x, my_y)

where my_x and my_y are the x and y sizes you'd like for the on screen display. Then use the show method:

p.show()
Community
  • 1
  • 1
mgig
  • 2,395
  • 4
  • 21
  • 36
  • Thank you. What I'd like to have is consistent "on screen" and "on file" image size. With p.save I can save the image with the desired width, issue is that on display that image will still remain in a tiny box 4x4. – teddy browns Apr 15 '17 at 16:43
  • It's not obvious to me that the on screen size can be set easily. I just edited my response above to include a possible work around. – mgig Apr 15 '17 at 17:18
  • FYI, this option is going away _savefig() got unexpected keyword argument "width" which is no longer supported as of 3.3 and will become an error in 3.6_ – Richard Erickson Jul 09 '22 at 20:46
0

Thanks to all comments -- this is what ended up working. A little repetition in the code, but it works at last.

import matplotlib.pyplot as plt
from ggplot import *
import numpy as np
import pandas as pd

df = pd.DataFrame({'Height':np.random.randn(10),
                   'Weight':np.random.randn(10),
                   'Gender': ["Male","Male","Male","Male","Male",
                              "Female","Female","Female","Female","Female"]})
p=ggplot(aes(x='Height', y='Weight', color='Gender'), data=df)  + geom_point()
p.make()
fig = plt.gcf()
ax = plt.gca()

w,h=25,10 #set width and height for both screen and file size

#this part prints on screen the chart
fig.set_figwidth(w, forward=True)  #screen
fig.set_figheight(h, forward=True) #screen
plt.show()

#this part saves it to file
p.save(filename, width=w, height=h, dpi=300) #file
teddy browns
  • 141
  • 1
  • 6