Chris B
Chris B

Reputation: 81

Extra lines in plot when using matplotlib.plot

I'm trying to make a program that will create a descending series of plots based on given files containing an n*2 matrix of numerical values (they more or less share an x-axis, and they're close enough on the y-axis that they need to be manipulated to avoid overlap).

Right now, the way it works is to read in the files one at a time with fileinput, add a constant to the values in column two (arbitrary so long as the constant splits each plot; I do it by multiplying the number of files by two, and decrementing by two each plot so they get split), then add the manipulated values to two master lists (for x and y), which are plotted at the end by matplotlib.

I have it doing very close to exactly what I want, but it has some odd lines connecting the end of one file to the beginning of the next, and I'd like to know how to remove them.

Here's the relevant part of the code:

mpl.suptitle(spectitle, fontsize=16)
mpl.xlabel('wavelength (A)', fontsize=14)
mpl.ylabel('flux (erg s^-1 cm^-2)', fontsize=14)

with open(filelist) as infile:
    allfiles = [line.rstrip('\n') for line in open(filelist)]

multiplier = len(allfiles)
multiplier *= 2

for line in fileinput.input(allfiles):
    filename = fileinput.filename()
    waveN, fluxN = np.loadtxt(filename, usecols=[0,1], unpack=True)
    fluxCalc = np.array(fluxN)
    fluxCalc += multiplier
    multiplier -= 2 #decrease multiplier, causing next output specturm to be placed below the one just calculated
    wavelenAll.extend(waveN)
    fluxCalc.tolist()
    fluxAll.extend(fluxCalc)
    fileinput.nextfile()

mpl.plot(wavelenAll, fluxAll)
mpl.savefig('allspec.png')
mpl.show()

I can add an image of the output in a few hours. Thanks for any help in advance.

Upvotes: 1

Views: 1878

Answers (2)

tacaswell
tacaswell

Reputation: 87376

Try something like:

import matplotlib.pyplot as plt
import numpy as np

filelist = []
spectitle = 'spectrum'

with open(filelist) as infile:
    allfiles = [line.rstrip('\n') for line in open(filelist)]

all_flux, all_wavelen = [], []

# just get the data from the file and accumulate in a list
# which assumes you want these lists for something else
for fname in allfiles:
    waveN, fluxN = np.loadtxt(fname, usecols=[0, 1], unpack=True)
    all_flux.append(fluxN)
    all_wavelen.append(waveN)


fig, ax = plt.subplots()

fig.suptitle(spectitle, fontsize=16)
ax.set_xlabel('wavelength (A)', fontsize=14)
ax.set_ylabel('flux (erg s^-1 cm^-2)', fontsize=14)
# loop over the data and plot
for wv, flux, shift in zip(all_wavelen, all_flux,
                           range(1, len(allfiles) + 1)[::-1]):
    # do the shift as late as possible so you do not accidentally reuse
    # cosmetically shifted data for computing something
    ax.plot(wv, flux + shift, color='b')

fig.savefig('allspec.png')
plt.show()

Upvotes: 2

Radek Hofman
Radek Hofman

Reputation: 527

It must be in the data or caused by its wrong postprocessing. It is hard to say more unless we see the data. Try to plot it without first of last elements, i.e. like mpl.plot(wavelenAll[1:-1], fluxAll[1:-1])

Upvotes: 0

Related Questions