0

I have a code like below and I want to get a combined array made by multiple pcolormesh. How can I do this? mesh.get_array() does not return what I want (i.e., 25x10 array) but a single 5x5 array.

import matplotlib.pyplot as plt
import numpy as np

def add_randam_data(ax, h, w):
    D = np.random.uniform(0, 100, size=(5, 5))
    mesh = ax.pcolormesh(np.arange(D.shape[0]+1)+w*D.shape[0], np.arange(D.shape[1]+1)+h*D.shape[1], D)
    return mesh

fig, ax = plt.subplots(1,1, figsize=(10, 5))
for ih in range(0,2):
    for iw in range(0,5):
        mesh = add_randam_data(ax,ih,iw)

plt.show()

## Here I want to get this 25x10 array to be saved, but how?
wjandrea
  • 28,235
  • 9
  • 60
  • 81
kilometer
  • 1
  • 1
  • You need to save the figure before doing`plt.show()`. Does this answer your question? [Saving a figure after invoking pyplot.show() results in an empty file](https://stackoverflow.com/q/21875356/7758804) – Trenton McKinney Aug 24 '21 at 18:40
  • inside your nested loops over `ih` and `iw`, you are overwriting `mesh` each iteration, so when you come to look at the end, you will only see the final one. You could create an empty 25x10 array before the loops (e.g. using `np.zeros`), then just assign the array returned in the `mesh` from `add_random_data` to the relevant part of your larger array inside the loops. – tmdavison Aug 24 '21 at 21:23
  • Trenton: Thanks for the comment, but what I want is to access the array at the end of the code. – kilometer Aug 25 '21 at 04:12
  • tmdavison: Thanks for the comment. Actually the code was just an example, and in practice the single array size could be different every time. So I want to access the combined image after completing all iterations, without setting a size for the combined array. Yea I see that mesh was overwitten each iteration. Is it possible to access the combined array from ax, not from mesh?? – kilometer Aug 25 '21 at 04:23

0 Answers0