1

I want to plot two graphs out of one pandas dataframe(a), by overlapping them.

This is part of the dataframe(a).

                       Temperature
DateTime
2017-05-01 07:20:00    49.15
2017-05-01 07:19:00    49.14
2017-05-01 07:18:00    49.15
2017-05-01 07:17:00    49.14
2017-05-01 07:16:00    49.14
2017-05-01 07:15:00    49.15
2017-05-01 07:14:00    49.15
2017-05-01 07:13:00    49.15
2017-05-01 07:12:00    49.16
2017-05-01 07:11:00    49.17
2017-05-01 07:10:00    49.18
2017-05-01 07:09:00    49.16
2017-05-01 07:08:00    49.15
2017-05-01 07:07:00    49.15
2017-05-01 07:06:00    49.16
2017-05-01 07:05:00    49.19
2017-05-01 07:04:00    49.19
2017-05-01 07:03:00    49.20
2017-05-01 07:02:00    49.21
2017-05-01 07:01:00    49.15

Firstly, I want to plot this graph as a timeseries. Secondly, I want to plot points that meet a certain condition. For example, I want to plot each teperature that is the highest value within the range of +- 5 rows. The below shows a function that I made for this condition.

def HIGH(a, span):
    for m in range(span, len(a)-span):
        temp_df = a.iloc[m-span:m+span]

        if a.iloc[m] == pd.DataFrame.max(temp_df, axis=0):
            print(a.index.values[m])
            print(a.iloc[m])

>>> HIGH(a, 5)
2017-05-01T07:10:00.000000000
49.18

>>> matplotlib.pyplot.plot(a)
>>> matplotlib.pyplot.show()

shows the graph below, but how can I mark the point(with 'rx' option)?

enter image description here

1 Answer 1

2

You need to return the values from the HIGH function and plot them with a marker.

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


u = u"""DateTime;Temperature
2017-05-01 07:20:00;49.15
2017-05-01 07:19:00;49.14
2017-05-01 07:18:00;49.15
2017-05-01 07:17:00;49.14
2017-05-01 07:16:00;49.14
2017-05-01 07:15:00;49.15
2017-05-01 07:14:00;49.15
2017-05-01 07:13:00;49.15
2017-05-01 07:12:00;49.16
2017-05-01 07:11:00;49.17
2017-05-01 07:10:00;49.18
2017-05-01 07:09:00;49.16
2017-05-01 07:08:00;49.15
2017-05-01 07:07:00;49.15
2017-05-01 07:06:00;49.16
2017-05-01 07:05:00;49.19
2017-05-01 07:04:00;49.19
2017-05-01 07:03:00;49.20
2017-05-01 07:02:00;49.21
2017-05-01 07:01:00;49.15"""

data = io.StringIO(u)
df = pd.read_csv(data, sep=";", index_col=0)
df.index = pd.to_datetime(df.index)


def HIGH(a, span):
    x,y = [],[]
    for m in range(span, len(a)-span):
        temp_df = a.iloc[m-span:m+span]
        cur = float(pd.DataFrame.max(temp_df, axis=0))
        if float(a.iloc[m]) == cur:
            x.append( a.index.values[m] )
            y.append( float(a.iloc[m]) )
    return x,y

plt.plot(df)
x,y = HIGH(df,5)
plt.plot(x,y, marker="*", color="crimson", ls="", ms=15)
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.