This article provides three different ways to invoke Ggplot in Python (IPython notebook).
In the large data age, data visualization is a very hot topic. Every bi manufacturer invests a lot of energy in the field of data visualization. Tableau, with its powerful data visualization capabilities, has become a hot-market company in Silicon Valley. Tableau data visualization of the product, its theoretical basis is actually "the grammar of Graphic", the book presents the information visualization diagram of the syntax of the abstract system, data exploration and analysis can be driven by the syntax of the image, rather than a fixed chart type to drive, Makes the data discovery process friendly and interesting.
However, the practice of the theory of the grammar of graphic is not tableau exclusive, ggplot as a graphic library in R language, and its theoretical basis is also the book. (Note, I worked in a bi-giant, the main responsibility is also data visualization, we have and the Canadian team developed similar products, based on HTML5 and D3, unfortunately for a variety of reasons not to market)
Now more and more people are starting to use Python to do data analysis, IPython notebook is particularly popular, its real-time interaction has the advantage of scripting language to maximize. So how can you use Ggplot in Ipython notebook? I'm here to share three different ways for you to choose.
RPy2
The first approach is to use Rpy2, Rpy2 is a rewrite and redesign of Rpy, designed to provide an API for Python users to use R in Python.
RPY2 provides a basic encapsulation of objects and methods for the R language, including, of course, a visual gallery piece.
Here's an example of an R program running Ggplot using RPY2 to run in Python:
From rpy2 import robjects
to rpy2.robjects import Formula, Environment from
rpy2.robjects.vectors Import Intvector, floatvector from
rpy2.robjects.lib import grid
from rpy2.robjects.packages import IMPORTR, data
import Rpy2.robjects.lib.ggplot2 as Ggplot2
# the R ' print ' function
rprint = Robjects.globalenv.get (" print ")
stats = importr (' stats ')
grdevices = Importr (' grdevices ')
base = Importr (' base ')
datasets = IMPORTR (' Datasets ')
mtcars = data (datasets). Fetch (' mtcars ') [' Mtcars ']
pp = Ggplot2.ggplot (mtcars) + \
ggplot2.aes_string (x= ' wt ', y= ' mpg ', col= ' factor (cyl) ') + \
ggplot2.geom_point () + \
ggplot2.geom_ Smooth (ggplot2.aes_string (group = ' cyl '), Method
= ' lm ')
Pp.plot ()
The above program will be defective in Ipython notebook, a new window will pop up and the Python process will block there. We want the chart to be embedded in the Ipython notebook page, and in order to solve the problem, we introduce the following code:
%matplotlib inline
import uuid from
rpy2.robjects.packages import importr from
IPython.core.display Import Image
grdevices = importr (' grdevices ')
def ggplot_notebook (gg, width = +, height = N):
fn = ' {uuid} ' . png '. Format (uuid = Uuid.uuid4 ())
grdevices.png (fn, width = width, height = height)
gg.plot ()
Grdevices.dev_off () return
Image (FILENAME=FN)
After running the above code, we can successfully embed the result of the display Ggplot with the Ggplot call Pp.plot () instead of calling Ggplot_notebook (PP, height=300).
Rmagic
Another way is to use rmagic,rmagicy to actually rely on rpy2. It's more likely to be used directly in the use of R
%load_ext rmagic
Library (ggplot2)
dat <-data.frame (x = Rnorm (ten), y = Rnorm (Ten),
lab = sample (C (' A '), ' B '), replace = TRUE)
x <-ggplot (DAT, AES (x = x, y = y, color = Lab)) + geom_point ()
print (x)
The results of the operation are as follows
Ggplot for Python
Ggplot is a Python library, essentially porting the functions of R language Ggplot to Python.
Running Setup Scripts
After the installation is successful, you can try this example
%matplotlib inline
Import pandas as PD from
ggplot import *
meat_lng = pd.melt (meat[[' date ', ' beef ', ' pork ', ' Broilers ']], id_vars= ' date ')
Ggplot (Aes (x= ' Date ', y= ' value ', colour= ' variable '), data=meat_lng) + \
geom_ Point () + \
stat_smooth (color= ' red ')
The results are as follows:
Summarize
This article provides three different ways to invoke Ggplot in Python (IPython notebook).
Rpy2 and Rmagic are all bridges to r, so you need to install R. The difference is that RPY2 provides a python interface and rmagic closer to R.
The Ggplot Python library is a ggplot python transplant, so it's simpler to deploy without installing r, but there may be a gap in functionality with R's Ggplot.
You can make a choice according to your needs.