user19087
user19087

Reputation: 2063

Vega: empty bar mark

Does Vega/Vega-Lite/Altair have a builtin method to draw a special mark for empty bars? When x == x2 no mark is currently shown. Perhaps a vertical rule mark of the same expected bar color as derived from a third encoding? Or perhaps a semi-transparent bar mark covering an expanded region with a red border?

data =\
[ ("a", 1, 100, 123, 4.5)
, ("a", 2, 140, 190, 5.6)
, ("a", 3, 402, 402, 1.6)
, ("b", 1, 100, 123, 5.7)
, ("b", 2, 134, 456, 6.7)
, ("b", 3, 503, 504, 8.2)
, ("b", 4, 602, 765, 1.1)
, ("c", 1, 95,  95,  0.1)
, ("c", 2, 140, 145, 7.5)
, ("c", 3, 190, 190, 9.9)
]
data = pd.DataFrame(data, columns=["k","ki","min","max","other"])
chart =\
    ( alt.Chart(data)
    . mark_bar()
    . encode
        ( x="min"
        , x2="max"
        , y="k:O"
        , color="ki:N"
        , tooltip=["min", "max", "other"]
        )
    . interactive()
    . properties
        ( width="container"
        , height=300
        )
    )

A calculate transform for min/max could reach a solution via an expanded region, with a conditional opacity on the original min/max fields. Not sure about the red border. The downside to this approach is the bar increases with zoom, unlike a rule mark.

A rule mark has the advantage of not misrepresenting the data but might be hard to spot. It would require a filter transform though I'm not sure if I have to build from the initial bar chart, or if I can chain mark_bar -> transform_filter -> mark_rule.

Anyway a solution is complicated both technically and in terms of data representation and I was wondering if Vega/Altair has a builtin solution to make either easier.

Upvotes: 2

Views: 302

Answers (1)

jakevdp
jakevdp

Reputation: 86310

You can set the stroke color for the outlines of the bars using something like mark_bar(stroke='gray') (it defaults to transparent): then empty bars will be shown by their outline:

enter image description here

Upvotes: 1

Related Questions