You're just missing the clip
parameter in the Chart.mark_area
method. From the Altair docs, this parameter determines "Whether a mark be clipped to the enclosing group's width and height
".
So, all you need to do is add the following line (don't forget a comma at the end of the line above!):
alt.Chart(price_data).mark_area(
line={'color': 'darkgreen'},
color=alt.Gradient(
gradient='linear',
stops=[alt.GradientStop(color='white', offset=0),
alt.GradientStop(color='darkgreen', offset=1)],
x1=1,
x2=1,
y1=1,
y2=0
),
clip=True, # add this line
).encode(
alt.X('date:T', title="Date"),
alt.Y('close:Q', scale=alt.Scale(domain=[y_min, y_max]), title="Close Price")
).properties(
title=f"{symbol} Price Trend"
)
For the dataset I had, here is the before chart:
And the after chart: