vega / altair

Declarative statistical visualization library for Python
https://altair-viz.github.io/
BSD 3-Clause "New" or "Revised" License
9.25k stars 789 forks source link

Legend cut off upon `save()` #3594

Open ck578 opened 1 week ago

ck578 commented 1 week ago

What happened?

I am generating a series of plots using altair and saving them as png files with .save(). All of the images render correctly in my editor, but upon saving the legend is cutting off on the right hand side. Here is my code and an example of the saved image

import altair as alt
import pandas as pd
from vega_datasets import data

data = {
    'Date': ['2024-01', '2024-01', '2024-01', '2024-02', '2024-02', '2024-02',
             '2024-03', '2024-03', '2024-03', '2024-04', '2024-04', '2024-04'],
    'Category': ['First category', 'Second Category', 'Third Category', 'First category', 'Second Category', 'Third Category', 'First category', 'Second Category', 'Third Category', 'First category', 'Second Category', 'Third Category'],
    'Value': [10, 15, 20, 25, 10, 15, 30, 20, 25, 35, 30, 20]
}

df = pd.DataFrame(data)

chart = alt.Chart(df).mark_bar().encode(
    x='Date:T',
    y='sum(Value):Q',
    color='Category:N',
    order='Category:N'
).properties(
    width=600,
    height=400,
    title='Stacked Bar Chart Trend Over Time'
).configure_axis(
    labelFontSize=12,
    titleFontSize=14
).configure_title(
    fontSize=16
)

chart.display()
chart.save(os.path.join(output_dir,'github_issue.png'))

github_issue

What would you like to happen instead?

I would like the saved file to match the rendered image. Here is what the image looks like before saving

image

Which version of Altair are you using?

5.2.0

joelostblom commented 1 week ago

That chart looks correct when I run the code on Altair 5.4.1. Could you try updating? If that doesn't work, please list which version of vl-convert you have installed