MDDawid1
MDDawid1

Reputation: 112

plt.imshow() shows only one color

I am trying to plot a heatmap from a 2000x2000 NumPy array. I have tried every solution from this post and many others. I have tried many cmaps and interpolation combinations. This is the code that prepares the data:

def parse_cords(cord: float):
    cord = str(cord).split(".")
    h_map[int(cord[0])][int(cord[1])] += 1

df["coordinate"] is a pandas series of floats x,y coordinate. x and y are ranging from 0 to 1999.

I have decided to modify the array so that values will range from 0 to 1, but I have tested the code also without changing the range.

h_map = np.zeros((2000, 2000), dtype='int')
cords = df["coordinate"].map(lambda cord: parse_cords(cord))
maximum = float(np.max(h_map))
precent = lambda x: x/maximum
h_map = precent(h_map)

h_map looks like this:

[[0.58396242 0.08840799 0.03153833 ... 0.00285187 0.00419393 0.06324442]
 [0.09075658 0.11172622 0.01476262 ... 0.00134206 0.00687804 0.0082201 ]
 [0.02986076 0.01862104 0.03959067 ... 0.00100654 0.00134206 0.00251636]
 ...
 [0.00301963 0.00134206 0.00134206 ... 0.00100654 0.00150981 0.00553598]
 [0.00419393 0.00268411 0.00100654 ... 0.00201309 0.00402617 0.01342057]
 [0.05183694 0.00251636 0.00184533 ... 0.00301963 0.00838785 0.1016608 ]]

Now the plot:

fig, ax = plt.subplots(figsize=figsize)
ax = plt.imshow(h_map)

And result: final plot The result is always a heatmap with only a single color depending on the cmap used. Is my array just too big to be plotted like this or am I doing something wrong?

EDIT: I have added plt.colorbar() and removed scaling from 0 to 1. The plot knows the range of data (0 to 5500) but assumes that every value is equal to 0. new_plot

Upvotes: 2

Views: 1536

Answers (1)

Soerendip
Soerendip

Reputation: 9184

I think that is because you only provide one color channel. Therefore, plt.imshow() interprets the data as black and white image. You could either add more channels or use a different function e.g. sns.heatmap().

from seaborn import sns

Upvotes: 1

Related Questions