0

I am creating an animated heatmap using matplotlib pcolourmesh. I am trying to control the size of each square of the graph area so that the resulting plot corrosponds directly to an image (that the plot is taking data from). Is this possible?

So far I have tried:

plt.rcParams["figure.figsize"] = [img_x/192, img_y/192]

where 192 is my dpi, however, .figsize size includes all of the white space around the graph that contains the title, axis etc. so this is not sutible.

Here is an example of the code plotting a 10 frame animation of 50x40 point heatmap where, because the img_x, img_y is 100x80, I would like each of the heatmap squares to be 2x2 pixels.

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np

img_x = 100
img_y = 80
Z = np.random.rand(10,40,50)

x_axis = np.arange(0, Z.shape[2], 1)
y_axis = np.arange(0, Z.shape[1], 1)
fig, ax = plt.subplots()
cmap = plt.get_cmap('OrRd')
cax = ax.pcolormesh(x_axis, y_axis, Z[0], cmap=cmap)
fig.colorbar(cax)

def animate(i):
    Z_current = Z[i+1]
    cax.set_array(Z_current.flatten())
anim = animation.FuncAnimation(fig, animate, interval=20, frames=len(Z)-1, repeat_delay=1000)

This at the moment is just creating a default sized graph.

Is the size of the graph area even something that is possible to control in the plotting process? Does anyone have a way of controlling this after-the-fact in a clever way if I were to just save each plot individually and stich them together into a video later with something like ImageJ?

Jack Atkinson
  • 43
  • 2
  • 11
  • Does this question help you? https://stackoverflow.com/questions/44970010/axes-class-set-explicitly-size-width-height-of-axes-in-given-units – thmslmr Mar 23 '23 at 13:08

0 Answers0