0

I have some issues to plot some data over datetime. Lets suppose I have the following data set:

                       Date           DateUnix  LaserPower  Index
0 2020-02-24 11:22:39+01:00 1582539759.0000000   1.5256180      0
1 2020-02-24 11:22:40+01:00 1582539760.0000000   1.5480100      1
2 2020-02-24 11:22:41+01:00 1582539761.0000000   1.5184820      2
3 2020-02-24 11:22:42+01:00 1582539762.0000000   1.5116720      3
4 2020-02-24 11:22:43+01:00 1582539763.0000000   1.4537440      4

Now I would like to plot the LaserPower over the Date. I tried the approach to reset the index by the column "Date"

overallBin.set_index(['Date'],inplace=True)

and than plot the data by

ax.plot(overallBin.index, overallBin["LaserPower"])

This yields to the following error: TypeError: float() argument must be a string or a number, not 'Timestamp' I checked the data type and it should be ok...

Date          datetime64[ns, Europe/Berlin]
DateUnix                            float64
LaserPower                          float64
Index                                 int64
dtype: object

Do you know how to fix this? I thought matplotlib could plot datetime?

Nevertheless, I tried another approach to create a new column "Date1" with the following code:

overallBin['Date1']=overallBin.Date.apply(lambda x: x.strftime('%Y-%m-%d %H:%M:%S'))
ax.plot(overallBin.Date1, overallBin["LaserPower"])

This works fine and python plotted the date on the x axis, but... in a very shitty way, because I got a "wonderful thick black bar". How can I change the xticks that I did not get a xtick for each datapoint?

I do not know, which approach is better for plotting a date on x axis, because I am an absolutely Python beginner. But maybe you have some suggestions.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Klamsi
  • 73
  • 6

0 Answers0