0

I'm trying to make a "heatmap" with Matplotlib of a 2d Numpy Array and found the NonUniformImage option here. Below is a simple single version. Unfortunately I'm not able to change the x without changing the y. The error that pyhton Propmpts is: Axes don't match array shape

Reproducible Example

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.image import NonUniformImage
from matplotlib import cm

x = np.arange(6)
y = np.arange(6)
z = x[:, np.newaxis].dot(y[:, np.newaxis].transpose())

fig, ax = plt.subplots()
im = NonUniformImage(ax, interpolation='nearest', extent=(0, len(x)-1, 0, len(y)-1))
im.set_data(x, y, z)
ax.images.append(im)
ax.set_xlim(0, len(x)-1)
ax.set_ylim(0, len(y)-1)

My goal is to plot a map based on a Numpy 2d array with 'custom Grid cells'. That means that I can assign x and y with irregular series (e.g. np.array([0.5,3,8,15,15.4]). I know the x and y that are assign represent the center of a cell so the last thing I would like to solve is that the plot is completely visible (if that is possible).

How can I make a image that has a different constraint (not square), with custom width and height depending on two lists?

2
  • When I run your code I get this image. Is this not what you want? Or do you get a different result? Commented May 8, 2019 at 22:55
  • No I got the same result but I can only create images with the same dimensions (e.g. 6x6, 8x8 or 21x21). I would like to make an image of 60 x 29 and have custom height and width depending on to lists. I changed the question to make it more clear. Commented May 9, 2019 at 7:30

1 Answer 1

1

I suppose you made a mistake defining z. Possibly what you're after is

x = np.arange(6)
y = np.arange(8)
z = x[np.newaxis,:] * y[:, np.newaxis]
print(z.shape)

such that the shape is (8,6).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.