Reputation: 9844
I'm trying to plot a image with imshow but I am getting outputs that I didn't expected... The method to show my image is:
def generate_data_black_and_white_heat_map(data, x_axis_label, y_axis_label, plot_title, file_path):
plt.figure()
plt.title(plot_title)
plt.imshow(data.data, extent=[0, data.cols, data.rows, 0], cmap='Greys')
plt.xlabel(x_axis_label)
plt.ylabel(y_axis_label)
plt.savefig(file_path + '.png')
plt.close()
My data is represented as:
def __init__(self, open_image=False):
"""
The Data constructor
"""
self.data = misc.lena() / 255.0
x, y = self.data.shape
self.rows = x
self.cols = y
I do some calculation and at some point I have to do this:
# A -> 2D ndarray
A.data[A.data >= 0.5] = 1.0
A.data[A.data < 0.5] = 0.0
Which gives me:
But I want the opposite (white background). So, I just did this:
# A -> 2D ndarray
A.data[A.data >= 0.5] = 0.0
A.data[A.data < 0.5] = 1.0
And then, I got this (!!!):
I just didn't get it. This makes any sense for me. And the weird thing is 'cause if I do:
for x in range(A.cols):
for y in range(A.rows):
if A.data[x][y] >= 0.5:
A.data[x][y] = 0.0
else:
A.data[x][y] = 1.0
It works. Am I accessing something in the wrong way?
Any help to clarify this in my mind would be very appreciated.
Thank you in advance.
Upvotes: 1
Views: 4291
Reputation: 87556
It is doing exactly what you are telling it to do:
A[A >= 0.5] = 0.0 # all of you values are now < 0.5
A[A < 0.5] = 1.0 # all of your values are now 1
It is far better to just do
B = A > .5 # true (1) where above thershold
iB = B < .5 # true (1) where below threshold
Upvotes: 1