简体   繁体   English

Altair中的项目符号图

[英]Bullet chart in Altair

I am trying to reproduce this Vega-lite chart in Altair, but encountering some issues. 我试图在Altair中重现这张Vega-lite图表 ,但遇到一些问题。 Here's what I have so far: 这是我到目前为止的内容:

# data import and prep
import json
import altair as alt
import pandas as pd

df = pd.read_json("""[{"title":"Revenue","subtitle":"US$, in thousands","ranges":[150,225,300],"measures":[220,270],"markers":250},
{"title":"Profit","subtitle":"%","ranges":[20,25,30],"measures":[21,23],"markers":26},
{"title":"Order Size","subtitle":"US$, average","ranges":[350,500,600],"measures":[100,320],"markers":550},
{"title":"New Customers","subtitle":"count","ranges":[1400,2000,2500],"measures":[1000,1650],"markers":2100},
{"title":"Satisfaction","subtitle":"out of 5","ranges":[3.5,4.25,5],"measures":[3.2,4.7],"markers":4.4}]""")

df[['measure1','measure2']] = pd.DataFrame(df.measures.values.tolist(), index=df.index)

df[['low', 'medium', 'high']] = pd.DataFrame(df.ranges.values.tolist())

# chart
base = alt.Chart(df).encode(row = 'title:O')
m1 = base.mark_bar().encode(x='measure1:Q')
m2 = base.mark_tick().encode(x='measure2:Q')

So far so good. 到现在为止还挺好。 When I try to layer the two charts, however: 但是,当我尝试对两个图表进行分层时:

m1 + m2

SchemaValidationError: Invalid specification

    altair.vegalite.v2.api.LayerChart->layer->items, validating 'anyOf'

    {'data': {'name': 'data-58353a9bcf31ee710e2a5cb2da21a143'}, 'mark': 'bar', 'encoding': {'row': {'type': 'nominal', 'field': 'title'}, 'x': {'type': 'quantitative', 'field': 'measure1'}}} is not valid under any of the given schemas

Note that this works if I specify a y encoding in both layers and facet at the end, however that defeats the purpose of having facets (all the Y axis marks are repeated in all facets. If I specify neither the row encoding in the base chart nor the y encoding, only one bar gets plotted (the largest one). 请注意,如果我在两个图层和最后一个方面都指定了y编码,则此方法有效,但是这违背了具有多个切面的目的(所有Y轴标记都在所有切面中重复。如果我在基图中未指定row编码也不使用y编码,只会绘制一个条形图(最大的一个)。

The reason I want facets is so I can specify independent x scales given the different domain of the data (see original example). 我需要构面的原因是,鉴于数据的不同域,我可以指定独立的x比例(请参见原始示例)。

Thanks for your help! 谢谢你的帮助!

In both Altair and vega-lite, it is invalid to layer two faceted charts (in general, there is no guarantee that two faceted charts will line up when layering). 在Altair和vega-lite中,对两个多面图表进行分层都是无效的(通常,不能保证在分层时两个多面图表会对齐)。 If you look closely at the vega-lite chart, you'll see that instead of layering faceted charts, it facets a layered chart. 如果您仔细观察素食主义者图表,您会发现它不是分层的多面图,而是分层的图。

The same can be accomplished in Altair this way: 在Altair中可以通过以下方式完成相同的操作:

import altair as alt
import pandas as pd

df = pd.DataFrame.from_records([
    {"title":"Revenue","subtitle":"US$, in thousands","ranges":[150,225,300],"measures":[220,270],"markers":[250]},
    {"title":"Profit","subtitle":"%","ranges":[20,25,30],"measures":[21,23],"markers":[26]},
    {"title":"Order Size","subtitle":"US$, average","ranges":[350,500,600],"measures":[100,320],"markers":[550]},
    {"title":"New Customers","subtitle":"count","ranges":[1400,2000,2500],"measures":[1000,1650],"markers":[2100]},
    {"title":"Satisfaction","subtitle":"out of 5","ranges":[3.5,4.25,5],"measures":[3.2,4.7],"markers":[4.4]}
])

alt.layer(
    alt.Chart().mark_bar(color='#eee').encode(alt.X("ranges[2]:Q", scale=alt.Scale(nice=False), title=None)),
    alt.Chart().mark_bar(color='#ddd').encode(x="ranges[1]:Q"),
    alt.Chart().mark_bar(color='#ccc').encode(x="ranges[0]:Q"),
    alt.Chart().mark_bar(color='lightsteelblue', size=10).encode(x='measures[1]:Q'),
    alt.Chart().mark_bar(color='steelblue', size=10).encode(x='measures[0]:Q'),
    alt.Chart().mark_tick(color='black').encode(x='markers[0]:Q'),
    data=df
).facet(
    row="title:O"
).resolve_scale(
    x='independent'
)

在此处输入图片说明

Some of the style/configuration options from the original chart are missing, but this is the rough idea. 原始图表中的某些样式/配置选项丢失了,但这是一个粗略的主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM