将 altair 条形图中的轴设置为整数

M.F*_*qqi 4 python axis altair

我正在尝试将许多统计数据的条形图可视化,并希望将 y 轴设置为整数(我的数据集中没有浮点型数据)

这是我想更改轴的图表之一。

图片链接

这是我的python源代码来可视化这个图表

def plot_3(data,x,y,width):
    selector = alt.selection_single(encodings=['x', 'color'])
    bars = alt.Chart(data).mark_bar(opacity=0.8).encode(
        alt.X('Tahun:O', title=''),
        alt.Y('N:Q', title=x, axis=alt.Axis(format='.0f')), # this format axis has no effect
        alt.Column('Keterangan:N', title=y),color=alt.condition(selector, 'Tahun:O', alt.value('lightgray')),
        tooltip = ['Tahun','Keterangan','N','Satuan']
    ).add_selection(selector
    ).interactive(
    ).resolve_scale(x='independent')
    
    return bars.properties(width=width
    )
Run Code Online (Sandbox Code Playgroud)

等待解决,谢谢:)

eit*_*ees 6

你可以设置tickMinStep=1

import altair as alt
import pandas as pd

source = pd.DataFrame({
    'a': ['A', 'B', 'C', 'D'],
    'b': [2.0, 1.0, 1.0, 3.0]
})

alt.Chart(source).mark_bar().encode(
    alt.X('a:N'),
    alt.Y('b:Q', axis=alt.Axis(tickMinStep=1))
)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明