python ggplot使用文档(1)

来源:互联网 发布:猎聘 JAVA 编程题 编辑:程序博客网 时间:2024/06/07 02:03

此文档是基于https://github.com/yhat/ggpy/tree/master/docs翻译的


ggplot

ggplot函数定义了一个底层,可以基于这个底层往上面添加表的部件(如x、y轴,形状、颜色等)通过添加部件,你可以通过较少的工作来建造复杂图表。ggplot函数有2个参数,如下

  • 画图所需要的数据。为DataFrame形式。
  • 数据将如何展现(aesthetics)

你可以基于ggplot上添加层。例子如下,我们通过diamonds数据集并定义了x轴为price来初始化ggplot,之后在ggplot上添加直方图(histogram)图层。最后通过函数scale_x_continuous来定制x轴

from ggplot import *p = ggplot(aes(x='price'), data=diamonds)p + geom_histogram()

这里写图片描述

p + geom_histogram() + scale_x_continuous("Price ($)", breaks=[0, 10000, 20000])

这里写图片描述

Aesthetics

该参数用来定义数据在图表上面如何展现。有一些参数是在所有方法中共享的,例如xycolor。有一些参数取决于所使用的函数。例如,画离散点geom_point和画线geom_line都有x,y参数,但是geom_line画线是会有linetype参数。
虽然该函数某些参数取决于所使用的函数,但此处还是给出了所有的参数

  • x:x轴取值。可用在连续图表(point,line)或离散图表(bar,histogram
  • y:y轴取值。只可用在连续图表(point,line
  • color:定义层的颜色。可以用在连续或离散图表。如果是用在连续的图表,其将会定义为两个颜色的颜色梯度(If continuous, this will be given a color gradient between 2 colors)。
  • shape:点的形状。仅用于geom_point
  • size:点或线的大小。
  • alpha:透明度。在[0,1]区间中取值。
  • ymin :y轴最小值
  • ymax :y轴最大值
  • xmin :x轴最小值。仅用与geom_hline
  • xmax :x轴最大值。仅用与geom_hline
  • slope :斜率。仅用与geom_hline
  • intercept :截距。仅用与geom_hline

    示例

from ggplot import *# set the x-axis equal to the `date` column and the y-axis equal to the `beef` columnmy_aes = aes(x='date', y='beef') my_aesggplot(meat, my_aes) + geom_line()

这里写图片描述

# normally, aes are defined within the `ggplot` constructorggplot(aes(x='carat', y='price'), diamonds) + geom_point()

这里写图片描述

# adding in another aesthetic into the mix...ggplot(aes(x='carat', y='price', color='clarity'), diamonds) + geom_point()

这里写图片描述

qplot

qplot(‘quickplot’)是ggplot的简易版。其提供类似于python中plot的用法,接受list和numpy,array来做直方图(histogram),散点图(scatterplot)及其他函数

from ggplot import *import numpy as np

这里写图片描述

qplot(diamonds.carat, diamonds.price)

这里写图片描述

x = range(100)y = range(100)qplot(x, y)

这里写图片描述

qplot(np.random.normal(0, 1, 10000))

这里写图片描述


参考文献
[1] https://github.com/yhat/ggpy/tree/master/docs

原创粉丝点击