Skip to content
Advertisement

is there way to plot graph only for constant data out of mixed data in python?

I am using SIPp tool for sip call that gives me a csv stat file (i configured every 5 sec new row is generated) with columns of current calls (integers) like 0,5,12,25,45,60,60,60,59,60,60,45,32,25,15,5,0.

  1. above is with 5 calls per sec with max call limit 60(pls ignore missing values in between). So, this would be my y-axis values.

  2. For x-axis (this is my another challenge :( .. )as call duration can be anything like 30 seconds to even 2 hours. csv files gives me timestamp in this format “2021-12-14 01:11:41.574282 1639424501.574282“. for which (as per my best I converted all these timestamps to a list of successive values’ diff i.e. 5 using below:

     T=[]
     for t in time:
        tvalue= t.split("t")[2]
        T.append(int(float(tvalue)))
     t_interval = [j-i for i, j in zip(T[:-1], T[1:])]
    

t_interval gives me [5, 5, 5, 5, 5…so on]

If I draw a linear graph, that will be a curved one (start with 0 , flat line , ends with 0 ), I want to draw something like “when first time max call limit reach (i.e. 60) and after my total call duration say 120 seconds (can vary also) when calls start ending i.e last 60 (till this point)” note:- in between sometime there may be values like 58/59. is there a way in metaplot or any python plotting module to do this easily?

with all above said, I am able to plot something that captures all values but not the line (that should be flat for max calls only against time) using below :

calls_tup = tuple(zip(list(calls),list(failedcalls)))
x= np.linspace(0,sum(t_interval),len(calls))
y= np.array(calls_tup)
fig, ax = plt.subplots()
ax.plot(x,y)
  1. Failedcalls is another line to be shown in my graph. that is not an issue. :)

need to have only the flat line from this plot

Advertisement

Answer

Here is an approach that searches for positions of the array with equal successive values. Then a horizontal line of length 1 is drawn from that position.

import matplotlib.pyplot as plt
import numpy as np

values = [0, 5, 12, 25, 45, 60, 60, 60, 59, 60, 60, 45, 32, 25, 15, 5, 0]
values = np.array(values)
pos_equal = np.argwhere(values[:-1] == values[1:]).squeeze()
x = np.vstack([pos_equal, pos_equal + 1, np.full_like(pos_equal, np.nan, dtype=float)]).T.ravel()
y = np.repeat(values[pos_equal], 3)
plt.plot(x, y, ls='-', lw=3, color='r')
plt.plot(values, ls=':', lw=0.5, color='b')
plt.show()

horizontal lines for equal positions

The x and y arrays look like:

x: [ 5.,  6., nan,  6.,  7., nan,  9., 10., nan]
y: [ 60,  60,  60,  60,  60,  60,  60,  60,  60]

The nan values provoke an interruption in the lines.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement