0

I'm trying to plot date vs num with these datasets

                    date    num
0   22/12/2022 09:09    7.92
1   22/12/2022 09:09    7.95
2   22/12/2022 09:09    7.98
3   22/12/2022 09:09    7.95
4   22/12/2022 09:09    7.93

... ... ...

4514    23/12/2022 14:48    5.68
4515    23/12/2022 14:48    5.68
4516    23/12/2022 14:48    5.69
4517    23/12/2022 14:48    5.70
4518    23/12/2022 14:48    5.66

When the date column type is a string it gets the correct plot

data_1.plot(x='date',y='num')
plt.xticks(rotation = 45)

enter image description here

However, when I converted the date to datetime, it plotted datetime which doesn't exist in data

data_1['date'] = pd.to_datetime(data_1['date'], format='%d/%m/%Y %H:%M')
data_1.plot(x='date',y='num')
plt.xticks(rotation = 45)

enter image description here

Do you know what is the problem? I'm having a hard time searching for the solution, thank you

Updated thanks for editing my question and giving me the solution, but still the solution is suggesting to convert to string not solve the problem using datetime

Muhammad M
  • 13
  • 3
  • What do you mean by "plotting datetime that doesn't exist" Are you referring to the line that connects the values in between ~12h Dec 22 and ~9h Dec 23 ? – FObersteiner Dec 31 '22 at 11:17
  • It seems like there is a large gap in your time series (between ~22-12 12:00 and 23-12 10:00), Matplotlib simply connects the last data point on 22-12 with the first on 23-12. You can check this by making e.g. a `scatter()` plot. – Bart Dec 31 '22 at 11:17
  • @FObersteiner yeah i'm referring the line, i want to plot without that line in datatime format – Muhammad M Dec 31 '22 at 11:24
  • @Bart indeed there is gap between in those data, that's how the data is, my goal is to plot how the number change in different days with line plot – Muhammad M Dec 31 '22 at 11:25
  • 1
    You could insert NaNs, as described in [matplotlib docs](https://matplotlib.org/stable/gallery/lines_bars_and_markers/masked_demo.html) – FObersteiner Dec 31 '22 at 12:02
  • @FObersteiner in that case, i need to add gap date to datasets with NaN values? – Muhammad M Jan 01 '23 at 06:47
  • I'd probably try to resample the data to a fixed frequency. That will create NaN where you have no data (num column). However, that requires a monotonically increasing datetime index (unique timestamps). Your timestamps seem to be minutes only, so what's the original frequency? – FObersteiner Jan 01 '23 at 11:56
  • related: [Python: Matplotlib avoid plotting gaps](https://stackoverflow.com/q/27266987/10197418) – FObersteiner Jan 01 '23 at 11:57
  • @FObersteiner turns out NaN values only hide the gap and leave the blank space, what i wants to remove the gap and connect the the line like i plotted using date string – Muhammad M Jan 02 '23 at 08:29
  • Ok, thanks for the clarification. You cannot do that with a datetime axis. You must use a categorial axis, i.e. datetime as string. As described in the question linked by Bart. – FObersteiner Jan 02 '23 at 08:48

0 Answers0