how to save image in PLT - python

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

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.

Reading a 4 band image with rasterio

I am trying to view a tif satellite image which has 4 bands. I want to remove the last band (NIR) and view the RGB image only, so I am trying to split the NIR from the rest of the image. Here is my code
import rasterio
from rasterio.plot import show
from matplotlib import pyplot as plt
from rasterio import plot
import numpy as np
#to display RGB
dataset = rasterio.open('2.tif')
%matplotlib inline
plot.show(dataset.read([1,2,3]), cmap="gray")
#to display just the red band
%matplotlib inline
plot.show(dataset.read(4), cmap="gray")
I provided a screen shot of the code and the output I am getting
Displaying just 1 band seems fine, but any idea why I keep seeing an image with a yellow and white color scheme when I try to display RGB bands together? I thought it's a cmap issue at the beginning, but even when I add 'cmap="gray"' the color of the image remains the same.

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

putting some text to a python plot

I'm trying to do a correlation plot using python, so I'm starting with this basic example
import numpy as np
import matplotlib.pyplot as plt
image=np.random.rand(10,10)
plt.imshow(image)
plt.colorbar()
plt.show()
ok, this script give to me an image like this
so the next step is to put my dataset and not a random matrix, i know it, but I want to put some axis or text in this plot, and to get something like this image
It is a very pretty image using paint (lol), but someone can say me what way I need to follow to do something like thik please (how to search it in google).
Before to post it I think in labels, but also I think that I can assign only one label to each axis
cheers
As #tcaswell said in the comments, the function you want to use is annotate, and the documentation can be found here.
I've given an example below using your code above:
import numpy as np
import matplotlib.pyplot as plt
def annotate_axes(x1,y1,x2,y2,x3,y3,text):
ax.annotate('', xy=(x1, y1),xytext=(x2,y2), #draws an arrow from one set of coordinates to the other
arrowprops=dict(arrowstyle='<->'), #sets style of arrow
annotation_clip=False) #This enables the arrow to be outside of the plot
ax.annotate(text,xy=(0,0),xytext=(x3,y3), #Adds another annotation for the text
annotation_clip=False)
fig, ax = plt.subplots()
image=np.random.rand(10,10)
plt.imshow(image)
plt.colorbar()
#annotate x-axis
annotate_axes(-0.5,10,4.5,10,2.5,10.5,'A') # changing these changes the position of the arrow and the text
annotate_axes(5,10,9.5,10,7.5,10.5,'B')
#annotate y-axis
annotate_axes(-1,0,-1,4,-1.5,2,'A')
annotate_axes(-1,4.5,-1,9.5,-1.5,7.5,'B')
plt.show()
This give the image shown below:

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