使用 Plotly 在 Python 中填充區域圖
Plotly 是 Python 中的一個庫,在繪製各種圖形和圖表方面非常有用。它是一個互動式視覺化庫,用於建立高質量的出版物圖形。可以使用 Plotly 視覺化的一些圖形和圖表包括折線圖、散點圖、區域圖、條形圖、誤差條、箱線圖、直方圖、熱圖、子圖、多軸、極座標圖和氣泡圖。
填充區域圖
填充區域圖是 Plotly 中一個增強的模組,它分析各種資料並生成易於閱讀的使用者友好型圖形。填充區域與特定值相協調,該值對應於由 line_group 引數給出的特定列。
資料框
對於繪圖,您需要一個具有屬性(列名)的資料框,該資料框通常使用 pandas 物件和 numpy 建立,或者模組本身提供某些內建資料集,如下所述:
carshare
election
Gapminder
iris
tips
Wind
它們每個都有 ai
引數
x,y - 指的是資料框中列的名稱。x 和 y 的值沿著它們在笛卡爾座標系中的位置標記。
顏色 - 指的是資料框中列的名稱。此列的值用於為標記的位置分配顏色。
示例 1
讓我們透過繪製強度與頻率的關係圖(方向作為顏色)來視覺化 wind 資料集。以下程式碼列印資料集及其屬性、行數和列數。
演算法
步驟 1 - 匯入 plotly.express 為 px
步驟 2 - 從 wind 資料集中建立資料框物件。
步驟 3 - 繪製資料框的區域圖,引數 x 為強度,y 為頻率,顏色為方向。
步驟 4 - 顯示區域圖。
import plotly.express as px df = px.data.wind() fig = px.area(df, x="strength", y="frequency", color="direction",) fig.show()
輸出

示例 2
以下程式碼透過視覺化 gapminder 資料集並繪製 lifeExp 與 year 的關係圖(以 continent 為顏色)來列印資料集及其屬性、行數和列數。
演算法
步驟 1 - 匯入 plotly.express 為 px
步驟 2 - 從 gapminder 資料集中建立資料框物件。
步驟 3 - 繪製資料框的區域圖,引數 x 為 year,y 為 lifeExp,顏色為 continent。
步驟 4 - 顯示區域圖。
import plotly.express as px df = px.data.gapminder() fig = px.area(df, x="year", y="lifeExp", color="continent") fig.show()
輸出

示例 3
以下程式碼列印使用 random.randint 函式生成隨機數建立的資料集,以及其屬性、行數和列數。
演算法
步驟 1 - 匯入 plotly.express 為 px
步驟 2 - 使用使用 randint 函式生成的資料集建立資料框物件。
步驟 3 - 繪製資料框的區域圖,引數 x 為 year,y 為 lifeExp,顏色為 continent。
步驟 4 - 顯示區域圖。
import plotly.express as px import numpy as np # creating random data through randomint # function of numpy.random np.random.seed(50) random_x= np.random.randint(1,100,50) random_y= np.random.randint(1,100,50) print(random_x) print(random_y) fig = px.area(x = random_x, y = random_y) fig.show() print(random_x) print(random_y)
輸出
[49 97 12 34 95 5 71 71 23 6 3 96 72 69 79 36 93 92 27 91 7 21 44 32 50 86 42 65 7 20 3 80 31 36 27 61 67 77 97 68 3 11 52 1 94 94 95 1 12 31] [27 72 52 29 25 92 47 63 14 72 37 1 59 44 65 20 59 46 84 2 62 10 30 53 44 88 33 20 48 70 93 7 66 37 85 37 45 54 79 5 87 44 20 97 38 31 63 45 44 36]
結論
Plotly 庫視覺化的區域主要有兩種變化,即堆疊區域和非堆疊區域。基本區域被放大以顯示大量值。但是,堆疊區域圖被認為是條形圖或圖表中資料表示的最佳方式之一。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP