简体   繁体   中英

Set axis in altair bar chart as a integer

I am trying to visualization a bar plot of many statistic data, and wanna set y-axis as a integer (there is no float type data in my dataset)

This is one of the charts, which I want to change the axis.

Image Link

This is my python source code to visualization this chart

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
    )

waiting for the solution, thank you:)

You could set 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))
)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM