'How do I plot my datetime on the x axis when this value is used as index?

I have a short question.

This is my dataframe:

                     gradient  result
date                                 
2022-04-15 09:43:20  0.206947    0.10
2022-04-15 10:25:00  0.102620    0.10
2022-04-15 11:06:40  0.019450    0.06
2022-04-15 11:48:20  0.025945    0.08
2022-04-15 12:30:00  0.022455    0.08
                      ...     ...
2022-05-02 14:13:20  0.003770    0.02
2022-05-02 14:55:00  0.084120    0.10
2022-05-02 15:36:40  0.134970    0.10
2022-05-02 16:18:20  0.261385    0.10
2022-05-02 17:00:00  0.955833     NaN

And the data types are:

gradient    float64
result      float64
dtype: object

So I want and have the date as index. But now I want to plot the gradient on the (y-axis) and the date on the (x-axis):

x = data_646_mean.gradient
y = data_646_mean.date

But this give me the following error:

AttributeError: 'DataFrame' object has no attribute 'date'

Does anyone know how to solve this error? I really hope I don't need to change my whole script to get the date out of the index.

Thank you for reading!

p.s. I already did what Andreas Deak suggest, but this does not work.

input:

x = data_646.mean.index
y = data_646.mean.gradient

plt.plot(x, y, 'o', color='black');

output:

AttributeError: 'function' object has no attribute 'index'


Solution 1:[1]

You can use pandas plot:

 data_646_mean.plot() for a line plot

or

 data_646_mean.plot(marker='.', linestyle='none')

But makes sure your index is datetime dtype, first:

 data_646_mean.index = pd.to_datetime(data_646_mean.index)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Scott Boston