Microsoft Fabric 中的筆記本視覺效果

Microsoft Fabric 是整合式分析服務,可加速跨數據倉儲和巨量數據分析系統深入解析的時間。 筆記本中的數據視覺效果是一個重要元件,可讓您深入瞭解您的數據。 它有助於讓人類更容易瞭解巨量和小型數據。 它也可讓您更輕鬆地在數據群組中偵測模式、趨勢和極端值。

當您在 Fabric 中使用 Apache Spark 時,有各種內建選項可協助您將數據可視化,包括網狀架構筆記本圖表選項,以及存取熱門的開放原始碼連結庫。

使用 Fabric 筆記本時,您可以使用圖表選項,將表格式結果檢視轉換成自定義圖表。 在這裡,您可以將數據可視化,而不需要撰寫任何程序代碼。

內建視覺效果命令 - display() 函式

網狀架構內建視覺效果函式可讓您將 Apache Spark DataFrame、Pandas DataFrame 和 SQL 查詢結果轉換成豐富的格式數據視覺效果。

您可以在 Spark 資料框架上建立的數據框架上使用顯示 函式,或在 Spark 數據框架上建立的 Scala 或彈性分散式數據集 (RDD) 函式來產生豐富的數據框架數據表檢視和圖表檢視。

SQL 語句的輸出預設會出現在轉譯的數據表檢視中。

豐富的數據框架數據表檢視

豐富數據框架預覽的動畫 GIF。

  1. 使用 display() 命令時,預設會轉譯數據表檢視。
  2. 您可以按下 [ 檢查 ] 按鈕來分析資料框架。 它提供摘要數據分佈,並顯示每個數據行的統計數據。
  3. [檢查] 側邊窗格中的每個卡片都會對應至 dataframke 的數據行,您可以按兩下卡片或選取資料表中的數據行來檢視更多詳細數據。
  4. 您可以按下資料表的儲存格來檢視儲存格詳細數據。 當數據框架包含長字串類型的內容時,這非常有用。
  5. 您可以指定數據表檢視的數據列計數、預設值為 1000、Notebook支援,最多可以檢視及分析 數據框架的10000 個數據列。

豐富的數據框架圖表檢視

圖表檢視的動畫GIF。

  1. 一旦您擁有轉譯的數據表檢視,請切換至 [ 圖表 ] 檢視。

  2. 網狀架構筆記本會根據目標數據框架自動建議「索引鍵」「值」配對,讓預設圖表具有數據見解意義。

  3. 您現在可以藉由指定下列值來自定義視覺效果:

    Configuration 說明
    圖表類型 顯示函式支援各種不同的圖表類型,包括條形圖、散佈圖、折線圖等等。
    機碼 指定 X 軸的值範圍。
    指定 y 軸值的值範圍。
    數列群組 使用此組態來判斷匯總的群組。
    彙總 使用此方法來匯總視覺效果中的數據。

    設定將會自動儲存在 Notebook 輸出內容中。

    注意

    根據預設, display(df) 函式只會採用前 1000 個數據列來轉譯圖表。 選取 [匯總至所有結果 ],然後選取 [ 套用 ] 以套用整個語意模型的圖表產生。 當圖表設定變更時,將會觸發 Spark 作業。 請注意,完成計算並轉譯圖表可能需要幾分鐘的時間。

  4. 當作業完成時,您可以檢視最終視覺效果並與其互動。

display(df) 摘要檢視

使用 display(df, summary = true) 來檢查指定 Apache Spark DataFrame 的統計數據摘要。 摘要包含數據行名稱、數據行類型、唯一值,以及每個數據行的遺漏值。 您也可以選取特定數據行,以查看其最小值、最大值、平均值和標準偏差。

摘要檢視的動畫GIF。

displayHTML() 選項

網狀架構筆記本支援使用 displayHTML 函式的 HTML 圖形。

下圖是使用 D3.js 建立視覺效果的範例。

使用 D3.js 建立之圖表範例的螢幕快照。

若要建立此視覺效果,請執行下列程序代碼。

