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