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.

Use dataframe index as X axis

See original GitHub issue

Hi, I am trying to use a pandas dataframe as the Chart data in altair, but I need to use the index of the dataframe as the X axis. My dataframe looks like this

image

with index of type

DatetimeIndex(['2014-04-01 00:00:00', '2014-04-01 00:05:00',
               '2014-04-01 00:10:00', '2014-04-01 00:15:00',
               '2014-04-01 00:20:00', '2014-04-01 00:25:00',
               '2014-04-01 00:30:00', '2014-04-01 00:35:00',
               '2014-04-01 00:40:00', '2014-04-01 00:45:00',
               ...
               '2014-04-14 23:10:00', '2014-04-14 23:15:00',
               '2014-04-14 23:20:00', '2014-04-14 23:25:00',
               '2014-04-14 23:30:00', '2014-04-14 23:35:00',
               '2014-04-14 23:40:00', '2014-04-14 23:45:00',
               '2014-04-14 23:50:00', '2014-04-14 23:55:00'],
              dtype='datetime64[ns]', name='timestamp', length=4032, freq=None)

I tried something like this, but to no use.

from altair import Chart, X, Y
Chart(data1).mark_line().encode(
    X('index'),
    Y('value'),
)

How should I do it? I know that if I convert my index to a column it can do the trick, but is there another way?

Issue Analytics

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

github_iconTop GitHub Comments

23reactions
jakevdpcommented, Nov 22, 2016

Altair only recognizes column data; it ignores index values. You can plot the index data by first resetting the index:

Chart(data.reset_index()).mark_line().encode(
    x='index',
    y='value'
)
2reactions
nova77commented, Apr 27, 2018

@afonit Thanks, that works. I apologize for the noob question, but I found it surprisingly difficult to do something as simple, and .reset_index() doesn’t seem very intuitive.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using a Pandas dataframe index as values for x-axis in ...
You can use yourDataFrame.plot(use_index=True) to use the DataFrame Index On X-Axis. The "use_index=True" sets the DataFrame Index on the ...
Read more >
Using a Pandas dataframe index as values for x-axis ... - Reddit
I have time series in a Pandas dateframe with a number of columns which I'd like to plot. Is there a way to...
Read more >
How to Use Index in Pandas Plot (With Examples) - Statology
You can use one of the following methods to use the values in the index of a pandas DataFrame as the x-axis values...
Read more >
Pandas: Create matplotlib plot with x-axis label not index
I've been using matplotlib a bit recently, and wanted to share a lesson I learnt about choosing the label of the x-axis.
Read more >
pandas.DataFrame.plot — pandas 0.13.1 documentation
Make line, bar, or scatter plots of DataFrame series with the index on the x-axis using matplotlib / pylab. Parameters : frame :...
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