在Python中以12小时格式固定了y轴绘制时间 [英] Fixed y axis in Python plotting times in 12 hr format

查看:57
本文介绍了在Python中以12小时格式固定了y轴绘制时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个图,但我需要将 y 轴固定为 00:00、01:00、02:00 等,一直到 12:00.截至目前,它仅在 y 轴上绘制我在 csv 中的值.csv的格式如下.如何使y轴保持恒定,并且仅以1小时的增量显示00:00到12:00,并且仍然正确绘制了数据?

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 csv is in the following format. How do o get the y axis to be constant and only show 00:00 to 12:00 in 1 hr increments and still have the 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 the following is the code I have so far.

    import pandas as pd
    import matplotlib.pyplot as plt
    import numpy as 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 for t in data[4]]
    int_dist = [d for d in ints[3]]
    ext_dist = [d for 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 a few more rows of data to make the problem, at least on my end, a bit more meaningful.

为我解决的问题是生成第五列(代码,而不是csv),该列是对应于特定时间的分钟数,即11:59映射为719分钟.使用 Pandas,我将这个新列插入到数据框中.然后,我可以每60分钟为每小时("0:00","1:00"等)放置字符串ticklabel.

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

打印我的伪造数据:

    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 a function to convert o'clock to minutes:

def convert_to_min(o_clock):
    h, m = o_clock.split(':')
    return int(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 

我们的新数据:

    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

现在构建x和y轴数据,刻度标签和刻度位置:

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 INT
ext_dist = EXTs[3]

# plotting time as minutes in range [0 720]
int_time = INTs[5]  # y-axis data for 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']
# this will place the strings above at every 60 min
tick_location = [t*60 for t in range(13)]

现在绘制:

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()

这篇关于在Python中以12小时格式固定了y轴绘制时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