Histogram image not getting saved - python

import numpy as np
import matplotlib.pyplot as plt
x=np.random.gamma( 2, 3, 100000)
plt.hist( x, bins=30)
plt.show()
plt.savefig("normalhistogram.png")
The above code is working perfectly for plotting histograms for gama distribution values, but the only problem is that I want to save the image of generated histogram but plt.savefig("normalhistogram.png") is creating a blank image everytime I execute the code instead of saving the histogram as image. I'm unable to figure out the issue here. Help?

Do not show() the image before saving it. Showing the image clears the canvas. (But saving does not, so you can show the image after saving it.)

Related

matplotlib save plot instead of displaying

I am using python on a linux shell and trying to save plot instead of displaying (displaying plot window leads to an error). I looked at question Save plot to image file instead of displaying it using Matplotlib, but didn't help. Here is my code:
import matplotlib.pyplot as plt
#
# list3 is list of data
plt.hist(list3, bins=10)
plt.xlabel('X')
plt.ylabel('Y')
fig.savefig('plot.png')
The problem is figure window is appearing even though I don't call plt.figure(). Is there any way to suppress graphical figure window and instead save plot to the file?
plt.savefig('plot.png') saves the png file for me. May be you need to give full path for the file

Plotting spectrogram but returned weird results

I already have some audio converted to spectrogram image using matplotlib.pyplot.specgram function.
After some operations on the image, now I would like to save it as an image file.
My simple thought was that since the data are already numpy arrays, I could just plot them.
The code was:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import imageio
im = imageio.imread(path/0.png')
plt.plot(im)
plt.show()
However, it returns very weird image as shown.
The image looks weird and obviously is not recognized as spectrograms
The original image was greyscale with shape (256, 128), how come the returned image has different colors?
What should I do now? Is it the wrong parameters setting that caused the results?
similar to the demonstration in https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.spectrogram.html, you should be using the plt.pcolormesh function instead of the plt.plot. Currently, you are plotting each of the spectrogram's lines separately.

Draw matplotlib plot to PNG in Jupyter when inline matplotlib is enabled

I am drawing a confusion matrix heatmap in Jupyter using code similar to the example here using imshow
Matplotlib is set to draw plots inline.
This works fine for outputting to the cell in the notebook, but I want to not output to the cell but instead get PNG data (ideally raw, not saved to a file) in this case only, not in general (in general I want matplotlib to display inline).
I'm not quite sure how to do that; examples I've seen seem to be global in nature (e.g. calling matplotlib.use() before importing pyplot).
Is this possible? How?
A simple way to not display the plot inline, is to use plt.close() at the end of the cell.
import matplotlib.pyplot as plt
%matplotlib inline
plt.plot([1,2,3],[1,2,3])
plt.savefig("image.png")
plt.close()
Turn off interactive mode:
plt.ioff()
To reactivate inline images, use
plt.ion()
%matplotlib inline
To save the PNG image as bytes, but not to a file, pass a file-like io.BytesIO object to plt.savefig instead of a file:
import io
data = io.BytesIO()
plt.savefig(data)

Reading png does not give expected result

import numpy as np
import matplotlib.pyplot as plt
from scipy import misc
img = misc.imread('W0002_0001.png')
plt.figure()
plt.imshow(img)
plt.show()
I want to read the first image that can be found here. The problem is that the image displayed is not the one I should see. I get:
When I should be getting:
When looking in detail, I noticed that the real image could be seen in the first row of the obtained image, repeated several times.
Also, I had several segfaults when trying to import the image several times in a row with IPython, although it seems like I cannot always reproduce the issue.
I tried reading the image with Pillow and it worked fine.
Do you have any idea what might cause that?

matplotlib: filter to produce a antialiased image

I have a low resolution image that I want to scale up and display in a figure. As you might expect, the image looks a bit pixelated, so I would like to do some filtering on it. Matplotlib has filternorm and filterrad as filtering options in plt.imshow(), but they don't seem to do anything. The best I have been able to do is change the interpolation method to bicubic. I would like something where I can smoothly adjust the degree of filtering. Does anyone have any suggestions on how to do this using Matplotlib? I imagine I could import PIL or something from Scipy, but I'd like to avoid that.
Here is the raw image at it's native resolution (340x240)
Here is the matplotlib code
import matplotlib.pyplot as plt
im = plt.imread('path/on/my/computer')
im_obj = plt.imshow(im, interpolation = 'bicubic', resample = True)
plt.savefig('im_bicubic.png')
and here is the resulting png

Categories