将网格隐藏在一组 vstacked 图中的特定梯形图中
Hide the grid in an a specificaltair plot within a set of vstacked plots
我正在尝试创建一个由 2 个垂直堆叠的图表组成的图:一个显示数据的时间序列图表,其下方的时间序列图表显示代表时间轴上事件的文本。我希望数据图表有网格,但下面的 mark_text 图表不显示外线也没有网格。我使用 chart.configure_axis(grid=False) 命令隐藏轴但出现以下错误:Objects with "config" attribute cannot be used within LayerChart. Consider defining the config attribute in the LayerChart object instead.
我不知道在哪里应用 configure_axis(grid=False) 选项,所以它只适用于底部图。对此的任何帮助将不胜感激。或任何关于如何以不同方式实现标签图的建议。
这是我的代码:
import altair as alt
import pandas as pd
import locale
from altair_saver import save
from datetime import datetime
file = '.\lagebericht.csv'
df = pd.read_csv(file, sep=';')
source = df
locale.setlocale(locale.LC_ALL, "de_CH")
min_date = '2020-02-29'
domain_pd = pd.to_datetime([min_date, '2020-12-1']).astype(int) / 10 ** 6
base = alt.Chart(source, title='Neumeldungen BS').encode(
alt.X('test_datum:T', axis=alt.Axis(title="",format="%b %y"), scale = alt.Scale(domain=list(domain_pd) ))
)
bar = base.mark_bar(width = 1).encode(
alt.Y('faelle_bs:Q', axis=alt.Axis(title="Anzahl Fälle"), scale = alt.Scale(domain=(0, 120)))
)
line = base.mark_line(color='blue').encode(
y='faelle_Total:Q')
chart1 = (bar + line).properties(width=600)
events= pd.DataFrame({
'datum': [datetime(2020,7,1), datetime(2020,5,15)],
'const': [1,1],
'label': ['allgememeiner Lockdown', 'Gruppen > 50 verboten'],
})
base = alt.Chart(events).encode(
alt.X('datum:T', axis=alt.Axis(title="", format="%b %y"), scale = alt.Scale(domain=list(domain_pd) ))
)
points = base.mark_rule(color='blue').encode(
y=alt.Y('const:Q', axis=alt.Axis(title="",ticks=False, domain=False, labels=False), scale = alt.Scale(domain=(0, 10)))
)
text = base.mark_text(
align='right',
baseline='bottom',
angle = 20,
dx=0, # Nudges text to right so it doesn't appear on top of the bar
dy=20,
).encode(text='label:O').configure_axis(grid=False)
chart2 = (points + text).properties(width=600, height = 50)
save(chart1 & chart2, r"images\figs.html")
这是没有 grid=False 选项的样子:
enter image description here
configure()
方法应该被认为是一种指定全局图表主题的方法;您不能在单个图表中使用不同的配置(有关此内容的讨论,请参阅 https://altair-viz.github.io/user_guide/customization.html#global-config-vs-local-config-vs-encoding)。
做你想做的事情的方法不是通过全局配置,而是通过轴设置。例如,您可以将 grid=False
传递给 alt.Axis
:
points = alt.Chart(events).mark_rule(color='blue').encode(
x=alt.X('datum:T', axis=alt.Axis(title="", format="%b %y"), scale = alt.Scale(domain=list(domain_pd) )),
y=alt.Y('const:Q', axis=alt.Axis(title="",ticks=False, domain=False, labels=False), scale = alt.Scale(domain=(0, 10)))
)
text = alt.Chart(events).mark_text().encode(
x=alt.X('datum:T', axis=alt.Axis(title="", grid=False, format="%b %y"), scale = alt.Scale(domain=list(domain_pd) )),
text='label:O'
)
我正在尝试创建一个由 2 个垂直堆叠的图表组成的图:一个显示数据的时间序列图表,其下方的时间序列图表显示代表时间轴上事件的文本。我希望数据图表有网格,但下面的 mark_text 图表不显示外线也没有网格。我使用 chart.configure_axis(grid=False) 命令隐藏轴但出现以下错误:Objects with "config" attribute cannot be used within LayerChart. Consider defining the config attribute in the LayerChart object instead.
我不知道在哪里应用 configure_axis(grid=False) 选项,所以它只适用于底部图。对此的任何帮助将不胜感激。或任何关于如何以不同方式实现标签图的建议。
这是我的代码:
import altair as alt
import pandas as pd
import locale
from altair_saver import save
from datetime import datetime
file = '.\lagebericht.csv'
df = pd.read_csv(file, sep=';')
source = df
locale.setlocale(locale.LC_ALL, "de_CH")
min_date = '2020-02-29'
domain_pd = pd.to_datetime([min_date, '2020-12-1']).astype(int) / 10 ** 6
base = alt.Chart(source, title='Neumeldungen BS').encode(
alt.X('test_datum:T', axis=alt.Axis(title="",format="%b %y"), scale = alt.Scale(domain=list(domain_pd) ))
)
bar = base.mark_bar(width = 1).encode(
alt.Y('faelle_bs:Q', axis=alt.Axis(title="Anzahl Fälle"), scale = alt.Scale(domain=(0, 120)))
)
line = base.mark_line(color='blue').encode(
y='faelle_Total:Q')
chart1 = (bar + line).properties(width=600)
events= pd.DataFrame({
'datum': [datetime(2020,7,1), datetime(2020,5,15)],
'const': [1,1],
'label': ['allgememeiner Lockdown', 'Gruppen > 50 verboten'],
})
base = alt.Chart(events).encode(
alt.X('datum:T', axis=alt.Axis(title="", format="%b %y"), scale = alt.Scale(domain=list(domain_pd) ))
)
points = base.mark_rule(color='blue').encode(
y=alt.Y('const:Q', axis=alt.Axis(title="",ticks=False, domain=False, labels=False), scale = alt.Scale(domain=(0, 10)))
)
text = base.mark_text(
align='right',
baseline='bottom',
angle = 20,
dx=0, # Nudges text to right so it doesn't appear on top of the bar
dy=20,
).encode(text='label:O').configure_axis(grid=False)
chart2 = (points + text).properties(width=600, height = 50)
save(chart1 & chart2, r"images\figs.html")
这是没有 grid=False 选项的样子: enter image description here
configure()
方法应该被认为是一种指定全局图表主题的方法;您不能在单个图表中使用不同的配置(有关此内容的讨论,请参阅 https://altair-viz.github.io/user_guide/customization.html#global-config-vs-local-config-vs-encoding)。
做你想做的事情的方法不是通过全局配置,而是通过轴设置。例如,您可以将 grid=False
传递给 alt.Axis
:
points = alt.Chart(events).mark_rule(color='blue').encode(
x=alt.X('datum:T', axis=alt.Axis(title="", format="%b %y"), scale = alt.Scale(domain=list(domain_pd) )),
y=alt.Y('const:Q', axis=alt.Axis(title="",ticks=False, domain=False, labels=False), scale = alt.Scale(domain=(0, 10)))
)
text = alt.Chart(events).mark_text().encode(
x=alt.X('datum:T', axis=alt.Axis(title="", grid=False, format="%b %y"), scale = alt.Scale(domain=list(domain_pd) )),
text='label:O'
)