Plotting a heat map from three lists: X, Y, Intensity - python

I don't get how to create a heatmap (or contour plot) when I have x, y, intensity. I have a file which looks like this:
0,1,6
0,2,10
....
So far:
with open('eye_.txt', 'r') as f:
for line in f:
for word in line.split():
l = word.strip().split(',')
x.append(l[0])
y.append(l[1])
z.append(l[2])
Tried using pcolormesh but it wants a shape object and I'm unsure how to convert these lists into a NumPy array.
I tried:
i,j = np.meshgrid(x,y)
arr = np.array(z)
plt.pcolormesh(i,j,arr)
plt.show()
It tells me that:
IndexError: too many indices
Can someone stop me from bashing my head against a keyboard, please?

OK, there's a few steps to this.
First, a much simpler way to read your data file is with numpy.genfromtxt. You can set the delimiter to be a comma with the delimiter argument.
Next, we want to make a 2D mesh of x and y, so we need to just store the unique values from those to arrays to feed to numpy.meshgrid.
Finally, we can use the length of those two arrays to reshape our z array.
(NOTE: This method assumes you have a regular grid, with an x, y and z for every point on the grid).
For example:
import matplotlib.pyplot as plt
import numpy as np
data = np.genfromtxt('eye_.txt',delimiter=',')
x=data[:,0]
y=data[:,1]
z=data[:,2]
## Equivalently, we could do that all in one line with:
# x,y,z = np.genfromtxt('eye_.txt', delimiter=',', usecols=(0,1,2))
x=np.unique(x)
y=np.unique(y)
X,Y = np.meshgrid(x,y)
Z=z.reshape(len(y),len(x))
plt.pcolormesh(X,Y,Z)
plt.show()

In case you don't have a regular grid (i.e. a value of z for each meshgrid value of x and y), you can use a more general method based on pandas data frames:
import pandas as pd
import seaborn as sns
import matplotlib.pypot as plt
data = pd.DataFrame(data={'x':x, 'y':y, 'z':z})
data = data.pivot(index='x', columns='y', values='z')
sns.heatmap(data)
plt.show()
The pivot method will use unique values from index and columns to construct a table with missing measurments set to NaN. The table can then be plotted as a heatmap.

The index error arises from the fact that pcolormesh expects a 2D array while your arr is a 1D vector. Also if I understand correctly, your input file has the form
0,1,z
0,2,z
...
0,ymax,z
...
1,1,z
1,2,z
...
xmax,ymax,z
In that case meshgrid(x,y) will not work as it expects something like meshgrid(range(xmax),range(ymax)) i.e. vectors without repeated values.
In your case you need to find out how many distinct x and y values there are and then simply reshape your vectors into 2D arrays.
shape = np.unique(x).shape[0],np.unique(y).shape[0]
x_arr = x.reshape(shape)
y_arr = y.reshape(shape)
z_arr = z.reshape(shape)
plt.pcolormesh(x_arr,y_arr,z_arr)

To convert a list into a numpy array you can use np.asarray.
Here's an easy way to get a heatmap, you should be able to adapt this example to your problem.
import matplotlib.pyplot as plt
import numpy as np
a = [[1,2,3], [3,4,5], [5,6,7], [7, 8, 9]]
b = np.asarray(a)
plt.pcolor(b)
plt.show()
To read the data in like a list of lists you can do:
a = []
for line in file.readlines():
a.append( map( int, line.split(',') ) )
in short. In a longer version it's the equivalent of:
a = []
for line in file.readlines():
tmp = line.split(',')
inttmp = [int(x) for x in a]
a.append(inttmp)

Related

Zipping coordinates and then accessing them

I have a three vectors of x,y,z coordinates, something like
x = [1,2,3,4,5]
y = [6,7,8,9,10]
z = [11,12,13,14,15]
I want to zip them to have an 5x3 array of coordinates. But when I do
coords = zip(x,y,z)
and then try to print coords, I get
and I cannot plot them either,
How can I get 5x3 array?
You can use numpy function directly:
np.array(list(zip(x,y,z)))
If you didn't import numpy:
import numpy as np
call list on the zip object or use it in a list comprehension.

numpy ndarray object has no attribute append

