果断收藏!python数据分析入门学习笔记(下)

目录

七、 可视化

八、 其它

六.可视化

  我觉得吧,其实看着excel就可以实现的功能为何那么复杂,excel确实够通用够便捷,但是处理很大数据量的话也许吃不消吧。学学python绘图也不赖,而且讲真,有的成效真的挺好看的。

(一)Seaborn

<code>我学数据分析可视化是从学习Seaborn入门的,Seaborn是基于matplotlib的Python可视化库,刚开始便接触matplotlib难免有些吃力,参数多且难理解,但是慢慢来总会学会的。还有关键的一点是,seaborn画出来的图好好看。。/<code>
<code>#基础导入
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib as mpl
import matplotlib.pyplot as plt/<code>


<code>#小费数据真的挺好的,这儿用tips作为example
tips = sns.load_dataset('tips') #从网络环境导入数据tips/<code>

1.lmplot函数

lmplot(x, y, data, hue=None, col=None, row=None, palette=None, col_wrap=None, size=5, aspect=1, markers='o', sharex=True, sharey=True, hue_order=None, col_order=None, row_order=None, legend=True, legend_out=True, x_estimator=None, x_bins=None, x_ci='ci', scatter=True, fit_reg=True, ci=95, n_boot=1000, units=None, order=1, logistic=False, lowess=False, robust=False, logx=False, x_partial=None, y_partial=None, truncate=False, x_jitter=None, y_jitter=None, scatter_kws=None, line_kws=None)功能:Plot data and regression model fits across a FacetGrid.

下面就不同的例子,对lmplot的参数进行解释

例子1. 画出总账单和小费回归关系图

用到了lmplot(x, y, data,scatter_kws)

x,y,data一目了然这儿就不多解释了,scatter_kws和line_kws的官方解释如下:

{scatter,line}_kws : dictionarie

Additional keyword arguments to pass to plt.scatter and plt.plot.

scatter为点,line为线。其实就是用字典去限定点和线的各种属性,如例子所示,散点的颜色为灰石色,线条的颜色为印度红,成像效果就是这样点线颜色分离,展现效果很好。大家也可以换上自己想要的图片属性。

<code>sns.lmplot("total_bill", "tip", tips,
scatter_kws={"marker": ".", "color": "slategray"},
line_kws={"linewidth": 1, "color": "indianred"}).savefig('picture2') /<code>
果断收藏!python数据分析入门学习笔记(下)

另外:颜色还可以使用RGB代码,具体对照表可以参考这个网站,可以自己搭配颜色:

http://www.114la.com/other/rgb.htm

marker也可以有多种样式,具体如下:

. Point marker,

Pixel marker

o Circle marker

v Triangle down marker

^ Triangle up marker

< Triangle left marker

> Triangle right marker

1 Tripod down marker

2 Tripod up marker

3 Tripod left marker

4 Tripod right markers

Square marker

p Pentagon marker

* Star marker

h Hexagon marker

H Rotated hexagon D Diamond marker

d Thin diamond marker

| Vertical line (vlinesymbol) marker

_ Horizontal line (hline symbol) marker

+ Plus markerx Cross (x) marker

<code>sns.lmplot("total_bill", "tip", tips,
scatter_kws={"marker": ".","color":"#FF7F00"},
line_kws={"linewidth": 1, "color": "#BF3EFF"}).savefig('s1')

ps.我修改maker属性不成功不知为何,求解答/<code>

果断收藏!python数据分析入门学习笔记(下)

例子2.用餐人数(size)和小费(tip)的关系图

官方解释:

x_estimator : callable that maps vector -> scalar, optional

Apply this function to each unique value of x and plot the resulting estimate. This is useful when x is a discrete variable. If x_ci is not None, this estimate will be bootstrapped and a confidence interval will be drawn.

大概解释就是:对拥有相同x水平的y值进行映射

<code>plt.figure()
sns.lmplot('size', 'tip', tips, x_estimator= np.mean).savefig('picture3')/<code>
果断收藏!python数据分析入门学习笔记(下)

{x,y}_jitter : floats, optional

Add uniform random noise of this size to either the x or y variables. The noise is added to a copy of the data after fitting the regression, and only influences the look of the scatterplot. This can be helpful when plotting variables that take discrete values.

jitter是个很有意思的参数, 特别是处理靶数据的overlapping过于严重的情况时, 通过增加一定程度的噪声(noise)实现数据的区隔化, 这样原始数据是若干 点簇 变成一系列密集邻近的点群. 另外, 有的人会经常将 rug 与 jitter 结合使用. 这依人吧.对于横轴取离散水平的时候, 用x_jitter可以让数据点发生水平的扰动.但扰动的幅度不宜过大。

<code>sns.lmplot('size', 'tip', tips, x_jitter=.15).savefig('picture4')/<code>
果断收藏!python数据分析入门学习笔记(下)

seaborn还可以做出xkcd风格的图片,还挺有意思的

<code>with plt.xkcd():
sns.color_palette('husl', 8)
sns.set_context('paper')
sns.lmplot(x='total_bill', y='tip', data=tips, ci=65).savefig('picture1')/<code>
果断收藏!python数据分析入门学习笔记(下)


<code>sns.jointplot("total_bill", "tip", tips).savefig('picture9')/<code>
果断收藏!python数据分析入门学习笔记(下)

<code>with plt.xkcd():
sns.lmplot('total_bill', 'tip', data=tips, hue='smoker')
plt.xlabel('hue = smoker')
plt.savefig('picture6')/<code>
果断收藏!python数据分析入门学习笔记(下)

<code>sns.set_style('dark')
sns.set_context('talk')
sns.lmplot('size', 'total_bill', tips, order=2)
plt.title('# poly order = 2')
plt.savefig('picture7')
plt.figure()
sns.lmplot('size', 'total_bill', tips, order=3)
plt.title('# poly order = 3')
plt.savefig('picture8')/<code>


果断收藏!python数据分析入门学习笔记(下)

(二)matplotlib ********待完善

七.其它~

(一)调用R

让Python直接调用R的函数,下载安装rpy2模块即可~

具体步骤:http://www.geome.cn/posts/python-%E9%80%9A%E8%BF%87rpy2%E8%B0%83%E7%94%A8-r%E8%AF%AD%E8%A8%80/

亲测可用~ 大大大大大前提:电脑上安装了R

(二)ipython ********待完善

end.


关注我们吧,查看更多干货文章,视频。回复“数据”还有数据分析相关资料领取,每周更有免费直播课,有问题也可私信咨询小编哦!


分享到:


相關文章: