how to plot two-dimension array in python?
16,294
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()
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.
Related videos on Youtube
Author by
윤진수
Updated on June 04, 2022Comments
-
윤진수 over 1 year
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?
-
Adam Hughes over 8 yearsI don't think he want's a 2d histogram. Plotting a 2d array is done through
imshow