熱線電話:13121318867

登錄
首頁精彩閱讀介紹一個Python模塊,Seaborn繪制的圖表也能實現動態交互
介紹一個Python模塊,Seaborn繪制的圖表也能實現動態交互
2022-06-01
收藏

作者:俊欣

來源:關于數據分析與可視化

相信大家一定會seaborn或者matplotlib這幾個模塊感到并不陌生,通常大家會用這幾個模塊來進行可視化圖表的制作,為了讓我們繪制的圖表更具交互性,今天小編來給大家介紹個組件。

ipywidgets

首先我們通過pip命令來下載該模塊

pip install ipywidgets

該模塊中的interact函數可以和我們自定義的函數相結合,隨著我們輸入的不斷變化,輸出也會產生相應的不同結果,我們來看一個簡單的案例

from ipywidgets import interact def f(x): print(f"The square value is: {x**2}")
    
interact(f, x=10)

output

介紹一個Python模塊,Seaborn繪制的圖表也能實現動態交互

當我們拖動當中的圓點的時候,輸出的結果也隨之變化。當然我們也可以將其當做是裝飾器來使用,代碼如下

@interact(x=10) def f(x): print(f"The square value is: {x**2}")

output

介紹一個Python模塊,Seaborn繪制的圖表也能實現動態交互

上面的自定義函數中,當然我們可以自行設定橫軸當中的最大值與最小值,以及每拖動一次x值的變化(和Python當中的range函數類似),

interact(f, x=widgets.IntSlider(min=-10, max=30, step=1, value=10))

output

介紹一個Python模塊,Seaborn繪制的圖表也能實現動態交互

而當輸入框中的參數不止一個參數的時候,可以有不止一個的滑動條,代碼如下

import ipywidgets as widgets
one = widgets.IntSlider(min = 0, max = 10)
two = widgets.IntSlider(min = 0, max = 100)
three = widgets.IntSlider(min = 0, max = 1000)

ui = widgets.HBox([one, two, three])
def func(x, y, z): print(f"The first value is: {x + 2}") print(f"The second value is: {y * 2}") print(f"The third value is: {z ** 2}")
    
out = widgets.interactive_output(func, {"x": one, "y": two, "z": three})
display(ui, out)

output

介紹一個Python模塊,Seaborn繪制的圖表也能實現動態交互

當參數類型是字符串時,則是需要通過輸入框的形式來進行交互,代碼如下

def f_2(x): print(f"The value is: {x}")

interact(f_2, x="Hello World")

output

介紹一個Python模塊,Seaborn繪制的圖表也能實現動態交互

而當我們輸入的X參數是一個列表里面有著若干個字符串的時候,則會在輸入框中出現個下拉框,如下所示

interact(f_2, x=["Hello World", "你好"])

output

介紹一個Python模塊,Seaborn繪制的圖表也能實現動態交互

seaborn之間的結合

然后我們來看看該模塊和seaborn之間的結合,我們先用Pandas模塊來讀取數據集,代碼如下

import pandas as pd
df = pd.read_csv("data.csv")
df.head()

output

介紹一個Python模塊,Seaborn繪制的圖表也能實現動態交互

我們簡單地來畫一張直方圖,代碼如下

import seaborn as sns import matplotlib.pyplot as plt
%matplotlib inline g = sns.countplot(data = df, x="Gender", hue="Attrition")

output

介紹一個Python模塊,Seaborn繪制的圖表也能實現動態交互

我們可以將繪制圖表的這一行代碼封裝成一個函數,將代碼中的“x”甚至是“hue”作為是輸入的參數,代碼如下

## 篩選出離散型變量的特征 categorical_columns = [column for column in df.columns if df[column].dtype == "object"] ## 做成下拉框的形式來進行交互 dd = widgets.Dropdown(options=categorical_columns, value=categorical_columns[0], description="Select a column") @interact(column=dd) def draw_countplot(column): g = sns.countplot(data = df, x=column, hue="Attrition")

output

介紹一個Python模塊,Seaborn繪制的圖表也能實現動態交互

我們可以在下拉框中選擇不同的離散型變量的特征從而繪制出不同的圖表,當然一個下拉框可能有人會覺得有點少,我們可以再來擴展一下

## 兩個下拉框 dd1 = widgets.Dropdown(options=categorical_columns, value=categorical_columns[0], description="Column")
dd2 = widgets.Dropdown(options=categorical_columns, value=categorical_columns[0], description="Hue")

ui = widgets.HBox([dd1, dd2]) ## 繪制圖表的函數 def draw_countplot(column, hue):
    g = sns.countplot(data = df, x=column, hue=hue) ## X軸方向的標記會旋轉60度 if len(df[column].unique()) > 3:
        g.tick_params(axis="x", rotation=60) out = widgets.interactive_output(draw_countplot, {'column':dd1, "hue": dd2}) ## 最終將圖表呈現出來 display(ui, out)

output

介紹一個Python模塊,Seaborn繪制的圖表也能實現動態交互

當然有可能會覺得都是輸入框的話會有點無聊,那我們在輸入框的同時加入一個滑動條,對應的是輸入的參數是整型或者是浮點數時

## 兩個輸入框還有一個滑動條 dd1 = widgets.Dropdown(options=numeric_columns, description="Column1")
dd2 = widgets.Dropdown(options=numeric_columns, description="Column2")
slider = widgets.IntSlider(min=df['Age'].min(), max=df["Age"].max(), description="Max Age")

ui = widgets.HBox([dd1, dd2, slider]) ## 繪制圖表的函數 def draw_relplot(column1, column2, age):
    p = sns.relplot(data=df[df['Age']<=age], x=column1, y=column2) out = widgets.interactive_output(draw_countplot, {"column1": dd1, "column2": dd2, "age": slider}) ## 將最終的圖表給呈現出來 display(ui, out)

output

介紹一個Python模塊,Seaborn繪制的圖表也能實現動態交互

數據分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數據分析師資訊
更多

OK
客服在線
立即咨詢
日韩人妻系列无码专区视频,先锋高清无码,无码免费视欧非,国精产品一区一区三区无码
客服在線
立即咨詢