Plotly rounding years to decade in bar chart

I can’t figure out how to display the actual years on the x axis. Notice in the image that all years are rounded down to the decade.

Here’s the code that is called in a function:


 years = [1955, 1965, 1975, 1985, 1995, 2005, 2015]


 # Global Temperatures
 # https://www.ncei.noaa.gov/access/monitoring/climate-at-a-glance/global/time-series

df_temp_anomaly = pd.read_csv('https://raw.githubusercontent.com/rebeccapeltz/emissions/refs/heads/main/global-temp-anomaly-data.csv',skiprows=4)

 temp_anomalies = df_temp_anomaly[df_temp_anomaly['Year'].isin(years)]['Anomaly'].tolist()

    
# Create the bar chart
fig2 = go.Figure(data=[
        go.Bar(x=years, y=temp_anomalies, marker_color='black')
])

fig2.update_layout(
        title='Global Temperature Anomalies<br><a href="https://raw.githubusercontent.com/rebeccapeltz/emissions/refs/heads/main/global-temp-anomaly-data.csv" target="_blank">data</a>',
        xaxis_title='years',
        yaxis_title='Temp Anomalies C &#176;',
        template='plotly_white'
)
return fig2

I think you can get the intended graph by changing the x-axis setting to x=[str(y) for y in years].Also, you can set the x-axis as a categorical variable. fig2.update_xaxes(type=‘category’)

1 Like

What you see are actually not rounded values but the default ticks. If you need specific ticks you might take a look at this:

1 Like