如何在Altair中隐藏轴线但在图表中显示刻度,同时主动使用“轴”参数?

2024-07-02 11:51:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道使用axis=None隐藏轴线。但是,当您主动使用axis修改图形时,是否可以只保留记号,而隐藏X轴和Y轴的轴线

例如,这里有一个图表,我希望它发生在-

import pandas as pd
import altair as alt

df = pd.DataFrame({'a': [1,2,3,4], 'b':[2000,4000,6000,8000]})

alt.Chart(df).mark_trail().encode(
    x=alt.X('a:Q', axis=alt.Axis(titleFontSize=12, title='Time →', labelColor='#999999', titleColor='#999999', titleAlign='right', titleAnchor='end', titleY=-30)),
    y=alt.Y('b:Q', axis=alt.Axis(format="$s", tickCount=3, titleFontSize=12, title='Cost →', labelColor='#999999', titleColor='#999999', titleAnchor='end')),
    size=alt.Size('b:Q', legend=None)
).configure_view(strokeWidth=0).configure_axis(grid=False)

输出应该类似于此SOpost中的刻度。
注意:该帖子中的情节与此处提供的演示无关。这只是为了理解的目的


Tags: importnonedftitleasaltendpd
1条回答
网友
1楼 · 发布于 2024-07-02 11:51:41

Vega Lite将轴线称为。您可以通过将domain=False传递到轴配置来隐藏它:

import pandas as pd
import altair as alt

df = pd.DataFrame({'a': [1,2,3,4], 'b':[2000,4000,6000,8000]})

alt.Chart(df).mark_trail().encode(
    x=alt.X('a:Q', axis=alt.Axis(titleFontSize=12, title='Time →', labelColor='#999999', titleColor='#999999', titleAlign='right', titleAnchor='end', titleY=-30)),
    y=alt.Y('b:Q', axis=alt.Axis(format="$s", tickCount=3, titleFontSize=12, title='Cost →', labelColor='#999999', titleColor='#999999', titleAnchor='end')),
    size=alt.Size('b:Q', legend=None)
).configure_view(strokeWidth=0).configure_axis(grid=False, domain=False)

enter image description here

相关问题 更多 >