[Matplotlib-users] How do i store plot data as numpy ndarray..
vincent.adrien at gmail.com
vincent.adrien at gmail.com
Mon May 15 02:33:41 EDT 2017
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 <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