简体   繁体   中英

Skip specific data lines from a .txt file in order to plot a 2D line with breaks

I am trying to plot a graph with one plot only regarding certain lines of its textfile.

How can I achieve this in my py script?

import pylab as py
xa, ya = py.loadtxt('filename1.txt',skiprows=1,usecols=(0,1),unpack=True ) 
xb, yb = py.loadtxt('filename2.txt',skiprows=1,usecols=(0,1),unpack=True )

fig = py.figure(1,figsize=(12,14))

py.semilogy(xa, ya*2, '-', xb, yb*1.5, 'o', linewidth = 0.5) 

My textfiles have 100 rows (and 2 columns) and I want to skip the lines 35:55 and 65:77 of 'filename2.txt' in order to plot a 2d line with gaps in between. Unfortunately I can't find a solution for this simple task via matplotlib.

If your data is not so big it might be easier to read all and then just select the required indices from your data:

idx = np.arange(xb.shape[0])   # array([0, 1, 2, 3, 4, 5, 6, ...
mask = (idx < 35) | (idx > 55) | (idx < 65) | (idx > 77)
xb, yb = xb[mask], yb[mask]

You can just set the unwanted datapoints in ya and yb to np.nan. Matplot skips (or just doesn't plot) those points.

So you could do (after reading the txt file):

np.put(ya,range(35,56,1),[np.nan]*(56-35))

and so on for the other parts. xa and xb should stay as they are.

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