如何在Matplotlib

时间:2019-02-15 12:53:23

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

我的代码:

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

我当前的输出: Output

数据框 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

我想要的输出: Desired

1 个答案:

答案 0 :(得分:2)

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

函数xmin有两个参数xmaxplt.axhline。 而且他们只能接受0到1之间的浮点数。因此上面有一个normalize函数。

相关问题