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

相关文章

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

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.我的目标是更好地了…

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…

关于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…

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

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

在Python中查找子字符串索引的5种方法

在Python中查找字符串中子字符串索引的5种方法 (5 Ways to Find the Index of a Substring in Strings in Python) str.find() str.find() str.rfind() str.rfind() str.index() str.index() str.rindex() str.rindex() re.search() re.search() str.find() (str.find()) …

Eclipse 插件开发 向导

阅读目录 最近由于特殊需要,开始学习插件开发。   下面就直接弄一个简单的插件吧!   1 新建一个插件工程   2 创建自己的插件名字,这个名字最好特殊一点,一遍融合到eclipse的时候,不会发生冲突。   3 下一步,进…

线性回归 假设_线性回归的假设

线性回归 假设Linear Regression is the bicycle of regression models. It’s simple yet incredibly useful. It can be used in a variety of domains. It has a nice closed formed solution, which makes model training a super-fast non-iterative process.线性回归是回…

solo

solo - 必应词典 美[soʊloʊ]英[səʊləʊ]n.【乐】独奏(曲);独唱(曲);单人舞;单独表演adj.独唱[奏]的;单独的;单人的v.独奏;放单飞adv.独网络梭罗;独奏曲;索罗变形复数&#xff1…

Eclipse 简介和插件开发天气预报

Eclipse 简介和插件开发 Eclipse 是一个很让人着迷的开发环境,它提供的核心框架和可扩展的插件机制给广大的程序员提供了无限的想象和创造空间。目前网上流传相当丰富且全面的开发工具方面的插件,但是 Eclipse 已经超越了开发环境的概念,可以…

趣味数据故事_坏数据的好故事

趣味数据故事Meet Julia. She’s a data engineer. Julia is responsible for ensuring that your data warehouses and lakes don’t turn into data swamps, and that, generally speaking, your data pipelines are in good working order.中号 EETJulia。 她是一名数据工程…

Linux 4.1内核热补丁成功实践

最开始公司运维同学反馈,个别宿主机上存在进程CPU峰值使用率异常的现象。而数万台机器中只出现了几例,也就是说万分之几的概率。监控产生的些小误差,不会造成宕机等严重后果,很容易就此被忽略了。但我们考虑到这个异常转瞬即逝、并…

python分句_Python循环中的分句,继续和其他子句

python分句Python中的循环 (Loops in Python) for loop for循环 while loop while循环 Let’s learn how to use control statements like break, continue, and else clauses in the for loop and the while loop.让我们学习如何在for循环和while循环中使用诸如break &#xf…