Vega-lite中是否可能出现山脊线图?

时间:2019-10-09 20:36:21

标签: python vega-lite

是否可以在vega-lite中制作类似this的情节?

文档中似乎没有任何关于三维的概念。是否有某种解决方法可以使像这样的重叠图成为可能?

1 个答案:

答案 0 :(得分:0)

是的,尽管需要一些努力。这是使用https://observablehq.com/@vega/psr-b1919-21-with-vega-lite改编而成的Altair的示例(它需要预处理数据源,因此最好使用输出Vega-Lite规范的框架):

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

# Note: data is in a CSV with no header info. We first load it as a 2D array,
# and then create a one-column dataframe where each entry is a list.
# This leads to a compact data representation, from which we use Altair
# transforms to tidy the data within the chart specification.
url = 'https://gist.githubusercontent.com/borgar/31c1e476b8e92a11d7e9/raw/0fae97dab6830ecee185a63c1cee0008f6778ff6/pulsar.csv'
values = np.genfromtxt(url, delimiter=',')
df = pd.Series(values.tolist(), name='data').to_frame()

# These values tweak the spacing & overlap between lines.
step=6
overlap=12

alt.Chart(df).transform_window(
    series='row_number()'
).transform_flatten(
    ['data']
).transform_window(
    index='row_number()',
    groupby=['series']
).mark_line(
    fill='white',
    stroke='black',
    strokeWidth=1
).encode(
    x=alt.X('index:Q', axis=None),
    y=alt.Y('data:Q', scale=alt.Scale(range=[step, -overlap * (step + 1)]), axis=None),
    row=alt.Row('series:O', header=alt.Header(title=None, labelPadding=0, labelFontSize=0)),
    tooltip=None
).properties(
    width=370,
    height=step,
    bounds='flush',
    spacing=0,
    padding=0,
).configure_view(
    stroke=None,
    fill='white'
)

enter image description here