7

python ggplot is great, but still new, and I find the need to fallback on traditional matplotlib techniques to modify my plots. But I'm not sure how to either pass an axis instance to ggplot, or get one back from it.

So let's say I build a plot like so:

import ggplot as gp

(explicit import)

p = gp.ggplot(gp.aes(x='basesalary', y='compensation'), data = df)
p + gp.geom_histogram(binwidth = 10000)      

No problems so far. But now let's say I want the y-axis in log scale. I'd like to be able to do this:

plt.gca().set_yscale('log')

Unfortunately, plt.gca() doesn't access the axis created by ggplot. I end up with two figures: the histogram from ggplot in linear scale, and an empty figure with a log-scale y axis.

I've tried a few variations with both gca() and gcf() without success.

joris
  • 133,120
  • 36
  • 247
  • 202
Aman
  • 45,819
  • 7
  • 35
  • 37
  • I tried to tag this question properly but failed. I am not referring to ggplot or ggplot2 in R. See https://github.com/yhat/ggplot. – Aman Oct 29 '13 at 18:10
  • 1
    I've added a tag and a rough wiki copied from the source. Once it gets peer-reviewed it should be up. Thanks for the heads-up on this library, it looks nice! – Hooked Oct 29 '13 at 18:48
  • That is odd, they use `plt.gca()` heavily in their code. – tacaswell Oct 30 '13 at 02:53

3 Answers3

5

There might have been some changes since 2013 when this question was asked. The way to produce a matplotlib figure from a ggplot is

g.make()

after that, figure and axes can be obtained via

fig = plt.gcf()
ax = plt.gca()

or, if there are more axes, axes = fig.axes.

Then, additional features can be added in matplotlib, like also shown in this question's answer.

Finally the plot can be saved using the usual savefig command.

Complete example:

import ggplot as gp
import matplotlib.pyplot as plt

# produce ggplot
g = gp.ggplot(gp.aes(x='carat', y='price'), data=gp.diamonds)
g = g + gp.geom_point()
g = g + gp.ylab(' ')+ gp.xlab(' ')
# Make
g.make()

# obtain figure from ggplot
fig = plt.gcf()
ax = plt.gca()
# adjust some of the ggplot axes' parameters
ax.set_title("ggplot plot")
ax.set_xlabel("Some x label")
plt.savefig(__file__+".png")
plt.show()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
3

[This is outdated with current ggpy]

There is now a scale_y_log(). If you want to do something in matplotlib, you can get the current figure/axis with

g = ggplot(...)
fig = g.draw()
#or
g.draw() # or print(g)
fig = plt.gcf() 
ax = plt.gca()

Your version fails because ggplots draws the plot on print(g) in the ggplot.__repr__() method (which calls ggplot.draw()), so there is simple no matplotlib figure right after constructing the ggplot object but only after print (or g.draw()). g.draw() also returns the figure, so you don't need to use plt.gcf()

Jan Katins
  • 2,219
  • 1
  • 25
  • 35
  • 1
    I get `AttributeError: 'ggplot' object has no attribute 'draw'`. – bli May 26 '17 at 12:58
  • This is because the answer was valid for ggpy before the big rewrite. See the above answer for the current situation:https://stackoverflow.com/a/44207128/1380673 – Jan Katins May 27 '17 at 11:37
0

Did you try:

p = gp.ggplot(gp.aes(x='basesalary', y='compensation'), data = df)
p + gp.geom_histogram(binwidth = 10000) + gp.scale_y_log()

Not sure if it works just like that though, just guessing from looking at the code...

herrfz
  • 4,814
  • 4
  • 26
  • 37
  • What I'm trying to do is work with matplotlib directly to modify ggplot's output. The log scaling was just an example. (FYI, I didn't find a `ggplot.scale_y_log()` when I originally wrote the question, but there could be one now.) – Aman Jan 13 '14 at 21:41
  • OK, I think I got what you mean.. I understand your orig question was from Oct '13, a lot might have changed since then, at least `gcf()` works for me now: `f = plt.gcf(); ax = f.gca(); ax.set_yscale('log'); plt.show()` – herrfz Jan 13 '14 at 22:27
  • The trick in the previous comment works in an ipython interactive session, but it seems that the graphics has to be displayed, which seems to prevent the integration in a non-interactive script. – bli May 26 '17 at 13:14