Skip to content Skip to sidebar Skip to footer

Fixed Y Axis In Python Plotting Times In 12 Hr Format

I have this plot but I need the y axis to be fixed to 00:00, 01:00, 02:00, etc all the way up to 12:00. As of now it's only plotting the values I have in the csv on the y axis. the

Solution 1:

I generated a few more rows of data to make the problem, at least on my end, a bit more meaningful.

What solved this for me was generating a 5th column (in code, not the csv) which is the number of minutes corresponding to a particular o'clock time, i.e. 11:59 maps to 719 min. Using pandas I inserted this new column into the dataframe. I could then place string ticklabels for every hour ('0:00', '1:00', etc.) at every 60 min.

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

data = pd.read_csv('Workbook2.csv', header=None)
print data

Prints my faked data:

012340  ML  INT0.10534.150:001  ML  EXT  0.25654.233:002  ML  INT0.30743.126:303  ML  EXT  0.35744.204:304  ML  INT0.45811.477:005  ML  EXT  0.55777.905:456  ML  INT0.66854.707:547  ML  EXT  0.74798.406:558  ML  INT0.87947.3011:59

Now make a function to convert o'clock to minutes:

defconvert_to_min(o_clock):
    h, m = o_clock.split(':')
    returnint(h) * 60 + int(m)
# using this function create a list times in minutes for each time in col 4
min_col = [convert_to_min(t) for t in data[4]]
data[5] = min_col  # inserts this list as a new column '5'print data 

Our new data:

0123450  ML  INT0.10534.150:0001  ML  EXT  0.25654.233:001802  ML  INT0.30743.126:303903  ML  EXT  0.35744.204:302704  ML  INT0.45811.477:004205  ML  EXT  0.55777.905:453456  ML  INT0.66854.707:544747  ML  EXT  0.74798.406:554158  ML  INT0.87947.3011:59719

Now build the x and y axis data, the ticklabels, and the tick locations:

INTs = data[data[1]=='INT']
EXTs = data[data[1]=='EXT']

int_dist = INTs[3]  # x-axis data for INText_dist = EXTs[3]

# plotting time as minutes in range [0 720]int_time = INTs[5]  # y-axis data for INText_time = EXTs[5]

time = ['0:00', '1:00', '2:00', '3:00', '4:00', '5:00', 
        '6:00', '7:00', '8:00', '9:00', '10:00', '11:00', '12:00']
# this will place the strings above at every 60 mintick_location = [t*60 for t in range(13)]

Now plot:

fig, ax = plt.subplots()
ax.scatter(int_dist, int_time, c='orange', s=150)
ax.scatter(ext_dist, ext_time, c='black', s=150)
ax.set_yticks(tick_location)
ax.set_yticklabels(time)
plt.legend(['INT', 'EXT'], loc=4)
plt.xlabel('Distance')
plt.ylabel('Time')
plt.title('Seems to work...')
plt.show()

enter image description here

Solution 2:

The ticks will be a lot smarter if you use a datetime for the y-axis.

Fake data:

df = pd.DataFrame({'value':[530,640,710], 'time':['0:00', '3:00', '6:30']})

   time  value
0  0:00    530
1  3:00    640
2  6:30    710

Convert df.time from str to datetime:

time2 = pd.to_datetime(df.time, format='%H:%M')
plt.plot(df.value, time2, marker='o', linestyle='None')

output

Can't seem to get this into a scatter instead of plot in case it matters for you (I suppressed the line). Maybe because datetime should always be in a timeseries lineplot and never in a scatterplot (I welcome comments that let me know if this is indeed the case and datetime cannot be put into a scatter).

Post a Comment for "Fixed Y Axis In Python Plotting Times In 12 Hr Format"