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.

3
  • I tried to tag this question properly but failed. I am not referring to ggplot or ggplot2 in R. See github.com/yhat/ggplot. Commented Oct 29, 2013 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! Commented Oct 29, 2013 at 18:48
  • That is odd, they use plt.gca() heavily in their code. Commented Oct 30, 2013 at 2:53

3 Answers 3

5
+50

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()
Sign up to request clarification or add additional context in comments.

Comments

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()

2 Comments

I get AttributeError: 'ggplot' object has no attribute 'draw'.
This is because the answer was valid for ggpy before the big rewrite. See the above answer for the current situation:stackoverflow.com/a/44207128/1380673
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...

3 Comments

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.)
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()
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.