Share via


顯現資料

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

在 Azure Synapse Analytics 中使用 Apache Spark 時,有各種內建選項可協助您將數據可視化,包括 Synapse 筆記本圖表選項、存取熱門的開放原始碼連結庫,以及與 Synapse SQL 和 Power BI 整合。

筆記本圖表選項

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

display(df) 函式

display 式可讓您將 SQL 查詢和 Apache Spark 數據框架和 RDD 轉換成豐富的資料視覺效果。 函 display 式可用於 PySpark、Scala、Java、R 和 .NET 中建立的數據框架或 RDD。

若要存取圖表選項:

  1. 魔術命令的 %%sql 輸出預設會出現在轉譯的數據表檢視中。 您也可以呼叫 display(df) Spark DataFrames 或復原分散式數據集 (RDD) 函式,以產生轉譯的數據表檢視。

  2. 一旦您擁有轉譯的數據表檢視,請切換至 [圖表檢視]。 built-in-charts

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

    組態 描述
    圖表類型 display 式支持各種不同的圖表類型,包括條形圖、散佈圖、折線圖等等
    索引鍵 指定 X 軸的值範圍
    指定 y 軸值的值範圍
    序列群組 用來判斷匯總的群組
    彙總 在視覺效果中匯總數據的方法

    注意

    根據預設,函 display(df) 式只會採用前 1000 個數據列來轉譯圖表。 檢查 [彙總所有結果],然後按兩下 [用] 按鈕,您將從整個數據集套用圖表產生。 當圖表設定變更時,將會觸發 Spark 作業。 請注意,完成計算並轉譯圖表可能需要幾分鐘的時間。

  4. 完成後,您可以檢視最終視覺效果並與其互動!

display(df) 統計數據詳細數據

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

displayHTML() 選項

Azure Synapse Analytics 筆記本支援使用 displayHTML 函式的 HTML 圖形。

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

d3-js-example

執行下列程式代碼以建立上述視覺效果。

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>

"""
)

Python 連結庫

在數據視覺效果方面,Python 提供多個圖形連結庫,其中包含許多不同的功能。 根據預設,Azure Synapse Analytics 中的每個 Apache Spark 集區都包含一組策劃且熱門的開放原始碼連結庫。 您也可以使用 Azure Synapse Analytics 連結庫管理功能來新增或管理其他連結庫和版本。

Matplotlib

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

下圖是使用 Matplotlib 建立條形圖的範例。 Line graph example.

執行下列範例程式代碼來繪製上述影像。

# 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

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

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

bokeh-example

執行下列範例程式代碼來繪製上述影像。

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

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

plotly-example

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 資料框架的 HTML 輸出視為預設輸出,筆記本會自動顯示樣式的 HTML 內容。

Panda graph example.

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 

其他連結庫

除了這些連結庫之外,Azure Synapse Analytics 運行時間也包含下列一組經常用於數據視覺效果的連結庫:

您可以流覽 Azure Synapse Analytics 執行時間 ,以取得可用連結庫和版本的最新資訊。

R 連結庫 (預覽)

R 生態系統提供多個圖形連結庫,其中包含許多不同的功能。 根據預設,Azure Synapse Analytics 中的每個 Apache Spark 集區都包含一組策劃且熱門的開放原始碼連結庫。 您也可以使用 Azure Synapse Analytics 連結庫管理功能來新增或管理其他連結庫和版本。

ggplot2

ggplot2 連結庫很適合用於數據視覺效果和探勘數據分析。

Screenshot of a ggplot2 graph example.

library(ggplot2)
data(mpg, package="ggplot2") 
theme_set(theme_bw()) 

g <- ggplot(mpg, aes(cty, hwy))

# Scatterplot
g + geom_point() + 
  geom_smooth(method="lm", se=F) +
  labs(subtitle="mpg: city vs highway mileage", 
       y="hwy", 
       x="cty", 
       title="Scatterplot with overlapping points", 
       caption="Source: midwest")

rBokeh

rBokeh 是原生 R 繪圖連結庫,可用來建立由 Bokeh 視覺效果連結庫支援的互動式圖形。

若要安裝 rBokeh,您可以使用下列命令:

install.packages("rbokeh")

安裝之後,您可以利用 rBokeh 來建立互動式視覺效果。

Screenshot of a rBokeh graph example.

library(rbokeh)
p <- figure() %>%
  ly_points(Sepal.Length, Sepal.Width, data = iris,
    color = Species, glyph = Species,
    hover = list(Sepal.Length, Sepal.Width))

R Plotly

Plotly 的 R 圖形連結庫會製作互動式出版物質量圖表。

若要安裝 Plotly,您可以使用下列命令:

install.packages("plotly")

安裝之後,您可以利用 Plotly 建立互動式視覺效果。

Screenshot of a Plotly graph example.

library(plotly) 

fig <- plot_ly() %>% 
  add_lines(x = c("a","b","c"), y = c(1,3,2))%>% 
  layout(title="sample figure", xaxis = list(title = 'x'), yaxis = list(title = 'y'), plot_bgcolor = "#c7daec") 

fig

Highcharter

Highcharter 是 Highcharts JavaScript 連結庫及其模組的 R 包裝函式。

若要安裝 Highcharter,您可以使用下列命令:

install.packages("highcharter")

安裝之後,您可以利用 Highcharter 來建立互動式視覺效果。

Screenshot of a Highcharter graph example.

library(magrittr)
library(highcharter)
hchart(mtcars, "scatter", hcaes(wt, mpg, z = drat, color = hp)) %>%
  hc_title(text = "Scatter chart with size and color")

使用 Apache Spark 和 SQL 隨選 連線 至 Power BI

Azure Synapse Analytics 與 Power BI 深入整合,讓數據工程師能夠建置分析解決方案。

Azure Synapse Analytics 可讓不同的工作區計算引擎在其 Spark 集區和無伺服器 SQL 集區之間共用資料庫和數據表。 使用共享元數據模型,您可以使用 SQL 隨選查詢 Apache Spark 數據表。 完成後,您可以將 SQL 隨選端點連線至 Power BI,以輕鬆地查詢同步處理的 Spark 數據表。

下一步