Lucy Crockford
Lucy Crockford

Reputation: 33

Hour ticks on X axis in Python

Working in Python 3.8.5

Problem: CSV file containing one column with DateTime data (10 minute frequency in the format dd/mm/YYYY HH:MM) and the type of data is called "object". The second column (wave_height) is simple numerical data. I just want to plot the numerical data on the y axis with the x axis showing ticks for hours. At the moment when I make a plot it shows all of the ticks which can't be read.

Action taken so far: I found the literature on major_locator and major_formatter. I've tried numerous things to try to get it to just do an interval of 1 hour for the x ticks but it keeps trying to plot 2613 ticks instead. Webpages I've found with example code to plot hourly ticks just don't seem to work, even if I try their code to see if it's me with my translating!

Example data:

DateTime,Date,Time,Wind_Direction,Effective Fetch,Wind_Speed,wave_height
01/10/2012 00:00,01/10/2012,00:00:00,228,510,1.976,0.0299
01/10/2012 00:10,01/10/2012,00:10:00,231,516,1.389,0.0207
01/10/2012 00:20,01/10/2012,00:20:00,239,532,1.759,0.0269
01/10/2012 00:30,01/10/2012,00:30:00,235,524,2.279,0.0352
01/10/2012 00:40,01/10/2012,00:40:00,238,530,1.983,0.0305
01/10/2012 00:50,01/10/2012,00:50:00,233,520,1.733,0.0262
01/10/2012 01:00,01/10/2012,01:00:00,236,526,2.02,0.031
01/10/2012 01:10,01/10/2012,01:10:00,234,522,2.071,0.0318
01/10/2012 01:20,01/10/2012,01:20:00,236,526,2.296,0.0356
01/10/2012 01:30,01/10/2012,01:30:00,244,542,2.061,0.0322
01/10/2012 01:40,01/10/2012,01:40:00,245,544,1.714,0.0265
01/10/2012 01:50,01/10/2012,01:50:00,236,526,1.74,0.0265
01/10/2012 02:00,01/10/2012,02:00:00,234,522,1.475,0.0222
01/10/2012 02:10,01/10/2012,02:10:00,237,528,0.228,0.0031
01/10/2012 02:20,01/10/2012,02:20:00,237,528,0,0.0
01/10/2012 02:30,01/10/2012,02:30:00,229,512,0.223,0.003
01/10/2012 02:40,01/10/2012,02:40:00,227,510,2.01,0.0304
01/10/2012 02:50,01/10/2012,02:50:00,235,524,1.541,0.0233
01/10/2012 03:00,01/10/2012,03:00:00,231,516,1.685,0.0254
01/10/2012 03:10,01/10/2012,03:10:00,234,522,2.245,0.0346
01/10/2012 03:20,01/10/2012,03:20:00,235,524,2.915,0.0457
01/10/2012 03:30,01/10/2012,03:30:00,229,512,2.971,0.0461
01/10/2012 03:40,01/10/2012,03:40:00,231,516,3.119,0.0488
01/10/2012 03:50,01/10/2012,03:50:00,228,510,2.915,0.0451
01/10/2012 04:00,01/10/2012,04:00:00,229,512,3.357,0.0525

Code so far:

data = pd.read_csv('WaveHeightCalc.csv', sep = ',')

x = data['DateTime']
y = data['wave_height']

import matplotlib.pyplot as plt
import matplotlib.dates as mdates

fig, ax = plt.subplots()
ax.plot(x,y,"r--")
ax.set_xlabel("Date and Time")
ax.set_ylabel("Wave Height (m)")
ax.set_title('Wave Height Over Time')
ax.xaxis.set_major_locator(mdates.HourLocator(byhour= None, interval=1, tz=None))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
ax.legend("Wave Height") #this also just shows the red line and the letter "W"
plt.show()

I'm just at a loss with this, it's just so easy in R as well but I have to use Python. I also tried making my data into strings instead to see if that did anything by using numpy np.array.

Upvotes: 2

Views: 561

Answers (0)

Related Questions