代码之家  ›  专栏  ›  技术社区  ›  Jsevillamol

Altair中的空散点图

  •  0
  • Jsevillamol  · 技术社区  · 3 年前

    我很困惑为什么这个代码不起作用:

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

    enter image description here

    当我用seaborn绘制它时,它就起作用了

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

    enter image description here

    没有显示任何错误消息,只有空的绘图。控制台发出此警告

    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
    

    发生了什么事?

    0 回复  |  直到 3 年前
        1
  •  2
  •   jakevdp    3 年前

    问题是您的列名中有特殊字符,需要在Altair中转义(例如,请参阅 field 中的文档 https://altair-viz.github.io/user_guide/generated/core/altair.Color.html?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")
              ),
    )
    

    enter image description here