[Matplotlib-users] How do i store plot data as numpy ndarray..

vincent.adrien at gmail.com vincent.adrien at gmail.com
Mon May 15 03:16:46 EDT 2017


I am not an expert in Numpy masked array, but if I am correct, you can access 
the data array of a masked array `m_arr` with no masking through the 
`m_arr.data` attribute, the boolean mask through the `m_arr.mask` one and the 
masked array through something like `m_arr[~m_arr.mask]`. This way, you can 
retrieve the data that is actually displayed by imshow, can't you?

If you absolutely need to save the mask too with Numpy, I would suggest to use 
`numpy.savez` (note the ending z) to record both the data array and the boolean 
mask.  With similar notations as in the previous code snippet, this may looks like:

```python

masked_data = im0.get_array()

np.savez('full_data.npz', data=masked_data.data, mask=masked_data.mask)
with np.load('test.npz') as f:
     reloaded_data = f['data']
     reloaded_mask = f['mask']
reloaded_ma = np.ma.MaskedArray(reloaded_data, mask=reloaded_mask)

ax1.imshow(reloaded_ma, **im_opts)

```

Best,
Adrien V.

Le 13/05/2017 à 13:52, Carlton Banks a écrit :
> I made this tiny example code for reproducing the output:
> 
> |importnumpy asnp importmatplotlib.pyplot asplt importmatplotlib fromPIL 
> importImageimportlibrosa importlibrosa.display frommatplotlib importcm fig 
> =plt.figure(figsize=(12,4))min =-1.828067max =22.70058data 
> =np.random.uniform(low=min,high=max,size=(474,40))librosa.display.specshow(data.T,sr=16000,x_axis='frames',y_axis='mel',hop_length=160,cmap=cm.jet)plt.show()raw_input("sadas")convert 
> =plt.get_cmap(cm.jet)numpy_output_static 
> =convert(data.T)plt.imshow(numpy_output_static,aspect 
> ='auto')plt.show()raw_input("asds")|
> 
> 
> Which gives two different images, a random plot in first one, and a red plot in 
> the second one.
>> Den 13. maj 2017 kl. 11.50 skrev Carlton Banks <noflaco at gmail.com 
>> <mailto:noflaco at gmail.com>>:
>>
>> Hi guys..
>>
>> I seem to have a problems with converting my plot to a numpy ndarray -
>> Such that if I made a imshow of the numpy ndarray i would see am image similar 
>> to the lmage i created..
>>
>> |librosa.display.specshow(static.T,sr=16000,x_axis='frames',y_axis='mel',hop_length=160,cmap=cm.jet)plt.title("log 
>> mel power spectrum of "+name)plt.colorbar(format='%+02.0f 
>> dB')plt.tight_layout()plt.savefig(plot+"/"+name+"_plot_static_conv.png")plt.show()|
>> This will show an image such as:
>> <JPR6G.png>
>>
>>
>> But if i try to store it as a numpy ndarray and then trying to plot it using 
>> this code
>> |convert =plt.get_cmap(cm.jet)numpy_output_static 
>> =convert(static.T)plt.imshow(numpy_output_static)plt.show()raw_input("sadas")|
>> I see something like this <2WGgJ.png>
> 
> 
> 
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users
> 



More information about the Matplotlib-users mailing list