Three methods of calling ggplot in Python: pythonggplot

Source: Internet
Author: User
Tags ggplot

Three methods of calling ggplot in Python: pythonggplot

This article provides three different methods to call ggplot in Python (IPython Notebook.

In the big data era, data visualization is a hot topic. Every BI manufacturer has invested a lot of energy in the field of data visualization. With its powerful data visualization capabilities, Tableau has become a hot listed company in Silicon Valley. The theoretical basis of Tableau's data visualization product is actually The Grammar of Graphic. This book proposes a syntax abstraction system for information visualization charts, data exploration and analysis can be driven by image syntax, rather than fixed chart types, making the data exploration process friendly and interesting.

However, The practice of The Grammar of Graphic theory is not exclusive to Tableau. ggplot is a graphics library on The R language, and its theoretical basis is also this book. (Note: a bi giant I once worked for is mainly responsible for data visualization. We once developed similar products with the Canadian team, based on HTML5 and D3, unfortunately, due to various reasons, the product cannot be pushed to the market)

More and more people are using python for data analysis. IPython Notebook is especially popular. Its real-time interaction brings the advantages of scripting language to the extreme. So how can ggplot be used in IPython Notebook? I would like to share with you three different ways for you to choose.
RPy2

The first method is to use rpy2. rpy2 is to rewrite and redesign rpy to provide Python users with R APIs in python.

Rpy2 provides basic encapsulation of the objects and methods of the R language, including the visual image library.

The following is an example of running a ggplot R program using rpy2 in python:
 

from rpy2 import robjectsfrom rpy2.robjects import Formula, Environmentfrom rpy2.robjects.vectors import IntVector, FloatVectorfrom rpy2.robjects.lib import gridfrom rpy2.robjects.packages import importr, dataimport rpy2.robjects.lib.ggplot2 as ggplot2 # The R 'print' functionrprint = 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 be blocked. We hope that the chart can be embedded in the IPython Notebook page. To solve this problem, we introduce the following code:
 

%matplotlib inline import uuidfrom rpy2.robjects.packages import importr from IPython.core.display import Image grdevices = importr('grDevices')def ggplot_notebook(gg, width = 800, height = 600):  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 change the call pp. plot () of ggplot to call ggplot_notebook (pp, height = 300) to successfully embed and display ggplot results.

RMagic

Another way is to use rmagic. rmagicy actually depends on rpy2. It is more like using R directly.
 

%load_ext rmagiclibrary(ggplot2)dat <- data.frame(x = rnorm(10), y = rnorm(10),          lab = sample(c('A', 'B'), 10, replace = TRUE))x <- ggplot(dat, aes(x = x, y = y, color = lab)) + geom_point()print(x)

The running result is as follows:

Ggplot for python

Ggplot is a python library and is basically transplanted to Python for the function of ggplot in R language.

Run the installation script

pip install ggplot

After the installation is successful, try this example.
 

%matplotlib inlineimport pandas as pdfrom 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 result is as follows:

Summary

This article provides three different methods to call ggplot in Python (IPython Notebook.

Both rpy2 and Rmagic are bridging R, so R needs to be installed. The difference is that rpy2 provides the Python interface and Rmagic is closer to R.

The ggplot Python library is a Python port of ggplot, so you do not need to install R, which is easier to deploy, but there may be a gap between the function and the ggplot of R.

You can make choices based on your needs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.