megan_17
megan_17

Reputation: 3

How to plot all points from a file (lack of first point in plot)?

I have problem with plotting data from file, and more precisely from first row. The data from the firts row are always skiped in the plot and I don't know why this sytuation occurs. Do anybody know how to resolve this problem?

I enclose the code:

import numpy as np
import matplotlib.pyplot as plt
fname1 = 'data.txt'
data1 = np.genfromtxt(fname1, names=True)
cols1 = data1.dtype.names
ax1 = plt.subplot(111)
ax1.grid(False)
plt.errorbar(data1[cols1[0]], data1[cols1[1]], yerr=[data1[cols1[2]],
data1[cols1[3]]], fmt='o', markersize=5, color='blue')
plt.setp(ax1.get_xticklabels(), visible=True)
plt.ylabel("PI")
plt.xlabel("MJD")
plt.savefig("test.png")
#plt.subplots_adjust(wspace=0, hspace=0)
plt.show()`

enter image description here

Upvotes: 0

Views: 41

Answers (2)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339280

If the first row of your data.txt contains column names, the solution from the question should work. Since it apparently doesn't, I assume that you don't have column names in you data file.

If you don't have column names in your data file, don't use names=True. Remove this argument. Then it's clear that you cannot use column names to index your array. So instead of data1[columnname] you need to index the array the usual way of providing row and column numbers.

plt.errorbar(data1[:,0], data1[:,1], yerr=[data1[:,2],data1[:,3]] )

Upvotes: 0

akshaynagpal
akshaynagpal

Reputation: 3147

From Docs:

If names is True, the field names are read from the first valid line after the first skip_header lines. If names is a sequence or a single-string of comma-separated names, the names will be used to define the field names in a structured dtype. If names is None, the names of the dtype fields will be used, if any.

I think data1 = np.genfromtxt(fname1, names=False) will solve the issue.

Upvotes: 2

Related Questions