繁体   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