Fixed y axis in Python plotting times in 12 hr format -
i have plot need y axis fixed 00:00, 01:00, 02:00, etc way 12:00. of it's plotting values have in csv on y axis. csv in following format. how o y axis constant , show 00:00 12:00 in 1 hr increments , still have data plotted correctly?
ml int 0.1 534.15 0:00 ml ext 0.25 654.23 3:00 ml int 0.35 743.12 6:30
and following code have far.
import pandas pd import matplotlib.pyplot plt import numpy np data = pd.read_csv('data.csv', header=none) ints = data[data[1]=='int'] exts = data[data[1]=='ext'] int_index = data[data[1]=='int'].index ext_index = data[data[1]=='ext'].index time = [t t in data[4]] int_dist = [d d in ints[3]] ext_dist = [d d in exts[3]] fig, ax = plt.subplots() ax.scatter(int_dist, int_index, c='orange', s=150) ax.scatter(ext_dist, ext_index, c='black', s=150) ax.set_yticks(np.arange(len(data[4]))) ax.set_yticklabels(time) plt.legend(['int', 'ext'], loc=4) plt.xlabel('distance') plt.ylabel('time') plt.show()
i generated few more rows of data make problem, @ least on end, bit more meaningful.
what solved me generating 5th column (in code, not csv) number of minutes corresponding particular o'clock time, i.e. 11:59 maps 719 min. using pandas inserted new column dataframe. place string ticklabels every hour ('0:00', '1:00', etc.) @ every 60 min.
import pandas pd import matplotlib.pyplot plt import numpy np data = pd.read_csv('workbook2.csv', header=none) print data
prints faked data:
0 1 2 3 4 0 ml int 0.10 534.15 0:00 1 ml ext 0.25 654.23 3:00 2 ml int 0.30 743.12 6:30 3 ml ext 0.35 744.20 4:30 4 ml int 0.45 811.47 7:00 5 ml ext 0.55 777.90 5:45 6 ml int 0.66 854.70 7:54 7 ml ext 0.74 798.40 6:55 8 ml int 0.87 947.30 11:59
now make function convert o'clock minutes:
def convert_to_min(o_clock): h, m = o_clock.split(':') return int(h) * 60 + int(m) # using function create list times in minutes each time in col 4 min_col = [convert_to_min(t) t in data[4]] data[5] = min_col # inserts list new column '5' print data
our new data:
0 1 2 3 4 5 0 ml int 0.10 534.15 0:00 0 1 ml ext 0.25 654.23 3:00 180 2 ml int 0.30 743.12 6:30 390 3 ml ext 0.35 744.20 4:30 270 4 ml int 0.45 811.47 7:00 420 5 ml ext 0.55 777.90 5:45 345 6 ml int 0.66 854.70 7:54 474 7 ml ext 0.74 798.40 6:55 415 8 ml int 0.87 947.30 11:59 719
now build x , y axis data, ticklabels, , tick locations:
ints = data[data[1]=='int'] exts = data[data[1]=='ext'] int_dist = ints[3] # x-axis data int ext_dist = exts[3] # plotting time minutes in range [0 720] int_time = ints[5] # y-axis data int ext_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'] # place strings above @ every 60 min tick_location = [t*60 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 work...') plt.show()
Comments
Post a Comment