Plotly is an interactive Python library that provides a wide range of visualisations accessible through a simple interface.
Plotly是一个交互式Python库,通过简单的界面即可提供广泛的可视化效果。
There are many different visualisation libraries available in Python. What sets Plotly apart are the interactivity of its plots, the richness and variety of visualisations, its relative simplicity, compared to say Matplotlib and the ability to deploy visualisations as web apps using the Dash module.
Python中有许多可用的可视化库。 与Matplotlib相比,Plotly与众不同之处在于其绘图的交互性,可视化的丰富性和多样性,相对简单性以及使用Dash模块将可视化部署为Web应用程序的能力。
There are many different parts to the Plotly suite of tools and when I first started using them I found it a little difficult to navigate. In this article, I want to give a brief introduction to the core elements of Plotly including the standard plotting module, Plotly express and Dash. Alongside some simple code, examples to get you up and running quickly with these tools.
Plotly工具套件有许多不同的部分,当我第一次开始使用它们时,我发现导航有些困难。 在本文中,我想简要介绍一下Plotly的核心元素,包括标准的绘图模块,Plotly express和Dash。 除了一些简单的代码外,还提供了一些示例,使您可以使用这些工具快速入门和运行。
1.标准绘图 (1. Standard plotting)
Plotly can be pip installed.
可以piply安装。
pip install plotly
Plotly visualisations are rendered as HTML files. If you are working in a Jupyter Notebook and would like to render the images directly you need to install the ipywidgets package.
直观地将可视化呈现为HTML文件。 如果您在Jupyter Notebook中工作,并且想直接渲染图像,则需要安装ipywidgets软件包。
pip install "notebook>=5.3" "ipywidgets>=7.2"
Or if you are using JupyterLab.
或者,如果您正在使用JupyterLab。
pip install jupyterlab "ipywidgets>=7.5"
jupyter labextension install jupyterlab-plotly@4.9.0
Plotly works with data structures known as figures which can either be represented as dictionaries, in which case you use the plotly.io
module. Or as graph objects rendered via the plotly.graph_objects
module.
Plotly使用称为图形的数据结构工作,这些数据结构可以表示为字典,在这种情况下,请使用plotly.io
模块。 或作为通过plotly.graph_objects
模块渲染的图形对象。
Graph objects are generally considered to be a better choice over dictionaries as they allow for precise data validation, support higher-level convenience functions for updating already constructed figures and the syntax of graph objects makes for more compact code.
通常认为图形对象是优于字典的选择,因为它们允许进行精确的数据验证,支持更高级别的便捷功能来更新已构造的图形,并且图形对象的语法使代码更紧凑。
Let’s import a toy data set and explore the basic functionality for standard plotting. The below code imports the Boston house prices data set, a popular toy data set for regression analysis from the scikit-learn library.
让我们导入一个玩具数据集并探索标准绘图的基本功能。 下面的代码导入了波士顿房价数据集,这是一个流行的玩具数据集,可从scikit-learn库进行回归分析。
The first few rows of the data are shown below.
数据的前几行如下所示。
Let’s use the graph objects module to explore the relationship between house price and the number of rooms. You will notice that I am using the helper functions I mentioned above to add titles to the visualisation.
让我们使用图形对象模块来探索房价和房间数量之间的关系。 您会注意到,我正在使用上面提到的帮助器功能为可视化添加标题。
2.情节快递 (2. Plotly express)
The standard plotting modules are useful if you need to create a bespoke visualisation. However, if you want to create something quite standard like the scatter plot shown above then the plotly.express
API is by far the best choice.
如果需要创建定制的可视化效果,则标准绘图模块非常有用。 但是,如果您想创建一些非常标准的东西,例如上面显示的散点图,那么到目前为止, plotly.express
API是最佳选择。
This module allows you to create entire figures in one line of code for most common visualisations. It also allows you to easily control colours, style and labelling with ease.
该模块允许您在一行代码中创建整个图形,以实现最常见的可视化效果。 它还使您可以轻松地轻松控制颜色,样式和标签。
We can create the above scatter plot with just one line of code using this module. Colour, labelling and style controls are all available with the px.scatter
function and the axis labels are automatically added.
我们可以使用此模块仅用一行代码来创建以上散点图。 px.scatter
函数可以使用颜色,标签和样式控件,并且会自动添加轴标签。
There are a large variety of options and controls for the ‘out of the box’ charts provided by plotly.express
, you can explore all options here.
plotly.express
提供了plotly.express
“即用型”图表选项和控件,您可以在此处浏览所有选项。
As an example, the below code creates a histogram to show the distributions for the CHAS variable. I have used the histnorm
option to apply normalization to better visualise the distribution and the hover_data
option to control the interactions on hover.
例如,下面的代码创建一个直方图以显示CHAS变量的分布。 我使用了histnorm
选项来应用规范化以更好地可视化分布,并使用了hover_data
选项来控制悬停时的交互。
3.短跑 (3. Dash)
Dash, also part of the Plotly suite of tools, is a framework for developing dashboards for data analysis and all Plotly visualisations can easily be embedded within the application.
Dash也是Plotly工具套件的一部分,是用于开发仪表板以进行数据分析的框架,所有Plotly可视化效果都可以轻松地嵌入到应用程序中。
Dash needs to be installed separately.
Dash需要单独安装。
pip install dash
It is possible to display Dash applications in Jupyterlab however you need to install this JupyterDash extension.
可以在Jupyterlab中显示Dash应用程序,但是您需要安装此JupyterDash扩展。
pip install "jupyterlab>=1.0" jupyterlab-dash==0.1.0a3
jupyter labextension install jupyterlab-dash@0.1.0-alpha.3
Alternatively, Dash will host the application on localhost, the address will be shown in the output when you run your code.
另外,Dash将在本地主机上托管应用程序,运行代码时,地址将显示在输出中。
Whenever fig.show
has been used to display a visualisation, using either Plotly express or standard plotting, you can pass the same plot to Dash.
每当使用fig.show
来显示可视化效果时,无论是使用Plotly Express还是标准绘图,都可以将相同的绘图传递给Dash。
The layout of a Dash app is determined using app.layout
which uses a combination of dash_core_components
and dash_html_components
to add charts, tables, interactivity and text to the dashboard.
破折号应用的布局是用确定的app.layout
其使用的组合dash_core_components
和dash_html_components
到图表,表格,交互性和文本添加到信息中心。
The code shown below creates a basic Dash app using the chart we created withplotly.express
. The resulting dashboard is shown below.
下面显示的代码使用我们使用plotly.express
创建的图表创建一个基本的Dash应用。 结果显示板如下所示。
One of the most useful aspects of Dash applications is that you can make your dashboards interactive by using callbacks
. The core components module contains a wide range of different interactive components including dropdowns, sliders and text boxes.
Dash应用程序最有用的方面之一是,您可以使用callbacks
使仪表板具有交互性。 核心组件模块包含各种不同的交互式组件,包括下拉菜单,滑块和文本框。
The below code adds a dropdown to the dashboard which allows you to filter on the RAD feature to view the distribution for each unique value. The resulting dashboard is shown below the code.
以下代码在仪表板上添加了一个下拉列表,使您可以筛选RAD功能,以查看每个唯一值的分布。 结果仪表板显示在代码下方。
I have recently found myself moving to use Plotly as my go-to visualisation library as I find the quality of analysis you can achieve with such relative ease can’t be matched by any other Python plotting library at the moment. This has become even more the case since Pandas added Plotly as an available backend for their plotting functionality.
我最近发现自己开始将Plotly用作我的可视化库,因为我发现可以相对轻松地实现的分析质量目前无法与其他任何Python绘图库相提并论。 自从Pandas将Plotly添加为绘图功能的可用后端以来,情况就更是如此。
I previously wrote this article outlining how to use Plotly as the backend for Pandas visualisations. This is particularly useful if you want to put together some very quick analysis piece.
我之前写过这篇文章,概述了如何将Plotly用作Pandas可视化的后端。 如果您想将一些非常快速的分析片段放在一起,这特别有用。
Thanks for reading!
谢谢阅读!
I send out a monthly newsletter if you would like to join please sign up via this link. Looking forward to being part of your learning journey!
如果您想加入,我会每月发送一次通讯,请通过此链接注册。 期待成为您学习之旅的一部分!
翻译自: https://towardsdatascience.com/create-beautiful-interactive-visualisations-in-python-f8517dc7ae5c
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388158.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!