Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

python - Altair: Converting Y-Axis to % Format in Global Configuration (vs Local Config)

I need my Y-axis labels to be in the '%' format. I know how to do this with local configuration:

line1 = alt.Chart(source).mark_line(
).encode(
    x='Date:T', 
    y=alt.Y('value:Q', title="Some Title", axis=alt.Axis(format='%')), 
)

line1

In the above example, the "axis=alt.Axis(format='%')" argument converts the axis to % instead of decimal.

What I need to know, however, is how to do this for the global configuration. For instance:

line1 = alt.Chart(source).mark_line(
).encode(
    x='Date:T', 
    y=alt.Y('value:Q'), 
)

line1.properties(
        width=800, 
        height=500, 
    ).configure_axisY(
        labelFontSize=15,
        titleFontSize=15, 
    )

Is there an attribute for .configure_axisY() that I can use to do this? I am glancing through all the options and nothing seems obvious.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use chart.configure(numberFormat='%'). For example:

import altair as alt
import pandas as pd
import numpy as np

source = pd.DataFrame({
    'Date': pd.date_range('2021-01-01', freq='D', periods=7),
    'value': np.linspace(0, 1, 7),
})

alt.Chart(source).mark_line().encode(
    x='Date:T', 
    y=alt.Y('value:Q', title="Some Title"), 
).configure(
    numberFormat='%'
)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...