简体   繁体   English

绘制矩形波python

[英]plot rectangular wave python

I have a dataframe that looks like this: 我有一个看起来像这样的数据框:

df: 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... 但它以不同的数字绘制...

Maybe you can use plt.step to plot a stepwise function: 也许您可以使用plt.step绘制逐步函数:

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 . 这会将每个Value从其对应的Start值绘制到下一个Start

阶梯图

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

在此处输入图片说明

I believe step function under matplotlib.pyplot is what you are looking for. 我相信您正在寻找matplotlib.pyplot下的step函数。

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: 最终将显示如下图: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM