Search code examples
pythonpandasresamplingohlc

pandas OHLC plot only high values with time


I have a text file with the following real-time data.

1,16:20:35
2,16:20:40
3,16:21:41
4,16:21:50
5,16:21:52
6,16:22:20
7,16:22:42
8,16:23:44

I want to plot only the high values against time. The following is what I tried.

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

data = pd.read_csv(('output.txt'), header=0, sep=',', names=['count', 'time'])

data['time'] = pd.to_datetime(data['time'], format='%H:%M:%S')

data = data.set_index(['time'])
df2 = data.resample('1T').ohlc().ffill()
print(df2)
fig, ax = plt.subplots()
df2.plot(time="time", high="high value", ax=ax)
plt.draw()

plt.show()

Solution

  • As you already have a DateTime index, You can simply plot it as:

    fig, ax = plt.subplots()
    df2[('count',  'high')].plot(ax=ax)
    

    I would also suggest removing the MultiIndex from df2, as there is only need of one level. i.e.:

    df2.columns = df2.columns.droplevel(0)
    fig, ax = plt.subplots()
    df2['high'].plot(ax=ax)