2

I have dataset with

data = [[1,2,3], [4,5,6], [7,8,9]].

and call

plot(data)
plot.show()

then y-axis is treated as inner array's value.

what I want is

f(0,0) = 1, f(0,1) = 2, f(1,2) = 3, 
f(1,0) = 4, f(1,1) = 5, f(1,2) = 6,
f(2,0) = 7, f(2,1) = 8, f(2,2) = 9

The question is, how to change y-axis to array's index instead of array's value?

2 Answers 2

6

A few things:

1) Python does not have the 2D, f[i,j], index notation, but to get that you can use numpy. Picking a arbitrary index pair from your example:

import numpy as np
f = np.array(data)
print f[1,2]
# 6
print data[1][2] 
# 6

2) Then for the plot you can do:

plt.imshow(f, interpolation="nearest", origin="upper")
plt.colorbar()
plt.show()

enter image description here

So here you have representative colors where you have the numbers in your f array.

Here I specified origin="upper". Usually people want the (0,0) point at the bottom for a data array (as opposed to an image), but you write out your array with (0,0) in the upper left, which is what "upper" does. This is also the default, btw, but it's explicit use might make clear that there's an option.

-1

What module do you use for plotting? Matplotlib?

What you want is a 2d Histogram. There are various implementations. I recommend the one which comes with numpy http://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram2d.html

1
  • I don't think he want's a 2d histogram. Plotting a 2d array is done through imshow Commented Mar 28, 2015 at 1:08

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.