用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.pswp.cn/news/388158.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388158.shtml
英文地址,請注明出處:http://en.pswp.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…