Accessing axis or figure in python ggplot

2024/9/28 4:52:46

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.

Answer

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()
https://en.xdnf.cn/q/71256.html

Related Q&A

Importing the same modules in different files

Supposing I have written a set of classes to be used in a python file and use them in a script (or python code in a different file). Now both the files require a set of modules to be imported. Should t…

Manually calculate AUC

How can I obtain the AUC value having fpr and tpr? Fpr and tpr are just 2 floats obtained from these formulas:my_fpr = fp / (fp + tn) my_tpr = tp / (tp + fn) my_roc_auc = auc(my_fpr, my_tpr)I know thi…

Plotly: How to make stacked bar chart from single trace?

Is it possible to have two stacked bar charts side by side each of them coming from a single column?Heres my df:Field IssuePolice Budget cuts Research Budget cuts Police Time consum…

zip function help with tuples

I am hoping someone can help me with a problem Im stuck with. I have a large number of tuples (>500) that look like this:(2,1,3,6) (1,2,5,5) (3,0,1,6) (10,1,1,4) (0,3,3,0) A snippet of my c…

Mini batch training for inputs of variable sizes

I have a list of LongTensors, and another list of labels. Im new to PyTorch and RNNs so Im quite confused as to how to implement minibatch training for the data I have. There is much more to this data,…

Python gTTS, is there a way to change the speed of the speech

It seems that on gTTS there is no option for changing the speech of the text-to-speech apart from the slow argument. I would like to speed up the sound by 5%. Any suggestion on how I can do it? Best.t…

Change QLabel text dynamically in PyQt4

My question is: how can I change the text in a label? The label is inside a layout, but setText() does not seem to work - maybe I am not doing it right. Here is my code:this is the Main windows GUI, t…

Setting figure size to be larger than screen size in matplotlib

Im trying to create figures in matplotlib that read nicely in a journal article. I have some larger figures (with subfigures) that Id like to take up nearly an entire page in portrait mode (specificall…

Tensorflow 0.7.1 with Cuda Toolkit 7.5 and cuDNN 7.0

I recently tried to upgrade my Tensorflow installation from 0.6 to 0.7.1 (Ubuntu 15.10, Python 2.7) because it is described to be compatible with more up-to-date Cuda libraries. Everything works well i…

How to export tensor board data?

In the tensorborads README.md, it ask me to do like this:How can I export data from TensorBoard?If youd like to export data to visualize elsewhere (e.g. iPython Notebook), thats possible too. You can…