用Python创建漂亮的交互式可视化效果

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.

数据的前几行如下所示。

Image for post

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.

让我们使用图形对象模块来探索房价和房间数量之间的关系。 您会注意到,我正在使用上面提到的帮助器功能为可视化添加标题。

Image for post

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选项来控制悬停时的交互。

Image for post

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将在本地主机上托管应用程序,运行代码时,地址将显示在输出中。

Image for post

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_componentsdash_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应用。 结果显示板如下所示。

Image for post

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功能,以查看每个唯一值的分布。 结果仪表板显示在代码下方。

Image for post

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,一经查实,立即删除!

相关文章

CCF 201809-1 买菜

问题描述| 试题编号: | 201809-2 | | 试题名称: | 买菜 | | 时间限制: | 1.0s | | 内存限制: | 256.0MB | 问题描述 小H和小W来到了一条街上,两人分开买菜,他们买菜的过程可以描述为,去店里买一…

笔试题③

1.线程间通信 handler机制 2.AsyncTask 异步任务 3.HandlerThread 子线程中创建了一个 Looper对象 可以在子线程里使用消息机制 IntentService 带了HandlerThread 并且创建了一个子线程的handler 在服务中 创建子线程执行耗时操作 耗时操作执行结束之后服务退出 如果想在Serv…

Hadoop 2.0集群配置详细教程

Hadoop 2.0集群配置详细教程 前言 Hadoop2.0介绍 Hadoop是 apache 的开源 项目,开发的主要目的是为了构建可靠,可拓展 scalable ,分布式的系 统, hadoop 是一系列的子工程的 总和,其中包含 1. hadoop common &#xff…

php如何减缓gc_管理信息传播-使用数据科学减缓错误信息的传播

php如何减缓gcWith more people now than ever relying on social media to stay updated on current events, there is an ethical responsibility for hosting companies to defend against false information. Disinformation, which is a type of misinformation that is i…

[UE4]删除UI:Remove from Parent

同时要将保存UI的变量清空,以释放占用的系统内存 转载于:https://www.cnblogs.com/timy/p/9842206.html

MySQL基础部分总结

MySQL 1、选择数据库 use dbnameshow databases;2、数据表 show tablesmysql> show columns from customers;mysql> desc customers;3、show 语句 show statusshow create databasesshow create tableshow grants4、select 检索 4.1.1版本后不再区分大小写,但…

BZOJ2503: 相框

Description P大的基础电路实验课是一个无聊至极的课。每次实验,T君总是提前完成,管理员却不让T君离开,T君只能干坐在那儿无所事事。先说说这个实验课,无非就是把几根导线和某些元器件(电阻、电容、电感等)…

泰坦尼克号 数据分析_第1部分:泰坦尼克号-数据分析基础

泰坦尼克号 数据分析My goal was to get a better understanding of how to work with tabular data so I challenged myself and started with the Titanic -project. I think this was an excellent way to learn the basics of data analysis with python.我的目标是更好地了…

Imperva开源域目录控制器,简化活动目录集成

Imperva已公开发布域目录控制器(Domain Directory Controller,DDC)的源代码,这是一个Java库,用于简化常见的Active Directory集成。 与Java的LdapContext不同,这个库构建在Apache Directory LDAP之上&#…

2018.10.24 NOIP模拟 小 C 的序列(链表+数论)

传送门 考虑到a[l],gcd(a[l],a[l1]),gcd(a[l],a[l1],a[l2])....gcd(a[l]...a[r])a[l],gcd(a[l],a[l1]),gcd(a[l],a[l1],a[l2])....gcd(a[l]...a[r])a[l],gcd(a[l],a[l1]),gcd(a[l],a[l1],a[l2])....gcd(a[l]...a[r])是可以分成最多logloglog段且段内的数都是相同的。 那么我们用…

vba数组dim_NDArray — —一个基于Java的N-Dim数组工具包

vba数组dim介绍 (Introduction) Within many development languages, there is a popular paradigm of using N-Dimensional arrays. They allow you to write numerical code that would otherwise require many levels of nested loops in only a few simple operations. Bec…

Nodejs教程08:同时处理GET/POST请求

示例代码请访问我的GitHub: github.com/chencl1986/… 同时处理GET/POST请求 通常在开发过程中,同一台服务器需要接收多种类型的请求,并区分不同接口,向客户端返回数据。 最常用的方式,就是对请求的方法、url进行区分判…

关于position的四个标签

四个标签是static,relative,absolute,fixed。 static 该值是正常流,并且是默认值,因此你很少看到(如果存在的话)指定该值。 relative:框的位置能够相对于它在正常流中的位置有所偏移…

python算法和数据结构_Python中的数据结构和算法

python算法和数据结构To至 Leonardo da Vinci达芬奇(Leonardo da Vinci) 介绍 (Introduction) The purpose of this article is to give you a panorama of data structures and algorithms in Python. This topic is very important for a Data Scientist in order to help …

CSS:元素塌陷问题

2019独角兽企业重金招聘Python工程师标准>>> 描述: 在文档流中,父元素的高度默认是被子元素撑开的,也就是子元素多高,父元素就多高。但是当子元素设置浮动之后,子元素会完全脱离文档流,此时将会…

Celery介绍及常见错误

celery 情景:用户发起request,并等待response返回。在本些views中,可能需要执行一段耗时的程序,那么用户就会等待很长时间,造成不好的用户体验,比如发送邮件、手机验证码等。 使用celery后,情况…

python dash_Dash是Databricks Spark后端的理想基于Python的前端

python dash📌 Learn how to deliver AI for Big Data using Dash & Databricks this recorded webinar with Peter Kim of Plotly and Prasad Kona of Databricks.this通过Plotly的Peter Kim和Databricks的Prasad Kona的网络研讨会了解如何使用Dash&#xff06…

js里的数据类型转换

1、类型转换 转换为字符串 - String(x)- x.toString(x, 10)- x 转换为数字 - Number(x)- parseInt(x, 10) - parseFloat(x) - x - 0- x 转换为boolean - Boolean(x)- !!x 2、falsy值(false) - 0- NaN- - null- undefined 3、内存图 - object存储的是地址…

Eclipse 插件开发遇到问题心得总结

Eclipse 插件开发遇到问题心得总结 Posted on 2011-07-17 00:51 季枫 阅读(3997) 评论(0) 编辑 收藏1、Eclipse 中插件开发多语言的实现 为了使用 .properties 文件,需要在 META-INF/MANIFEST.MF 文件中定义: Bundle-Localization: plugin 这样就会…

/src/applicationContext.xml

<?xml version"1.0" encoding"UTF-8"?> <beans xmlns"http://www.springframework.org/schema/beans" xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance" xmlns:context"http://www.springframework.org/schema…