Pretty new to python and programming in general so bear with me please. I have a data set imported from a .csv file and I'm trying to plot a column of values (y axis) by date (x axis) over a 1 year period but the problem is that the dates are way too dense and I can't for the life of me figure out how to space them out or modify how they're defined. Here's the code I'm working with:
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib as mpl
from scipy import stats
import cartopy.crs as ccrs
import cartopy.io.img_tiles as cimgt
df = pd.read_csv('Vanuatu Earthquakes 2018-2019.csv')
and here's the line plot code:
plt.figure(figsize=(15, 7))
ax = sns.lineplot(x='date', y='mag', data=df).set_title("Earthquake magnitude May 2018-2019")
plt.xlabel('Date')
plt.ylabel('Magnitude (Mw)')
plt.savefig('EQ mag time')
This currently gives me this line plot:
Currently I want to do it by something like a small tick for each day and a larger tick + label for the beginning of each week. Doesn't have to be exactly that but I'm mostly looking to just decrease the density. I've looked at loads of posts on here but none of them seem to work for my situation so any help would be greatly appreciated.
[Update]
Got the dates working as per Konqui's advice below and my code now looks like this:
time = pd.date_range(start = '01-05-2018',
end = '01-05-2019',
freq = 'D')
df = pd.DataFrame({'date': list(map(lambda x: str(x), time)),
'mag': np.random.random(len(time))})
plt.figure(figsize=(15, 7))
df['date'] = pd.to_datetime(df['date'], format = '%Y-%m')
ax = sns.lineplot(x='date', y='mag', data=df).set_title("Earthquake magnitude May 2018-2019")
ax.xaxis.set_major_locator(md.WeekdayLocator(byweekday = 1))
ax.xaxis.set_major_formatter(md.DateFormatter('%Y-%m-%d'))
plt.setp(ax.xaxis.get_majorticklabels(), rotation = 90)
ax.xaxis.set_minor_locator(md.DayLocator(interval = 1))
plt.xlabel('Date')
plt.ylabel('Magnitude (Mw)')
which gives me an error message: AttributeError: 'Text' object has no attribute 'xaxis'
. Any thoughts?