全文共字,预计学习时长10分钟
图源:tehrantimes流行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,则需要使用pipintsallplotly来安装。如果是在Jupyterlab中使用Plotly,则需要额外执行几个安装步骤来显示可视化效果。首先,安装IPywaidgets:
pipenvinstalljupyterlabipywidgets=7.5
pipinstalljupyterlabipywidgets=7.5
然后运行以下命令以安装Plotly扩展:
jupyterlabextensioninstalljupyterlab-plotly
4.8.1为了说明绘图后端的用法,使用openml.org名为“wine(葡萄酒)”的数据集。
importpandasaspd
importnumpyasnp
fromsklearn.datasetsimportfetch_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()
该数据集由各类葡萄酒的多个特征和相应的标签组成。下图显示了数据集的前几行。
绘图功能的工作方式与标准Pandas绘图功能的工作方式大致相同,只是现在可视化效果同Plotly一样丰富。下面的代码绘制了数据集中两个特征之间的关系。
fig=data[[Alcohol,Proline]].plot.scatter(y=Alcohol,x=Proline)
fig.show()
可以通过组合Pandas的groupby函数创建一个柱状图来总结类之间的平均色调差异:
data[[Hue,class]].groupby([class]).mean().plot.bar()
将类添加到之前创建的散点图中。使用Plotly,可以轻松地给每个类使用不同的颜色,以便直观地区分:
fig=data[[Hue,Proline,class]].plot.scatter(x=Hue,y=Proline,color=class,title=ProlineandHuebywineclass)
fig.show()
Bokeh后端
Bokeh也可以提供丰富交互式可视化效果。其可视化图表可以在Web浏览器中查看,嵌入到Web应用程序中或用于创建交互式仪表板。Bokeh甚至有一个流式API,可以为流数据(如金融市场数据)创建实时可视化图表。
库可以通过pip来安装:
pipinstallpandas-bokeh
要在Jupyterlab中显示Bokeh的可视化效果,需要安装两个新的扩展:
jupyterlabextensioninstall
jupyter-widgets/jupyterlab-managerjupyterlabextensioninstallbokeh/jupyter_bokeh使用Bokeh后端重新创建之前的散点图:
pd.options.plotting.backend=pandas_bokeh
importpandas_bokeh
frombokeh.ioimportoutput_notebook
frombokeh.plottingimportfigure,show
output_notebook()
p1=data.plot_bokeh.scatter(x=Hue,
y=Proline,
category=class,
title=ProlineandHuebywineclass,
show_figure=False)
show(p1)
可视化效果如下:
Bokeh有一个plot_grid函数,可为多个图表创建仪表板式布局。下面的代码在网格布局中创建四个图表:
output_notebook()
p1=data.plot_bokeh.scatter(x=Hue,
y=Proline,
category=class,
title=ProlineandHuebywineclass,
show_figure=False)
p2=data[[Hue,class]].groupby([class]).mean().plot.bar(title=MeanHueperClass)
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=DistributionperClass: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=DistributionperClass:Proline)
pandas_bokeh.plot_grid([[p1,p2],
[p3,p4]],plot_width=)
为内置的Pandas绘图功能添加多个第三方后端,这大大增强了该库用于数据可视化的能力。从此之后,pandas就可以集美貌与实用于一身啦。
留言点赞