Three ways to call Ggplot in Python (graphic)

Source: Internet
Author: User
Tags ggplot
This article provides three different ways to call Ggplot in Python (IPython Notebook).

In the big data age, data visualization is a very hot topic. Every BI vendor invests 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 is actually the Grammar of Graphic, which presents a syntax abstraction system for the visualization of information, and the exploration and analysis of the data can be driven by the syntax of the image, rather than by a fixed chart type, Makes the process of data discovery friendly and interesting.

However, for the Grammar of Graphic's theoretical practice, not tableau exclusive, ggplot as the R language to get a graphics library, its theoretical basis is the book. (note, I have worked for a BI giant, the main responsibility is also data visualization, we have developed similar products with the Canadian team, based on HTML5 and D3, unfortunately for various reasons failed 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 to the scripting language advantage to play to the extreme. So how can you use Ggplot in Ipython notebook? I am here to share with you three different ways for you to choose.
RPy2

The first is to use Rpy2, Rpy2 is a rewrite and redesign of Rpy, designed to provide Python users with an API for using R in Python.

RPY2 provides a basic encapsulation of objects and methods of the R language, including, of course, a visual gallery.

Here is an example of an R program running Ggplot using Rpy2 in Python:

From rpy2 import robjectsfrom rpy2.robjects import Formula, environmentfrom rpy2.robjects.vectors import Intvector, Float Vectorfrom 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 (' STA ts ') 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 display will pop up, and the Python process will block there. We want the chart to be embedded in the Ipython notebook page, in order to solve the 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 = +, height = +):  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 change the Ggplot call Pp.plot () to Call Ggplot_notebook (PP, height=300) to successfully embed the result of the display ggplot.

Rmagic

Another way to use rmagic,rmagicy is to actually rely on rpy2. It is more likely to be used directly in the use of R

%load_ext rmagiclibrary (ggplot2) Dat <-data.frame (x = rnorm), y = Rnorm (Ten),          lab = sample (C (' A ', ' B '), ten, Repla CE = TRUE) x <-ggplot (DAT, AES (x = x, y = y, color = Lab)) + Geom_point () print (x)

The operation results are as follows

Ggplot for Python

Ggplot is a python library, basically porting the functionality of the R language Ggplot to Python.

Run the installation script

Pip Install Ggplot

Once the installation is successful, you can 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 results are as follows:

Summarize

This article provides three different ways to call Ggplot in Python (IPython Notebook).

Rpy2 and Rmagic are both a bridge to r, so you need to install R. The difference is that RPY2 provides a Python interface and Rmagic is closer to R.

The Ggplot Python library is a ggplot python migration, so there's no need to install r, and it's easier to deploy, but there may be a difference in functionality with R's Ggplot.

You can make a choice according to 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.