How do i store plot data as numpy ndarray..

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How do i store plot data as numpy ndarray..

noflaco
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:


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  

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: How do i store plot data as numpy ndarray..

noflaco
I made this tiny example code for reproducing the output:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from PIL import Image
import librosa
import librosa.display
from matplotlib import cm


fig = plt.figure(figsize=(12,4))
min = -1.828067
max = 22.70058
data =  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 <[hidden email]>:

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
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: How do i store plot data as numpy ndarray..

vincent.adrien@gmail.com
Hi,

I think the problem is that you do not actually retrieve the plotted array data
with `matplotlib.pyplot.get_cmap` but the `matplotlib.colormap.Colormap`
instance instead (i.e. just the color <-> value mapping).

Please see the following code snippet to achieve something that looks like you
want if I understood correctly your wish.

```python

import matplotlib.pyplot as plt
import numpy as np

original_data = np.arange(600).reshape((200, -1))
im_opts = {'aspect': 'auto', 'cmap': 'viridis'}

fig, (ax0, ax1) = plt.subplots(ncols=2)

ax0.set_title('From Original Data')
im0 = ax0.imshow(original_data, **im_opts)

# NB: ``get_array`` returns a masked array from `numpy.ma`.  More information:
# https://docs.scipy.org/doc/numpy/reference/maskedarray.generic.html
masked_data = im0.get_array()
np.save('recorded_array.npy', masked_data.data)  # NB: use the mask if needed
reloaded_data = np.load('recorded_array.npy')

ax1.set_title('From Recorded Data')
im1 = ax1.imshow(reloaded_data, **im_opts)

# just to be sure :)
np.testing.assert_allclose(original_data, reloaded_data)

plt.show()

```

Best regards,
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 <[hidden email]
>> <mailto:[hidden email]>>:
>>
>> 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
> [hidden email]
> https://mail.python.org/mailman/listinfo/matplotlib-users
>

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: How do i store plot data as numpy ndarray..

vincent.adrien@gmail.com
In reply to this post by noflaco
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 <[hidden email]
>> <mailto:[hidden email]>>:
>>
>> 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
> [hidden email]
> https://mail.python.org/mailman/listinfo/matplotlib-users
>

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users