0

I have a data file containing 6 columns and I am going to read it using genfromtxt and store the data in such a way that the first and second column go to variables mCH, tb and the rest (columns 2 to 5) go to BR so that I can plot different BR columns like this:

mCH, tb, BR = np.genfromtxt(BRfile, unpack=True)

for i in range(4):

  CS = axs[i].tricontour(mCH, tb, BR[i], cmap='gnuplot')
...

The BRfile has the following structure:

 150 1 0.2 0.3 0.4 0.1 
 150 2 0.25 0.25 0.4 0.1
 160 1 0.2 0.3 0.45 0.05
 160 2 0.25 0.25 0.45 0.05

But when I run the code, it can not realize that BR is going to be used for 4 columns and gives this error:

ValueError: too many values to unpack (expected 3)

Would be grateful if any help. Thanks, Majid

bumbread
  • 460
  • 2
  • 11

2 Answers2

1

Thanks, I actually got it working in the following style:

data = np.genfromtxt(BRfile,unpack=True)

mCH, tb = data[:2]
BR = data[2:]
0

Using np.genfromtxt, you are trying to unpack all the columns into three variables (mCH, tb, and BR), but there are more than three columns in your data file.

Try this:

import numpy as np

data = np.genfromtxt(BRfile)

# Extract the first 2 columns into mCH and tb
mCH, tb = data[:, :2]

# Extract the 3 remaining columns into BR
BR = data[:, 2:]

for i in range(BR.shape[1]):
    # Access BR[:, i] for each column
    print(f"BR column {i + 1}:")
    print(BR[:, i])
sylvain
  • 853
  • 1
  • 7
  • 20