简体   繁体   中英

Plot a step function using initial and final x values as x axis and V as y axis

I have a txt file set up as follows:

x0              x1              V
0              0.11            1.77
0.11           0.143           1.48
0.143          1               1.35

and I want to make a stepfunction plot where x axis is values from x0 to x1 and y axis is V.

Here is my attempt:

#!/bin/env/python
import numpy as np 
import matplotlib.pyplot as plt 
import csv

x=[]
y=[]

with open('voltage.txt','r') as csvfile:
    points = csv.reader(csvfile, delimiter=',')
    next(points)
    for row in points:
        x.append(float(row[0]))
        y.append(float(row[1]))
allpoints=np.loadtxt('voltage.txt',delimiter=',')
plt.step(x,y)
plt.show()

but it does not generate a step function plot that accounts two x values.

fake file i/o, then ravel and repeat slices for x, y

from io import StringIO   # StringIO behaves like a file object
from matplotlib import pyplot as plt


txt = '''x0              x1              V
0              0.11            1.77
0.11           0.143           1.48
0.143          1               1.35'''

dary = np.loadtxt(StringIO(txt), skiprows=1)

print(dary)

[[0.    0.11  1.77 ]
 [0.11  0.143 1.48 ]
 [0.143 1.    1.35 ]]


plt.step(np.ravel(dary[:,:2]), np.repeat(dary[:,2], 2))

在此处输入图片说明

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