Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ipython-notebook: print rmagic plots as svg

I use the following code fragment to plot a pandas dataframe with ggplot in ipython notebook.

%%R -i data
plot = ggplot(data) + geom_line(aes(x=x, y=y))
print(plot)

The result is the expected plot in png format. I would like to have an svg image instead, but I just cannot figure out how this can be done.

like image 294
cel Avatar asked Mar 29 '26 22:03

cel


1 Answers

In previous versions of rpy2 my proposed solution did not work. However with rpy2-2.4.3 the following now works as expected:

%load_ext rpy2.ipython 
%R require(ggplot2)
%Rdevice svg

After setting up R, the plotting works as expected and the shown plot will be in svg format.

Create data as a pandas DataFrame in python:

import pandas as pd
data = pd.DataFrame({"x":[1,2,3], "y": [3,2,1]})

And finally do the plotting:

%%R -i data
plot = ggplot(data) + geom_line(aes(x=x, y=y))
print(plot)
like image 130
cel Avatar answered Apr 02 '26 13:04

cel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!