How to automatically close rasterio raster histogram - python

I want to automatically save some histograms from raster data as images as png and I am using rasterio.
My question is how to save histogram images without needing to show the histogram (because then I have to close the figure manual) or how to close the figure automatically with code.
Thanks.
from rasterio.plot import show_hist
raster='image path'
fig, ax = pyplot.subplots(figsize=(10, 6))
show_hist(raster, bins=50, lw=0.0, stacked=False, alpha=0.3,
histtype='stepfilled', title="Histogram")
fig.savefig(path)

Related

Add svg image to a subplot in matplotlib

So here is the simple code that is producing a subplot with dark background:-
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(6,6), facecolor='black')
ax.set_facecolor('black')
And I want this image which is in .svg format to be added to the subplot. While using PIL's Image.open() and plt.imread I am getting UnidentifiedImageError. What should I do to read in the .svg file and add it to the subplot.

Cannot seem to print a figure I imported from another file into pdf

I produce a plt plot (using Spyder) from a file called fedSentiChart.py as such
fig, ax1 = plt.subplots(figsize=(8, 8))
ax1.plot(Data.index, Data['Value'], color='m', linewidth=1.5)
Then in a separate file, I import this chart as such
from fedSentiChart import fig1
I want to put this chart in a pdf file as such
with PdfPages('/Users/mak/Test.pdf') as pdf:
fig1
pdf.savefig()
plt.close()
But I get an error here:
ValueError: No figure None
Note: It works if I use this method
fig1.savefig('/Users/mak/Test.pdf')
But I want to produce a pdf with multiple pages.
Any idea how I can do this?
I err "figured" it out...
with PdfPages('/Users/mak/Test.pdf') as pdf:
pdf.savefig(fig1)
plt.close()
This works.

How to embed a zoomed portion of a FITS image in the same plot with APLpy

I want to embed a zoomed portion of a FITS image in the same plot with APLpy.
But when loading a FITS file with APLpy, there is only a 'FITSFigure' object returned.
fig = aplpy.FITSFigure('tmp.fits', slices=[0,0])
Is it possible to make it work with zoomed_inset_axes like here , or there are some other solution?
You may specify the figure to which to plot with aplpy. You can then get the axes inside the figure.
fig = plt.figure()
aplpyfig = aplpy.FITSFigure('tmp.fits', figure=fig)
axes = fig.get_axes()
From that point onwards you can work with that axes and use any of the methods that matplotlib offers to obtain insets.
Also see this question: Aplpy multiplot dynamic axis sharing

Reading matplotlib graphs from file

Is it possible to read (say) 4 .jpeg graphs produced by matplotlib into matplotlib again so that they can be replotted as subplots? If so, how would I do it?
If you really want to do it by reading jpeg files of existing plots (noting the comments), one way might be to read in the graphs in with scipy.misc.imread. I've set the axis labels off assuming you saved the original graphs with labels and everything.
import matplotlib.pyplot as plt
from scipy.misc import imread
# Create a figure with 2x2 arranged subplots
fig, ax = plt.subplots(2,2)
# Plot images one by one here
# (Just using the same jpeg file in this example...)
im1 = imread("graph1.jpg")
ax[0,0].imshow(im1)
ax[0,0].axis('off')
ax[0,1].imshow(im1)
ax[0,1].axis('off')
ax[1,0].imshow(im1)
ax[1,0].axis('off')
ax[1,1].imshow(im1)
ax[1,1].axis('off')
fig.show()

Blank image when saving an imshow matplotlib figure in iPython notebook

I'm using iPython notebook w/ matplotlib to display a bunch of images inline, but now it's come time to save a number of these images (think for-loop, i.e. not a small number of images to save). My issue seems to be something to do with how I'm using iPython since I could do this alright when my script was a standalone.
%matplotlib inline
import matplotlib.pyplot as plt
....
grid_z2 = griddata(....)
fig = plt.figure()
ax = fig.add_axes([1,1,1,1])
plt.imshow(grid_z2.transpose(),origin='Lower')
plt.colorbar()
plt.draw()
fig.savefig('slicemap.png')
I have also tried plt.savefig(), fig1 = plt.gcf() before plt.imshow then trying to save fig1... always every single time a blank file.
Any suggestions?

Categories

Resources