question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Date axis labels are off by one

See original GitHub issue

I have a time-series chart that I’d like to specify the tick labels for, but I can’t seem to get the right dates to show up. An MWE looks like this:

import altair as alt
import pandas as pd

axis_dates = ['2020-03-01', '2020-03-08', '2020-03-15', '2020-03-22', '2020-03-29', '2020-04-05']

df = pd.DataFrame({
    'x': axis_dates,
    'y': [1, 3, 5, 6, 4, 2]
})

alt.Chart(df).mark_line().encode(
    x=alt.X('x:T', axis=alt.Axis(title='Date', format=("%b %d"), values=axis_dates)),
    y='y')

image

How can I make the chart use the dates in the DataFrame? Thank you.

I am using 4.1.0.

EDIT: fixed my code

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
jakevdpcommented, Apr 9, 2020

The issue is you are not passing full ISO8601 strings, so some browsers parse them as UTC time, and then they are displayed in local time by Vega-Lite. This is why Altair always outputs full ISO strings (e.g. 2010-01-01T00:00:00) which javascript always parses as local time. In general, the easiest way to deal with this is to always use pandas datetime type for time columns, then Altair takes care of the details for you.

1reaction
jakevdpcommented, Apr 9, 2020

e.g.

import altair as alt
import pandas as pd

df = pd.DataFrame({
    'x': pd.to_datetime(['2020-03-01', '2020-03-08', '2020-03-15', '2020-03-22', '2020-03-29', '2020-04-05']),
    'y': [1, 3, 5, 6, 4, 2]
})

alt.Chart(df).mark_line().encode(
    x=alt.X('x:T', axis=alt.Axis(title='Date', format=("%b %d"))),
    y='y'
  )

visualization - 2020-04-09T092406 249

Read more comments on GitHub >

github_iconTop Results From Across the Web

Display or change dates on a category axis - Microsoft Support
In the chart, right-click the category axis, and then click Format Axis. · In the Format Axis pane, select the Axis Options tab....
Read more >
Date Axis in Excel Chart is wrong - AuditExcel
You've built your chart, it all makes sense and suddenly you look at your horizontal axis, and the date axis is wrong. This...
Read more >
Horizontal date axis incorrect on Excel line chart with ...
It turns out I accidentally keyed in a date that does not exist. ... (category) axis labels, if one of them is blank,...
Read more >
Date axis labels in ggplot2 is one day behind - Stack Overflow
The dates in the data frame is May 1, May 2, and up to May 10, and the labels goes from April 30...
Read more >
Excel axis shows wrong date- start of month or large gaps in ...
Date Axis in Excel Chart is wrong How to force Excel to use your typed in dates in a chart Although this feature...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found