简体   繁体   中英

Plot values on a increasing x-axis

I'm working on python where I'm monitoring delay flows between two hosts. My program creates a file the contains two rows of information on column1 are different time interval for when I received the value in column2, example:

2.0     -0.430053710938         
4.0     -0.0391845703125
1.0      5.830078125
4.0      5.07067871094

It took 2 seconds before I received the value -0.430053710938, 4 seconds later I got -0.0391845703125, a second later the value 5.830078125 and so on.

How can I plot this so it would make sense?, I tried look into gnuplot, but it creates column1 as x-axes which messes up everything since my my 3rd value as 1.0.

What you need is a cumulative sum ( np.cumsum ) of the time (first column) after reading from the file. Below is a complete working answer. I am reading the data from the file and then converting the time list to an array followed by taking a cumulative sum for the time.

import matplotlib.pyplot as plt
import numpy as np

with open('data.dat',"r") as file:
    lines = file.readlines()

x = np.cumsum(np.array([float(row.split()[0]) for row in lines]))
y = [float(row.split()[1]) for row in lines]

plt.plot(x, y, '-kx')
plt.show()

Alternative way to load data

data = np.loadtxt('data.dat', usecols=(0,1))
x = np.cumsum(data[:,0])
y = data[:,1]
plt.plot(x, y, '-kx')
plt.show()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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