displayHTML("""<!DOCTYPE html>
<meta charset="utf-8">

<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>

<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>
<script>

// set the dimensions and margins of the graph
var margin = {top: 10, right: 30, bottom: 30, left: 40},
  width = 400 - margin.left - margin.right,
  height = 400 - margin.top - margin.bottom;

// append the svg object to the body of the page
var svg = d3.select("#my_dataviz")
.append("svg")
  .attr("width", width + margin.left + margin.right)
  .attr("height", height + margin.top + margin.bottom)
.append("g")
  .attr("transform",
        "translate(" + margin.left + "," + margin.top + ")");

// Create Data
var data = [12,19,11,13,12,22,13,4,15,16,18,19,20,12,11,9]

// Compute summary statistics used for the box:
var data_sorted = data.sort(d3.ascending)
var q1 = d3.quantile(data_sorted, .25)
var median = d3.quantile(data_sorted, .5)
var q3 = d3.quantile(data_sorted, .75)
var interQuantileRange = q3 - q1
var min = q1 - 1.5 * interQuantileRange
var max = q1 + 1.5 * interQuantileRange

// Show the Y scale
var y = d3.scaleLinear()
  .domain([0,24])
  .range([height, 0]);
svg.call(d3.axisLeft(y))

// a few features for the box
var center = 200
var width = 100

// Show the main vertical line
svg
.append("line")
  .attr("x1", center)
  .attr("x2", center)
  .attr("y1", y(min) )
  .attr("y2", y(max) )
  .attr("stroke", "black")

// Show the box
svg
.append("rect")
  .attr("x", center - width/2)
  .attr("y", y(q3) )
  .attr("height", (y(q1)-y(q3)) )
  .attr("width", width )
  .attr("stroke", "black")
  .style("fill", "#69b3a2")

// show median, min and max horizontal lines
svg
.selectAll("toto")
.data([min, median, max])
.enter()
.append("line")
  .attr("x1", center-width/2)
  .attr("x2", center+width/2)
  .attr("y1", function(d){ return(y(d))} )
  .attr("y2", function(d){ return(y(d))} )
  .attr("stroke", "black")
</script>

"""
)

在筆記本中內嵌 Power BI 報表

重要

這項功能目前為「預覽」狀態。 這項資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不提供任何明確或隱含的瑕疵擔保。

Fabric 筆記本現在原生支援 Powerbiclient Python 套件。 您不需要在 Fabric Notebook Spark 執行時間 3.4 上執行任何額外的設定(例如驗證程式)。 只要匯入 powerbiclient ,然後繼續探索。 若要深入瞭解如何使用 powerbiclient 套件,請參閱 powerbiclient 檔

Powerbiclient 支援下列主要功能。

轉譯現有的Power BI報表

只要幾行程序代碼,您就可以輕鬆地在筆記本中內嵌Power BI報表並與其互動。

下圖是轉譯現有Power BI報表的範例。

Spark 快速視覺效果的螢幕快照。

執行下列程式代碼來轉譯現有的Power BI報表。

from powerbiclient import Report

report_id="Your report id"
report = Report(group_id=None, report_id=report_id)

report

從 Spark DataFrame 建立報表視覺效果

您可以在筆記本中使用 Spark DataFrame 來快速產生深入解析的視覺效果。 您也可以選取內嵌報表中的 [ 儲存 ],以在目標工作區中建立報表專案。

下圖是Spark DataFrame中的範例 QuickVisualize()

螢幕擷取畫面:Power BI 報表。

執行下列程式代碼,從Spark DataFrame轉譯報表。

# Create a spark dataframe from a Lakehouse parquet table
sdf = spark.sql("SELECT * FROM testlakehouse.table LIMIT 1000")

# Create a Power BI report object from spark data frame
from powerbiclient import QuickVisualize, get_dataset_config
PBI_visualize = QuickVisualize(get_dataset_config(sdf))

# Render new report
PBI_visualize

從 pandas DataFrame 建立報表視覺效果

您也可以根據 Notebook 中的 pandas DataFrame 建立報表。

下圖是 pandas DataFrame 中的 QuickVisualize() 範例。

pandas 快速視覺效果的螢幕快照。

執行下列程式代碼,從Spark DataFrame轉譯報表。

import pandas as pd

# Create a pandas dataframe from a URL
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv")

# Create a pandas dataframe from a Lakehouse csv file
from powerbiclient import QuickVisualize, get_dataset_config

# Create a Power BI report object from your data
PBI_visualize = QuickVisualize(get_dataset_config(df))

# Render new report
PBI_visualize

在數據視覺效果方面,Python 提供多個圖形連結庫,其中包含許多不同的功能。 根據預設,Fabric 中的每個 Apache Spark 集區都包含一組策劃且熱門的開放原始碼連結庫。

Matplotlib

您可以使用每個連結庫的內建轉譯函式,來轉譯標準繪製連結庫,例如 Matplotlib。

下圖是使用 Matplotlib 建立條形圖的範例。

使用 Matplotlib 建立之折線圖的螢幕快照。

使用 Matplotlib 建立的條形圖螢幕快照。

執行下列範例程式代碼來繪製此條形圖。

# Bar chart

import matplotlib.pyplot as plt

x1 = [1, 3, 4, 5, 6, 7, 9]
y1 = [4, 7, 2, 4, 7, 8, 3]

x2 = [2, 4, 6, 8, 10]
y2 = [5, 6, 2, 6, 2]

plt.bar(x1, y1, label="Blue Bar", color='b')
plt.bar(x2, y2, label="Green Bar", color='g')
plt.plot()

plt.xlabel("bar number")
plt.ylabel("bar height")
plt.title("Bar Chart Example")
plt.legend()
plt.show()

Bokeh

您可以使用 displayHTML(df) 來轉譯 HTML 或互動式連結庫,例如 bokeh

下圖是使用 bokeh繪製地圖圖像的範例。

繪製地圖圖像範例的螢幕快照。

若要繪製此映像,請執行下列範例程序代碼。

from bokeh.plotting import figure, output_file
from bokeh.tile_providers import get_provider, Vendors
from bokeh.embed import file_html
from bokeh.resources import CDN
from bokeh.models import ColumnDataSource

tile_provider = get_provider(Vendors.CARTODBPOSITRON)

# range bounds supplied in web mercator coordinates
p = figure(x_range=(-9000000,-8000000), y_range=(4000000,5000000),
           x_axis_type="mercator", y_axis_type="mercator")
p.add_tile(tile_provider)

# plot datapoints on the map
source = ColumnDataSource(
    data=dict(x=[ -8800000, -8500000 , -8800000],
              y=[4200000, 4500000, 4900000])
)

p.circle(x="x", y="y", size=15, fill_color="blue", fill_alpha=0.8, source=source)

# create an html document that embeds the Bokeh plot
html = file_html(p, CDN, "my plot1")

# display this html
displayHTML(html)

Plotly

您可以使用 displayHTML()來轉譯 HTML 或互動式連結庫,例如 Plotly

若要繪製此映像,請執行下列範例程序代碼。

使用繪圖建立之 美國 地圖的螢幕快照。

from urllib.request import urlopen
import json
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
    counties = json.load(response)

import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv",
                   dtype={"fips": str})

import plotly
import plotly.express as px

fig = px.choropleth(df, geojson=counties, locations='fips', color='unemp',
                           color_continuous_scale="Viridis",
                           range_color=(0, 12),
                           scope="usa",
                           labels={'unemp':'unemployment rate'}
                          )
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})

# create an html document that embeds the Plotly plot
h = plotly.offline.plot(fig, output_type='div')

# display this html
displayHTML(h)

Pandas

您可以將 pandas DataFrame 的 HTML 輸出視為預設輸出。 網狀架構筆記本會自動顯示樣式的 HTML 內容。

使用 pandas 建立之數據表的螢幕快照。

import pandas as pd 
import numpy as np 

df = pd.DataFrame([[38.0, 2.0, 18.0, 22.0, 21, np.nan],[19, 439, 6, 452, 226,232]], 

                  index=pd.Index(['Tumour (Positive)', 'Non-Tumour (Negative)'], name='Actual Label:'), 

                  columns=pd.MultiIndex.from_product([['Decision Tree', 'Regression', 'Random'],['Tumour', 'Non-Tumour']], names=['Model:', 'Predicted:'])) 

df