【问题标题】:How to restrict Horizontal Lines with Timeframe Range in Matplotlib如何在 Matplotlib 中使用时间范围限制水平线
【发布时间】:2019-02-15 12:53:22
【问题描述】:

我的代码:

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

lot_size = 3750
min_vol = 60
path = 'C:\\Data\\ONGC19FEBFUT.txt'
df = pd.read_csv(path, sep=",")
df.columns = ['Date','Time','Price','volume']
df['Volume'] = np.where((df.volume/lot_size) < min_vol, 0, (df.volume/lot_size))
df["Time"] = pd.to_datetime(df['Time'])

df.plot(x="Time",y='Price', rot=0, color='g')

plt.title("Date: " + str(df['Date'].iloc[0]))

dff = df[df.Volume > min_vol].reset_index(drop=True)
dff = dff[['Time','Price','Volume']]
print(dff)

dict = dff.to_dict('index')
for x in range(0, len(dict)):
    plt.axhline(y=dict[x]['Price'],linewidth=1, color='blue')

plt.subplots_adjust(left=0.05, bottom=0.06, right=0.95, top=0.96, wspace=None, hspace=None)
plt.show()

我当前的输出:

Dataframe dff 给出要在图表上绘制的价格值。我想将要绘制的价格值分隔为每 30 分钟持续时间,即要在时间范围内绘制 09:00 到 09:30 的价格值,要在此时间范围内绘制 09:30 到 10:00 的价格值等等。 我想限制每 30 分钟时间范围内的水平价格线。

dff 输出:

                 Time  Price    Volume
0 2019-02-15 09:15:02  132.90   111.0
1 2019-02-15 09:15:03  134.15    78.0
2 2019-02-15 11:14:46  132.65    68.0
3 2019-02-15 11:27:24  131.95    73.0
4 2019-02-15 12:40:36  129.50   176.0
5 2019-02-15 13:42:52  129.90    75.0
6 2019-02-15 13:52:26  130.05    71.0
7 2019-02-15 13:52:40  129.70    99.0

我想要的输出:

【问题讨论】:

    标签: python python-3.x pandas dataframe matplotlib


    【解决方案1】:
    import matplotlib.pyplot as plt
    import pandas as pd
    import numpy as np
    from datetime import datetime
    
    lot_size = 3750
    min_vol = 60
    path = 'ONGC19FEBFUT.txt'
    df = pd.read_csv(path, sep=",")
    df.columns = ['Date','Time','Price','volume']
    df['Volume'] = np.where((df.volume/lot_size) < min_vol, 0, (df.volume/lot_size))
    df["Time"] = pd.to_datetime(df['Time'])
    
    pic = df.plot(x="Time",y='Price', rot=0, color='g')
    pic.margins(0.0)
    
    plt.title("Date: " + str(df['Date'].iloc[0]))
    
    dff = df[df.Volume > min_vol].reset_index(drop=True)
    dff = dff[['Time','Price','Volume']]
    print(dff)
    
    dict = dff.to_dict('index')
    
    # get the 30-min interval in which x resides
    def get_interval(x):
        y, m, d = x.year, x.month, x.day
        if x.minute < 30:
            hours = (x.hour, x.hour)
            minute = (0,30)
        else:
            hours = (x.hour, x.hour+1)
            minute = (30,0)
        return datetime(y, m, d, hours[0], minute[0], 0), datetime(y, m, d, hours[1], minute[1], 0)
    
    start = df["Time"][0]
    end = df["Time"][df["Time"].size-1]
    
    # get the position of x in x-axis
    def normalize(x):
        return (x-start)/(end-start)
    
    for x in range(0, len(dict)):
        interval = get_interval(dict[x]["Time"])
        xmin, xmax = list(map(normalize, interval))
        plt.axhline(y=dict[x]['Price'], xmin=xmin, xmax=xmax, linewidth=1, color='blue')
    
    plt.subplots_adjust(left=0.05, bottom=0.06, right=0.95, top=0.96, wspace=None, hspace=None)
    plt.show()
    

    函数plt.axhline 有两个参数xminxmax。 而且他们只能接受0到1之间的浮点数。所以上面有个normalize函数。

    【讨论】:

    • 感谢先生的精彩解释。我想将音量值写在水平线上方。有没有可能..?
    • 先生 5 2019-02-15 12:40:36 129.50 176.0 行的时间间隔显示错误 (datetime.datetime(2019, 2, 15 , 12, 30), datetime.datetime(2019, 2, 15, 12, 0))。它应该是 13 (datetime.datetime(2019, 2, 15, 12, 30), datetime.datetime(2019, 2, 15, 13, 0))
    • 函数def normalize(x): return (x-start)/(end-start) & xmin, xmax = list(map(normalize, interval))返回时间框架的正确位置。就像我想绘制在 09:00 到 09:30、09:30 到 10:00、10:00 到 10:30 之间精确绘制的线。
    • 那是因为熊猫给图片添加了边距,所以线条看起来向左移动了。
    猜你喜欢
    • 2016-01-27
    • 2023-02-22
    • 2018-10-29
    • 2021-01-03
    • 2017-08-02
    • 1970-01-01
    • 2015-03-06
    • 2021-04-25
    相关资源
    最近更新 更多