详解在Python中调用ggplot的三种方法(图文)
本文提供了三种不同的方式在Python(IPython Notebook)中调用ggplot。
在大数据时代,数据可视化是一个非常热门的话题。各个BI的厂商无不在数据可视化领域里投入大量的精力。Tableau凭借其强大的数据可视化的功能成为硅谷炙手可热的上市公司。Tableau的数据可视化的产品,其理论基础其实是《The Grammar of Graphic》,该书提出了对信息可视化的图表的语法抽象体系,数据的探索和分析可以由图像的语法来驱动,而非有固定的图表类型来驱动,使得数据的探索过程变得友好而有趣。
然而对于The Grammar of Graphic的理论的实践,并非Tableau独占,ggplot作为R语言上得一个图形库,其理论基础也是这本书。(注,笔者曾就职的某BI巨头,主要职责也是数据可视化,我们曾经和加拿大团队研发过类似的产品,基于HTML5和D3,可惜由于种种原因未能推向市场)
现在越来越多的人开始使用python来做数据分析,IPython Notebook尤其令人喜爱,它的实时交互把脚本语言的优势发挥到极致。那么怎样才能在IPython Notebook中使用ggplot呢?我这里跟大家分享三种不同的方式供大家选择。
RPy2
第一种方式是使用rpy2, rpy2是对rpy的改写和重新设计,旨在提供Python用户在python中使用R的API。
rpy2提供了对R语言的对象和方法的基本封装,当然也包括可视化的图库这一块。
下面就是一段运行ggplot的R程序使用rpy2在python中运行的例子:
from rpy2 import robjects from 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()
以上程序在IPython Notebook中运行会有缺陷,会弹出一个新的窗口显示图,而且该python进程会阻塞在那里。我们希望图表能内嵌在IPython Notebook的页面中,为了解决该问题,我们引入如下代码:
%matplotlib inline import uuid from 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)
运行上述代码后,我们把ggplot的调用pp.plot()改为调用ggplot_notebook(pp, height=300)就能成功嵌入显示ggplot的结果。
RMagic
另一种方式是使用rmagic,rmagicy实际上依赖于rpy2。它的使用方式更像是直接在使用R
%load_ext rmagic library(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)
运行结果如下
ggplot for python
ggplot是一个python的库,基本上是对R语言ggplot的功能移植到Python上。
运行安装脚本
pip install ggplot
安装成功后,可以试一下这个例子
%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')
结果如下:
总结
本文提供了三种不同的方式在Python(IPython Notebook)中调用ggplot。
rpy2和Rmagic都是一种对R的桥接,所以都需要安装R。不同之处在于rpy2提供Python接口而Rmagic更接近R。
ggplot Python库是ggplot的Python移植,所以无需安装R,部署起来更为简单,但功能上也许和R的ggplot还有差距。
大家可以根据自己的需要做出选择。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

Golang is more suitable for high concurrency tasks, while Python has more advantages in flexibility. 1.Golang efficiently handles concurrency through goroutine and channel. 2. Python relies on threading and asyncio, which is affected by GIL, but provides multiple concurrency methods. The choice should be based on specific needs.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