I am struggling with a program I am making on the part where I have to store values that I get from my loop in an array.
What I tried to do is to make an empty array called M. Then for every new value "a" calculated in a loop I use the command M.append(a) to add the value a to the array M.
The thing is, python says this error : 'numpy.ndarray' object has no attribute 'append'
and I don't know how to fix it.
Here is my code :
import numpy as np
from matplotlib import pyplot as plt
with open('expFcn0.txt') as f:
M = np.array([])
print(M)
lines = f.readlines()
x = [float(line.split()[0]) for line in lines]
y = [float(line.split()[1]) for line in lines]
for i in range(0,181):
a=np.log(y[i])/x[i]
print(a)
i=i+1
M.append(a)
print(M)
plt.plot(x, y, 'r--')
plt.xlabel('Time')
plt.ylabel('Biomass')
plt.title('Exponential Function')
plt.show()
Thank you very much!
Numpy arrays don't have a method append(). You need to use np.append(array, values) as per the documentation, or for your case, np.append(M, a).
Other answers explain that numpy arrays do not have an .append() method and point to numpy.append. Using numpy.append, however, is bad practice because it creates a new array each time. A better solution is to create one numpy and fill it during the for loop (see end of answer).
An even better solution would make use of numpy's broadcasting. That's a core feature of numpy, and it's what helps make numpy fast.
import numpy as np
with open('expFcn0.txt') as f:
lines = f.readlines()
x = np.array([float(line.split()[0]) for line in lines])
y = np.array([float(line.split()[1]) for line in lines])
M = np.log(y) / x
You can also look into numpy.loadtxt to read the file into a numpy array directly.
How to fill a numpy array in a for loop:
import numpy as np
with open('expFcn0.txt') as f:
lines = f.readlines()
x = [float(line.split()[0]) for line in lines]
y = [float(line.split()[1]) for line in lines]
M = np.zeros(181)
for i in range(181):
a = np.log(y[i])/x[i]
print(a)
M[i] = a
Numpy arrays do not have an append method. Use the Numpy append function instead:
M = np.append(M, a)

Plot 3rd axis of a 3D numpy array

I have a 3D numpy array that is a stack of 2D (m,n) images at certain timestamps, t. So my array is of shape (t, m, n). I want to plot the value of one of the pixels as a function of time.
e.g.:
import numpy as np
import matplotlib.pyplot as plt
data_cube = []
for i in xrange(10):
a = np.random(100,100)
data_cube.append(a)
So my (t, m, n) now has shape (10,100,100). Say I wanted a 1D plot the value of index [12][12] at each of the 10 steps I would do:
plt.plot(data_cube[:][12][12])
plt.show()
But I'm getting index out of range errors. I thought I might have my indices mixed up, but every plot I generate seems to be in the 'wrong' axis, i.e. across one of the 2D arrays, but instead I want it 'through' the vertical stack. Thanks in advance!
Here is the solution: Since you are already using numpy, convert you final list to an array and just use slicing. The problem in your case was two-fold:
First: Your final data_cube was not an array. For a list, you will have to iterate over the values
Second: Slicing was incorrect.
import numpy as np
import matplotlib.pyplot as plt
data_cube = []
for i in range(10):
a = np.random.rand(100,100)
data_cube.append(a)
data_cube = np.array(data_cube) # Added this step
plt.plot(data_cube[:,12,12]) # Modified the slicing
Output
A less verbose version that avoids iteration:
data_cube = np.random.rand(10, 100,100)
plt.plot(data_cube[:,12,12])

Representing row vector as a line plot in matplotlib

Let's say I have a row vector with the shape (1, 100). Call it row.
I want to display its values in a line plot. The x-axis displays the vector indices. The y-axis displays the values at the corresponding indices. How would you do it in matplotlib?
EDIT:
Here is what I have tried:
indices = [n for n in range(100)]
values = list(row[:, :100])
pyplot.plot(indices, values)
The array indices is not necessary.
The values array syntax is unclear... and an error in python, unless you are using numpy.
The following will plot an array of random values, with indices as x and the random numbers stored in values as y
import matplotlib.pyplot as plt
import random
# indices = [n for n in range(100)]
values = [random.random() for _ in range(100)]
plt.plot(values)
plt.show()
import matplotlib.pyplot as plt
indices = range(100) # already returns a list, no need to iterate again
values = # insert 1d vector here, what you seem to use is multi-dim
plt.plot(indices, values)
plt.show()

Interpolate each row in matrix of x values

I want to interpolate between values in each row of a matrix (x-values) given a fixed vector of y-values. I am using python and essentially I need something like scipy.interpolate.interp1d but with x values being a matrix input. I implemented this by looping, but I want to make the operation as fast as possible.
Edit
Below is an example of a code of what I am doing right now, note that my matrix has more rows on order of millions:
import numpy as np
x = np.linspace(0,1,100).reshape(10,10)
results = np.zeros(10)
for i in range(10):
results[i] = np.interp(0.1,x[i],range(10))
As #Joe Kington suggested you can use map_coordinates:
import scipy.ndimage as nd
# your data - make sure is float/double
X = np.arange(100).reshape(10,10).astype(float)
# the points where you want to interpolate each row
y = np.random.rand(10) * (X.shape[1]-1)
# the rows at which you want the data interpolated -- all rows
r = np.arange(X.shape[0])
result = nd.map_coordinates(X, [r, y], order=1, mode='nearest')
The above, for the following y:
array([ 8.00091648, 0.46124587, 7.03994936, 1.26307275, 1.51068952,
5.2981205 , 7.43509764, 7.15198457, 5.43442468, 0.79034372])
Note, each value indicates the position in which the value is going to be interpolated for each row.
Gives the following result:
array([ 8.00091648, 10.46124587, 27.03994936, 31.26307275,
41.51068952, 55.2981205 , 67.43509764, 77.15198457,
85.43442468, 90.79034372])
which makes sense considering the nature of the aranged data, and the columns (y) at which it is interpolated.

Categories