Add svg image to a subplot in matplotlib - python

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.

Related

how to save image in PLT

I'm trying to save my result image using matplotlib.pyplot as the following code which works good without any problem
import matplotlib.pyplot as plt
I = image.astype(np.uint8)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(I)
plt.plot(marker_x, marker_y, color='yellow')# our circle
ax.set_title('Macula')
plt.savefig("J://uni//final project//res_image//marked_M.png")
fig.show()
here is my saved picture plt.savefig("J://uni//final project//res_image//marked_M.png") :
the yellow circle is a plot and as you know when we use plt.plot automatically we have a white space around the main picture (sth like margin as you see in the picture ) and also the axes
and its my 'tkinter' UI:
I'm wondering if is any way to crop this annoying white space or save image in another way that we get rid of this margin?
You can use the matplotlib transparent flag
plt.savefig("J://uni//final project//res_image//marked_M.png", transparent=True)
This will save the image as a png file with a transparent background

How to automatically close rasterio raster histogram

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)

Image plotted with matplotlib not showing colorbar

I have a .fit file. I have read the file, displayed the image with scale. When I want to write this image in .png file, the .png file is displaying the image without scale. I am attaching the code that I have tried.
import pyfits
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
hdulist = pyfits.open('HMI20170425_134641_6173.fits')
image_data = hdulist[0].data
hdulist.close()
fig=plt.imshow(image_data, cmap='gray')
plt.colorbar()
fig.write_png('image.png')
It is showing output image with scale. However, the 'image.png' file showing image without scale.
Please help me in this regard.
I guess what you call the scale is actually the colorbar ? Which indeed is missing when you use fig.write_png because here you are saving only the image part of the plot. You should use plt.savefig instead:
# use astropy instead of pyfits which is no more maintained
import astropy.io.fits as pyfits
import matplotlib.pyplot as plt
%matplotlib inline
image_data = pyfits.getdata('HMI20170425_134641_6173.fits')
plt.imshow(image_data, cmap='gray')
plt.colorbar()
plt.savefig('image.png')

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()

How to save Matplotlib.pyplot.loglog to file?

I am trying to generate the log-log plot of a vector, and save the generated plot to file.
This is what I have tried so far:
import matplotlib.pyplot as plt
...
plt.loglog(deg_distribution,'b-',marker='o')
plt.savefig('LogLog.png')
I am using Jupyter Notebook, in which I get the generated graph as output after statement 2 in the above code, but the saved file is blank.
Notice that pyplot has the concept of the current figure and the current axes. All plotting commands apply to the current axes. So, make sure you plot in the right axes. Here is a WME.
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.loglog(range(100), 'b-',marker='o')
plt.savefig('test.png') # apply to the axes `ax`

Categories

Resources