I have some line charts that display a percentage year-over-year. The number of years displayed can vary from one year to ten years. A chart with ten years of data looks fine. A chart with 1 year of data isn’t going to win any awards (it’s just a single point in the middle of the chart), but it’s fine. However, charts with 2 - 4 years of data are ugly, and I cannot seem to figure out a solution. Sample with 2 years of data:
Year Grade 3 Grade 4 Grade 5 School Name
0 2021 0.193548 0.071429 0.153846 Sample School
1 2022 0.136364 0.347826 0.142857 Sample School
My code:
color=['#f4979c','#df8f2d','#a4dbdb','#165b65','#b1b134']
cols=[i for i in data.columns if i not in ['School Name','Year']]
fig = px.line(
data,
x='Year',
y=cols,
markers=True,
color_discrete_sequence=color,
)
fig.update_traces(mode='markers+lines', hovertemplate=None)
fig.update_layout(
margin=dict(l=40, r=40, t=40, b=60),
title_x=0.5,
font = dict(
family = 'Open, sans-serif',
color = 'steelblue',
size = 10
),
plot_bgcolor='white',
xaxis = dict(
title='',
tickmode = 'array',
tickvals = data['Year'],
tickformat="%Y",
categoryorder = 'array',
categoryarray = data['Year'],
mirror=True,
showline=True,
linecolor='#b0c4de',
linewidth=.5,
gridwidth=.5,
showgrid=True,
gridcolor='#b0c4de',
zeroline=False,
),
legend=dict(orientation="h"),
hovermode='x unified',
height=400,
legend_title='',
)
fig.update_yaxes(
title='',
mirror=True,
showline=True,
linecolor='#b0c4de',
linewidth=.5,
gridwidth=.5,
showgrid=True,
gridcolor='#b0c4de',
zeroline=False,
range=[0, 1],
dtick=.2,
tickformat=',.0%',
)
The output:
Three years of data is similarly ugly, it simply keeps the outer ticks in the same position and adds a tick in the middle. Ideally, the tick lines would be evenly spaced, in the case above, dividing the chart into equal thirds. Three years of data would result in a chart divided into equal quarters, etc. etc.
It seems like this would be easy to accomplish but I have tried multiple things, pretty much everything I could find on Stack and this website (hours of work), with no success.
What am I missing?