Altair并排绘制两个堆栈条形图

oxiaedzo  于 2022-10-22  发布在  Python
关注(0)|答案(2)|浏览(161)

我正在尝试在堆栈条形图中并排绘制两个不同的属性。
这就是我所拥有的:

  • 导入库
import pandas as pd
from datetime import datetime 
from dateutil.relativedelta import relativedelta
import random
import altair as alt
  • 玩具数据:
df = pd.DataFrame(data={
    "date": [datetime.now()+relativedelta(month=+i) for i in range(5)],
    "label": random.choices(["positive", "negative"], k=5),
    "prediction": random.choices(["likely", "very_likely", "unlikely", "very_unlikely"], k=5),
    "indicator": random.choices(["0", "1"], k=5),
})
  • 牛郎星图:
selection1 = alt.selection_multi(fields=["prediction"], bind="legend")
plot1 = (
    alt.Chart()
    .mark_bar()
    .encode(
        x=alt.X("count(prediction):O", title="count", stack="zero"),
        y=alt.Y("month(date):N", title=None),
        color=alt.Color("prediction:N", scale=alt.Scale(scheme="tableau10")),
        opacity=alt.condition(selection1, alt.value(1), alt.value(0.2)),
        tooltip="prediction:N",
    )
    .add_selection(selection1)
)

selection2 = alt.selection_multi(fields=["label"], bind="legend")
plot2 = (
    alt.Chart()
    .mark_bar()
    .encode(
        x=alt.X("count(label):O", title="count", stack="zero"),
        y=alt.Y("month(date):N", title=None),
        color=alt.Color("label:N", scale=alt.Scale(scheme="tableau10")),
        opacity=alt.condition(selection2, alt.value(1), alt.value(0.2)),
        tooltip="label:N",
    )
    .add_selection(selection2)
)

text = (
    alt.Chart()
    .mark_text(dx=-15, dy=3, color="white")
    .encode(
        x=alt.X("count(prediction):O", title="label count", stack="zero"),
        y=alt.Y("month(date):N", title=None),
        detail="sipredictionte:N",
        text=alt.Text("count(prediction):Q", format=".1f"),
    )
)

plot = alt.layer((plot1+plot2)+text, data=df).facet(
    column=alt.Column("indicator", title="title"),
)

此代码生成以下绘图:


小时

  • 预期

这有点像我正在尝试的情节,我不确定牛郎星是否可能。


小时
基本上,我有两个字段,每个月我都要在一个列中并排绘制。对于每个箱子,我想知道每个图例的计数。

b09cbbtk

b09cbbtk1#

分组条形图在Altair中的工作方式略有不同。您需要使用行和列的组合。为了得到您想要的,您还应该使用Y轴变量作为行变量。像这样的东西

selection1 = alt.selection_multi(fields=["prediction"], bind="legend")
plot1 = (
    alt.Chart()
    .mark_bar()
    .encode(
        x=alt.X("count(prediction):O", title="count", stack="zero",axis=alt.Axis(ticks=False, domain=False)),
        y=alt.Y("month(date):N", title=None),
        color=alt.Color("prediction:N", scale=alt.Scale(scheme="tableau10")),
        opacity=alt.condition(selection1, alt.value(1), alt.value(0.2)),
        tooltip="prediction:N",
    )
    .add_selection(selection1)
)

selection2 = alt.selection_multi(fields=["label"], bind="legend")
plot2 = (
    alt.Chart()
    .mark_bar()
    .encode(
        x=alt.X("count(label):O", title="",axis=None),
        y=alt.Y("label:N", title=None),
        color=alt.Color("label:N", scale=alt.Scale(scheme="tableau10")),
        opacity=alt.condition(selection2, alt.value(1), alt.value(0.2)),
        tooltip="label:N",
    )
    .add_selection(selection2)
)

text = (
    alt.Chart()
    .mark_text(dx=-15, dy=3, color="white")
    .encode(
        x=alt.X("count(prediction):O", title="label count", axis=None),
        y=alt.Y("month(date):N", title=None, axis=None),
        detail="sipredictionte:N",
        text=alt.Text("count(prediction):Q", format=".1f"),
    )
)

plot = alt.layer((plot1+plot2)+text, data=df
).facet(
    column=alt.Column("indicator", title="title",),
    row = alt.Row("month(date):N", title='')

).resolve_scale(
    y='independent'
).configure_axis(
    grid=False, 
    domain=False,
    ticks=False, 
).configure_view(
    strokeWidth=0
).configure_header(
    labels=False
)
plot


小时

ulydmbyx

ulydmbyx2#

将图更改为烛台图,其中y1是预测,y2是标签,似乎适合我的用例。

相关问题