I'm not sure what's going on here, but when I try to do a scatter plot with a dataframe that has the index set to datetimes, I get a much wider range of dates in the plot for the x-axis. Here's an example:
import matplotlib.pyplot as plt
import pandas as pd
datetimes = ['2020-01-01 01:00:00', '2020-01-01 01:00:05',
'2020-01-01 01:00:10', '2020-01-01 01:00:15',
'2020-01-01 01:00:20', '2020-01-01 01:00:25',
'2020-01-01 01:00:30', '2020-01-01 01:00:35',
'2020-01-01 01:00:40', '2020-01-01 01:00:45']
datetimes = pd.to_datetime(datetimes)
values = [1,2,3,4,5,6,7,8,9,10]
df = pd.DataFrame()
df['values'] = values
df = df.set_index(datetimes)
fig, ax = plt.subplots(figsize=(16,9))
ax.scatter(df.index, df.values)
plt.show()
Yet if I do a plot instead of a scatter
fig, ax = plt.subplots(figsize=(16,9))
ax.plot(df)
plt.show()
I don't understand why the x-axis has a huge date range on the scatter plot which is not included in the datetime range I gave it. It appears to work correctly using plot but not scatter. I'm guessing I'm missing something obvious here but I haven't had any success googling it. Any insight would be greatly appreciated!




xlim.