云计算|Pandas万花筒:让绘图变得更美观

来源 : 99API发布 : 2020-07-20 11:34:48访问数 : 3点赞数 : 1

  流行 Python 数据分析库 Pandas 中的绘图功能一直是迅速绘制图表的首选之一。但是,其可用的可视化效果总是十分粗略,实用有余、美观不足。

  笔者常用 Pandas 的绘图功能快速地执行一些可视的数据探索,但在介绍数据洞察时,我会使用“更美观”的绘图库(如 Plotly 或 Bokeh )来重做可视化。

  自最新的 Pandas 版本0.25.3发布后,无需这样做了,现在我们可以使用第三方可视化库作为 Pandas 绘图功能的后端。Plotly是一款基于 web 实现交互式可视化的流行Python库,其最近发布了 Pandas绘图后端。

  来看看如何使用 Plotly 和 Bokeh 后端创建更丰富的可视化效果。

  使用不同的后端

  想要激活绘图功能的不同后端需在导入 pandas 后,添加此行代码:

  pd.options.plotting.backend = 'plotly'

  当前可用的后端有:

  Plotly Holoviews Matplotlib Pandas _bokeh Hyplot

  Plotly后端

  Plotly是一个 Python库,其支持丰富的交互式可视化效果。Plotly包的好处之一在于它是在库的 Javascript 版本之上构建的,这意味着图表会基于Web,可以显示为 HTML 文件或嵌入到基于Python的Web应用程序中。用户还可以将可视化内容下载为高质量的图像文件,以便在文档或论文中使用。

  下面来浏览一些Plotly作为Pandas绘图后端的快速示例。

  如果还没有安装Plotly ,则需要使用pip intsall plotly来安装。如果是在Jupyterlab中使用 Plotly ,则需要额外执行几个安装步骤来显示可视化效果。首先,安装IPywaidgets:

  pipenv install jupyterlab " ipywidgets>=7.5" pip install jupyterlab "ipywidgets>=7.5"

  然后运行以下命令以安装Plotly扩展:

  jupyter labextension install jupyterlab-plotly@4.8.1

  为了说明绘图后端的用法,使用openml.org名为“wine(葡萄酒)”的数据集。

  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) data.head()

  该数据集由各类葡萄酒的多个特征和相应的标签组成。下图显示了数据集的前几行。

529192d082ab4f46a45832eff6fc8d1c.png

  绘图功能的工作方式与标准Pandas绘图功能的工作方式大致相同,只是现在可视化效果同Plotly一样丰富。下面的代码绘制了数据集中两个特征之间的关系。

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

  可以通过组合 Pandas 的groupby函数创建一个柱状图来总结类之间的平均色调差异:

  data[['Hue','class']].groupby(['class']).mean().plot.bar()

c6cf428a86594134bd29f2f10de21bc0.png

  将类添加到之前创建的散点图中。使用Plotly,可以轻松地给每个类使用不同的颜色,以便直观地区分:

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

d0e1d98b34e749e7b03f5cda4f358de8.png

  Bokeh 后端

  Bokeh 也可以提供丰富交互式可视化效果。其可视化图表可以在 Web 浏览器中查看,嵌入到 Web应用程序中或用于创建交互式仪表板。Bokeh 甚至有一个流式 API,可以为流数据(如金融市场数据)创建实时可视化图表。

  库可以通过pip来安装:

  pip install pandas-bokeh

  要在 Jupyterlab中显示 Bokeh的可视化效果,需要安装两个新的扩展:

  jupyter labextension install @jupyter-widgets/jupyterlab-managerjupyterlabextension install @bokeh/jupyter_bokeh

  使用 Bokeh 后端重新创建之前的散点图:

  pd.options.plotting.backend ='pandas_bokeh' import pandas_bokeh from bokeh.io import output_notebook from bokeh.plotting import figure, show output_notebook() p1= data.plot_bokeh.scatter(x='Hue', y='Proline', category='class', title='Proline and Hue by wine class', show_figure=False) show(p1)

  可视化效果如下:

5e12ae5926444ab5bfa834f89b6fc7ac.png

  Bokeh 有一个plot_grid函数,可为多个图表创建仪表板式布局。下面的代码在网格布局中创建四个图表:

  output_notebook() p1 = data.plot_bokeh.scatter(x='Hue', y='Proline', category='class', title='Proline and Hue by wine class', show_figure=False) p2 = data[['Hue','class']].groupby(['class']).mean().plot.bar(title='Mean Hue per Class') df_hue = pd.DataFrame({ 'class_1': data[data['class'] =='1']['Hue'], 'class_2': data[data['class'] =='2']['Hue'], 'class_3': data[data['class'] =='3']['Hue']}, columns=['class_1', 'class_2', 'class_3']) p3 = df_hue.plot_bokeh.hist(title='Distribution perClass: Hue') df_proline = pd.DataFrame({ 'class_1': data[data['class'] =='1']['Proline'], 'class_2': data[data['class'] =='2']['Proline'], 'class_3': data[data['class'] =='3']['Proline']}, columns=['class_1', 'class_2', 'class_3']) p4 =df_proline.plot_bokeh.hist(title='Distribution per Class: Proline') pandas_bokeh.plot_grid([[p1, p2], [p3, p4]], plot_width=450)

1b1262931a8a42588e20e0a069ece961.png

  为内置的Pandas绘图功能添加多个第三方后端,这大大增强了该库用于数据可视化的能力。从此之后,pandas就可以集美貌与实用于一身啦。


1

电子邮箱:

service@99api.com

联系电话:

17350028402

微信客服:

电子邮箱:

service@99api.com

联系电话:

17350028402

微信客服: