python使用Pandas,数据可视化

在这里插入图片描述

 

文章目录

 


pandas数据可视化

其实,pandas作图是个什么效果大家心里应该有数,不然也不会有那么多其他的库来作图,或者说,pandas本身是一个做数据分析的库,作图的话,可能并不是它的本职工作。

但是呢,既然人家有,那我就放一下。

不急,文章后面会有比较好的pandas可视化工具。
(如果性子急的朋友可以从目录直接跳转到下面的第二个大板块儿)

线形图

import pandas as pd
import numpy as np
 
s = Series( np. random. randn( 10). cumsum(), index= np. arange( 0, 100, 10))
s. plot()
  • 1
  • 2
  • 3
  • 4
  • 5

在这里插入图片描述

条形图

df = pd.DataFrame(np.random.rand(10,4),columns=['a','b','c','d')
df.plot.bar()
  • 1
  • 2

在这里插入图片描述

堆积条形图

传递stacked = True :

df = pd.DataFrame(np.random.rand(10,4),columns=['a','b','c','d')
df.plot.bar(stacked=True)
  • 1
  • 2

在这里插入图片描述

水平条形图

要获得水平条形图,请使用 barh 方法:

df = pd.DataFrame(np.random.rand(10,4),columns=['a','b','c','d')

df.plot.barh(stacked=True)
  • 1
  • 2
  • 3

在这里插入图片描述

直方图

df = pd.DataFrame({'a':np.random.randn(1000)+1,'b':np.random.randn(1000),'c':
np.random.randn(1000) - 1}, columns=['a', 'b', 'c'])
 
df.plot.hist(bins=20)
  • 1
  • 2
  • 3
  • 4

在这里插入图片描述

箱型图

df = pd.DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E'])
df.plot.box()
  • 1
  • 2

在这里插入图片描述

区域图

df = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
df.plot.area()
  • 1
  • 2

在这里插入图片描述

散点图

df = pd.DataFrame(np.random.rand(50, 4), columns=['a', 'b', 'c', 'd'])
df.plot.scatter(x='a', y='b')
  • 1
  • 2

在这里插入图片描述

饼状图

df = pd.DataFrame(3 * np.random.rand(4), index=['a', 'b', 'c', 'd'], columns=['x'])
df.plot.pie(subplots=True)
  • 1
  • 2

在这里插入图片描述


plotly数据可视化

pandas现在可以使用Plotly、Bokeh作为可视化的backend,直接实现交互性操作,无需再单独使用可视化包了。

激活backend:

pd.options.plotting.backend = 'plotly'
  • 1

可以填在上面的有:

Plotly
Holoviews
Matplotlib
Pandas_bokeh
Hyplot
  • 1
  • 2
  • 3
  • 4
  • 5

Plotly backend简介

Plotly是基于Javascript版本的库写出来的,因此生成的Web可视化图表,可以显示为HTML文件或嵌入基于Python的Web应用程序中。
使用Plotly可以画出很多媲美Tableau的高质量图:
在这里插入图片描述

此前一直是苦于pyecharm的模板不够多,前端不熟的小伙伴,这可是一个好东西哦!!!


if Jupyter

如果是在Jupyterlab中使用Plotly,那还需要执行几个额外的安装步骤来显示可视化效果。

首先,安装IPywidgets。

pip install jupyterlab "ipywidgets>=7.5"
  • 1

然后运行此命令以安装Plotly扩展。

jupyter labextension install jupyterlab-plotly@4.8.1
  • 1

好,我用的是pyechart


数据集选取

数据集来源

这个数据也是Scikit-learn中的样本数据,所以也可以使用以下代码将其直接导入。

import pandas as pd
import numpy as np

from sklearn.datasets import fetch_openml

pd.options.plotting.backend = 'plotly'

X,y = fetch_openml("wine", version=1, as_frame=True, return_X_y=True)
data = pd.concat([X,y], axis=1)
print(data.head().T)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
                                     0       1       2       3      4
Alcohol                          14.23    13.2   13.16   14.37  13.24
Malic_acid                        1.71    1.78    2.36    1.95   2.59
Ash                               2.43    2.14    2.67     2.5   2.87
Alcalinity_of_ash                 15.6    11.2    18.6    16.8   21.0
Magnesium                        127.0   100.0   101.0   113.0  118.0
Total_phenols                      2.8    2.65     2.8    3.85    2.8
Flavanoids                        3.06    2.76    3.24    3.49   2.69
Nonflavanoid_phenols              0.28    0.26     0.3    0.24   0.39
Proanthocyanins                   2.29    1.28    2.81    2.18   1.82
Color_intensity                   5.64    4.38    5.68     7.8   4.32
Hue                               1.04    1.05    1.03    0.86   1.04
OD280%2FOD315_of_diluted_wines    3.92     3.4    3.17    3.45   2.93
Proline                         1065.0  1050.0  1185.0  1480.0  735.0
class                                1       1       1       1      1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

该数据集是葡萄酒相关的,包含葡萄酒类型的许多功能和相应的标签。


开始绘图

散点图

绘图方式与正常使用Pandas内置的绘图操作几乎相同,只是现在以丰富的Plotly显示可视化效果。

fig = data[['Alcohol', 'Proline']].plot.scatter(y='Alcohol', x='Proline')
fig.show()
  • 1
  • 2

在这里插入图片描述

放大缩小,矢量图,你懂得。


条形辉度图

我们可以结合Pandas的groupby函数创建一个条形图,总结各类之间Hue的均值差异。

fig = data[['Hue','class']].groupby(['class']).mean().plot.bar()
  • 1

在这里插入图片描述


散点分类图

将class添加到我们刚才创建的散点图中。通过Plotly可以轻松地为每个类应用不同的颜色,以便直观地看到分类。

fig = data[['Hue', 'Proline', 'class']].plot.scatter(x='Hue', y='Proline', color='class', title='Proline and Hue by wine class')
  • 1

在这里插入图片描述


资料推荐

这里示例就放这些,比较简单一些。
这里不建议大家去看各种网文,质量都参差不齐的,直接去看官方文档。

Python Figure Reference: Single-Page
Plotly Python Open Source Graphing Library Fundamentals

推一篇还不错的博文:可视化神器Plotly(5)—参数详解


在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值