0

I have a dataframe that looks like this:

df:

    Start     End  Value
0       0   98999      0
1   99000  101999      1
2  102000  155999      0
3  156000  161999      1
4  162000  179999      0

I would like to plot a rectangular wave that goes from "Start" to "End" and that has the value in "Value" Could you please help me? thanks!

My attempt:

    for j in range(0,df.shape[0]):
        plt.figure()
        plt.plot(range(df['Start'].iloc[j],df['End'].iloc[j]), np.ones(df['End'].iloc[j]-df['Start'].iloc[j])*df['Value'].iloc[j])    

but it plots in different figures...

1
  • 1
    It's ploting in different figure because you have the plt.figure() in the loop Commented Jun 23, 2017 at 14:55

3 Answers 3

1

Maybe you can use plt.step to plot a stepwise function:

import pandas as pd
from matplotlib import pyplot as plt

df = pd.DataFrame({'End': [98999, 101999, 155999, 161999, 179999],
 'Start': [0, 99000, 102000, 156000, 162000],
 'Value': [0, 1, 0, 1, 0]})

plt.step(df.Start, df.Value, where='post')

This will plot every Value from its corresponding Start value to the next Start.

step plot

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

Comments

0

A bit slow...

import matplotlib.pyplot as plt 
l = df.apply(lambda x: ( [x.Value for i in range(x.Start,x.End)] ),axis=1).sum()
plt.plot(l)
plt.show()

enter image description here

Comments

0

I believe step function under matplotlib.pyplot is what you are looking for.

You can try out this code:

import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame([[0, 98999, 0],[99000, 101999, 1],[102000, 155999, 0],[156000, 161999, 1],[162000, 179999, 0]],columns=["Start","End","Value"])
plt.step(df["Start"],df["Value"],where="post")
plt.show()

This will end up showing below graph: enter image description here

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.