简体   繁体   English

Altair 中的空散点图

[英]Empty scatter plot in Altair

I am very puzzled on why this code does not work:我很困惑为什么这段代码不起作用:

#@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")
          ),
)

在此处输入图片说明

It works when I plot it using seaborn当我使用 seaborn 绘制它时它有效

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

在此处输入图片说明

No error message is shown - just the empty plot.没有显示错误消息 - 只是空图。 The console throws this warning控制台抛出这个警告

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

What is going on?到底是怎么回事?

The issue is that you have special characters in your column name, which need to be escaped in Altair (see eg the field documentation in https://altair-viz.github.io/user_guide/generated/core/altair.Color.html?highlight=escape )问题是您的列名中有特殊字符,需要在 Altair 中进行转义(参见例如https://altair-viz.github.io/user_guide/generated/core/altair.Color.html 中field文档?highlight=escape )

Why is this?为什么是这样? Characters like .. and [] in Vega-Lite are used to access nested attributes of columns. Vega-Lite 中的[][]用于访问列的嵌套属性。

The easiest approach would be to avoid such special characters in your dataframe column names.最简单的方法是在数据框列名称中避免此类特殊字符。 Alternatively, you can escape the special characters with a back-slash ( \\ ) though be careful about the effect of back-slashes in Python strings (use an r prefix for raw string encoding).或者,您可以使用反斜杠 ( \\ ) 转义特殊字符,但要注意 Python 字符串中反斜杠的影响(使用r前缀进行原始字符串编码)。 For example:例如:

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