Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Column xyz data to grid for plotting

I have a very large amount of data that needs plotting which is stored in 3 columns as xyz data. I need to convert the column data into a grid so I can easily plot it with contourf in matplotlib and I was wondering whether there was a function to do this as the code I've written myself is very slow?

i.e.

x y z

1 1 10

1 2 12

2 1 14

2 2 16

to a grid like this:

10 12

14 16
like image 511
user1185379 Avatar asked Jun 16 '26 00:06

user1185379


1 Answers

numpy is kind of smart with this. You could just read the columns in separate arrays and do:

import numpy

idx1 = numpy.array([0, 0, 1, 1])
idx2 = numpy.array([0, 1, 0, 1])
data = numpy.array([10, 12, 14, 16])

grid = numpy.zeros(len(data)/2, 2)
grid[idx1, idx2] = data

>>>grid
array([[ 10.,  12.],
      [ 14.,  16.]])

Keep in mind that indexing starts from 0 so if yours starts from 1 you need to decrement 1 from each element.

like image 84
Bogdan Avatar answered Jun 18 '26 13:06

Bogdan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!