簡體   English   中英

Altair 中的空散點圖

[英]Empty scatter plot in Altair

我很困惑為什么這段代碼不起作用:

#@title Visualize trends in accelerators

x_axis = 'Release Year'
y_axis = 'FP32 (single precision) Performance [FLOP/s]'

# Libraries
import pandas as pd
import numpy as np
import altair as alt

# Download data
key = '1AAIebjNsnJj_uKALHbXNfn3_YsT6sHXtCU0q7OIPuc4'
sheet_name = 'HARDWARE_DATA'
url = f'https://docs.google.com/spreadsheets/d/{key}/gviz/tq?tqx=out:csv&sheet={sheet_name}'
df = pd.read_csv(url)

# Filter NaN datapoints
df = df[~df[x_axis].isna()]
df = df[~df[y_axis].isna()]

# Plot the dataset
alt.themes.enable('fivethirtyeight')
alt.Chart(df).mark_circle(size=60).encode(
  x=alt.X(f'{x_axis}:Q',
          scale=alt.Scale(
                          domain=(df[x_axis].min(), df[x_axis].max())
                          ),
          axis=alt.Axis(format=".0f")
          ),
  y=alt.Y(f'{y_axis}:Q',
          scale=alt.Scale(
                          domain=(df[y_axis].min(), df[y_axis].max())
                          ),
          axis=alt.Axis(format=".1e")
          ),
)

在此處輸入圖片說明

當我使用 seaborn 繪制它時它有效

import seaborn as sns
sns.set_theme()
sns.regplot(x=df[x_axis], y=df[y_axis]);

在此處輸入圖片說明

沒有顯示錯誤消息 - 只是空圖。 控制台拋出這個警告

DevTools failed to load source map: Could not load content for https://cdn.jsdelivr.net/npm/vega-embed.min.js.map: HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE

到底是怎么回事?

問題是您的列名中有特殊字符,需要在 Altair 中進行轉義(參見例如https://altair-viz.github.io/user_guide/generated/core/altair.Color.html 中field文檔?highlight=escape )

為什么是這樣? . Vega-Lite 中的[][]用於訪問列的嵌套屬性。

最簡單的方法是在數據框列名稱中避免此類特殊字符。 或者,您可以使用反斜杠 ( \\ ) 轉義特殊字符,但要注意 Python 字符串中反斜杠的影響(使用r前綴進行原始字符串編碼)。 例如:

x_axis = 'Release Year'
y_axis = 'FP32 (single precision) Performance [FLOP/s]'
y_axis_escaped = r'FP32 (single precision) Performance \[FLOP/s\]'

# Libraries
import pandas as pd
import numpy as np
import altair as alt

# Download data
key = '1AAIebjNsnJj_uKALHbXNfn3_YsT6sHXtCU0q7OIPuc4'
sheet_name = 'HARDWARE_DATA'
url = f'https://docs.google.com/spreadsheets/d/{key}/gviz/tq?tqx=out:csv&sheet={sheet_name}'
df = pd.read_csv(url)

# Filter NaN datapoints
df = df[~df[x_axis].isna()]
df = df[~df[y_axis].isna()]

# Plot the dataset
alt.themes.enable('fivethirtyeight')
alt.Chart(df).mark_circle(size=60).encode(
  x=alt.X(f'{x_axis}:Q',
          scale=alt.Scale(
                          domain=(df[x_axis].min(), df[x_axis].max())
                          ),
          axis=alt.Axis(format=".0f")
          ),
  y=alt.Y(f'{y_axis_escaped}:Q',
          scale=alt.Scale(
                          domain=(df[y_axis].min(), df[y_axis].max())
                          ),
          axis=alt.Axis(format=".1e")
          ),
)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM