I lose my image from a subplot when I shift the image.
(The code is run in Jupyter Lab):
from mpl_toolkits.axes_grid1 import host_subplot
from mpl_toolkits import axisartist
hostImage = host_subplot(221, axes_class=axisartist.Axes)
from matplotlib.offsetbox import TextArea, DrawingArea, OffsetImage, AnnotationBbox
import matplotlib.image as mpimg
test_image = mpimg.imread('testImage.png')
imagebox = OffsetImage(test_image, zoom=1)
ab = AnnotationBbox(imagebox, (-0.0014, 0), box_alignment=(1, 0))
hostImage.add_artist(ab)
The image can still be seen with the above configuration.
Next, when I change parameters the image vanishes:
Shifting the image to the left changing line 7
ab = AnnotationBbox(imagebox, (-0.0025, 0), box_alignment=(1, 0))
to
ab = AnnotationBbox(imagebox, (-0.5, 0), box_alignment=(1, 0))
Changing the matrix layout of the subplots changing line
hostImage = host_subplot(221, axes_class=axisartist.Axes)
to
hostImage = host_subplot(111, axes_class=axisartist.Axes)
-> How can I show everything I add to a subplot (more or less) regardless how far off it may be from the axes 'central part' (the area spanned by the two axes, 'axes' in the sense of a plot)?
Using the plt.tight_layout() method did not help.
Here is the test image I used (the red rhomboid).
%%%%%%%%%%%
To make it clearer what I really want to achieve (practical background of the question):
I have line plots showing measurement data of about 30 sensors which are positioned in the real world in a rather geometrically complex 3D measurement setup. The position of the sensors is essential for anybody trying to understand the chart. So the image serves as a kind of 3D legend for the chart. In a single plot I show data of about 5-6 sensors (more sensors in single chart would make it unreadable).
See this real example (work in progress where I stopped to post my question):
image of the real case
This example I established by creating a second subplot below the subplot with the curves. This second suplot has hidden axes (in the sense of plural of axis). It already is a workable solution and my current baseline.
By the way, for this reason I want the image to be rather below the plot in order not to 'waste' horizontal space for the chart where I plot curves.
So the '3D image legend' is integral part of the finally exported 'all-in-one' plot (.png)
The .pngs go into my written report which is my ultimate goal.
In the report I could also add each image corresponding to a plot by hand, but having all info (plot and image) included in one-in-all matplotlib figures makes it more convenient to establish the report and also less error-prone (pairing wrong images and plots, since I have many sensors and many configurations thus creating quite a number of such plots).
What triggered my question beyond my above solution already established:
I want to finally place labels (matplotlib annotations) as 'overlay' on the image with the sensor names on top of the image.
And then connect these labels via arrow lines with the corresponding curves of the plot. This would make it very clear and convenient to the reader to understand which plot curve corresponds to which sensor position in the image -> kind of '3D legend'.
I had found ConnectionPatch as a solution for drawing lines between subplots but I got an error message which I ultimately did not want to try to resolve but choose the approach:
Have the image as part of the very same subplot of the curves because connecting labels within a subplot is easy (actually you can see in the image I uploaded already such sensor name labels placed along the right y-axis).
Why do I use host_subplot?
I have up to five y-axes in my plot (I am aware that this high number of y-axis may be questionable but it is please not what I want to discuss in this post) and I understood having more than 2 additional y-axis is possible only with host_subplot using .twinx().
P.S.:
After all I think I should for now lower my high expectations and stick with my workable solution of two subplots and just renounce on the possibility of connecting labels in the second subplot with curves in the first subplot.
Matplotlib 3.5 (or presumably better)
If you are using Matplotlib 3.5 (or presumably better), this works for what you want, I think (or close):
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as axisartist
hostImage = host_subplot(221, axes_class=axisartist.Axes)
from matplotlib.offsetbox import TextArea, DrawingArea, OffsetImage, AnnotationBbox
import matplotlib.image as mpimg
test_image = mpimg.imread('testImage.png')
imagebox = OffsetImage(test_image, zoom=1)
ab = AnnotationBbox(imagebox, (-0.0025, 0), box_alignment=(1, 0))
hostImage.add_artist(ab)
hostImage.figure.subplots_adjust(left=0.69) # based on https://matplotlib.org/stable/tutorials/intermediate/tight_layout_guide.html saying how to manually adjust
hostImage.figure.set_size_inches((18, 10)) # from https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/figure.py; also see drevicko's comment https://stackoverflow.com/a/638443/8508004
hostImage.figure.savefig("my_image_test.png") # fix for `hostImage.savefig("my_image_test.png")`, based on https://forum.freecodecamp.org/t/attribute-error-axessubplot-object-has-no-attribute-savefig/46025
This will show the same view of the produced plot in both the direct in JupyterLab output and in the image file produced. (The actual size will probably be slightly different, with the image file displaying better resolution.) **If you don't want to produce an image file, then you can remove the last two lines and just include the adjustment **,figure.subplots_adjust(left=0.69) , to account for the Annotation box being added.
I put pertinent sources in the comments for each line.
My test image was wide and short so you may need to adjust figure.subplots_adjust(left=0.69) to what works for you. (Now I don't like that I had to stumble around trying very high and low versions of the left value for figure.subplots_adjust(), and then hone in on a just-right setting but it worked. I will say that usually I set the figure size before making the subplots, such as here, and maybe doing it that way makes it seem less experimenting is necessary to get it working. But the fact the manual adjustment is mentioned in discussion of tight_layout in Matplotlib's documentation, in regards to elements going outside the figure area, makes me think it happens that you need to do some adjusting now and then.)
Here I use hostImage.figure.set_size_inches((18, 10)). Maybe you don't need yours as wide?
Code for checking Matplotlib version:
import matplotlib
print (matplotlib.__version__ )
Matplotlib versions prior to 3.5 (or maybe specifically 3.2.1?)
The code above wasn't working with Matplotlib 3.2.1 with all else the same. (In launches of Jupyter sessions served via MyBinder from here before running %pip install matplotlib --upgrade in a cell and restarting the kernel.) The image produced was good but the output directly in the Jupyter notebook was cutoff and only showing a fragment.
This code block below works for what you want, I think (or close), if using Matplotlib 3.2.1. Since I couldn't get the direct output in the Jupyter cell where I was using Matplotplib 3.2.1 to display correctly, this just displays the plot from the associated image file produced.
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as axisartist
hostImage = host_subplot(221, axes_class=axisartist.Axes)
from matplotlib.offsetbox import TextArea, DrawingArea, OffsetImage, AnnotationBbox
import matplotlib.image as mpimg
test_image = mpimg.imread('testImage.png')
imagebox = OffsetImage(test_image, zoom=1)
ab = AnnotationBbox(imagebox, (-0.0025, 0), box_alignment=(1, 0))
hostImage.add_artist(ab)
hostImage.figure.subplots_adjust(left=0.69) # based on https://matplotlib.org/stable/tutorials/intermediate/tight_layout_guide.html saying how to manually adjust
hostImage.figure.set_size_inches((18, 10)) # from https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/figure.py; also see drevicko's comment https://stackoverflow.com/a/638443/8508004
hostImage.figure.savefig("my_image_test.png") # fix for `hostImage.savefig("my_image_test.png")`, based on https://forum.freecodecamp.org/t/attribute-error-axessubplot-object-has-no-attribute-savefig/460255
hostImage.figure.clf() # using this so, Jupyter won't display the Matplotlib plot object; instead we'll show the image file
from IPython.display import Image
Image(filename="my_image_test.png")
How things are working for the shared lines I added is covered above.
Optionally when using Matplotlib 3.2.1 with code like here, to not also show the matplotlib cruft, such as something like <Figure size 1296x720 with 0 Axes>, you can split running this between two cells.
First cell's code:
%%capture
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as axisartist
hostImage = host_subplot(221, axes_class=axisartist.Axes)
from matplotlib.offsetbox import TextArea, DrawingArea, OffsetImage, AnnotationBbox
import matplotlib.image as mpimg
test_image = mpimg.imread('testImage.png')
imagebox = OffsetImage(test_image, zoom=1)
ab = AnnotationBbox(imagebox, (-0.0025, 0), box_alignment=(1, 0))
hostImage.add_artist(ab)
hostImage.figure.subplots_adjust(left=0.69) # based on https://matplotlib.org/stable/tutorials/intermediate/tight_layout_guide.html saying how to manually adjust
hostImage.figure.set_size_inches((18, 10)) # from https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/figure.py; also see drevicko's comment https://stackoverflow.com/a/638443/8508004
hostImage.figure.savefig("my_image_test.png") # fix for `hostImage.savefig("my_image_test.png")`, based on https://forum.freecodecamp.org/t/attribute-error-axessubplot-object-has-no-attribute-savefig/460255
hostImage.figure.clf() # using this so, Jupyter won't display the Matplotlib plot object; instead we'll show the image file
Second cell's code:
from IPython.display import Image
Image(filename="my_image_test.png")
The first cell will show no output of any kind now due to the %%capture cell magic.
UPDATE:
(code below only tested with Matplotlib 3.5.)
Some options based on addition of sample figure OP is using and additional information in comment here, I suggest starting over with simpler subplot use for arranging the two elements. (If it was much more complex, I'd suggest other methods for compositing the two elements. Options would include: If just for presenting in Jupyter, ipywidgets can be used for layout. Pillow and ReportLab can be useful if making a publication-quality figure is the goal.)
!curl -o testImage.png https://owncloud.tuwien.ac.at/index.php/s/3caJsb2PcwN7HdU/download
#based on https://matplotlib.org/stable/gallery/subplots_axes_and_figures/subplots_demo.html
# and https://www.moonbooks.org/Articles/How-to-insert-an-image-a-picture-or-a-photo-in-a-matplotlib-figure/
# and https://nbviewer.org/gist/fomightez/4c2116e50f080b1305c41b9ac70df124#Solution
# axis off for lower plot based on https://stackoverflow.com/a/10035974/8508004
import matplotlib.pyplot as plt
from matplotlib.offsetbox import TextArea, DrawingArea, OffsetImage, AnnotationBbox
import matplotlib.image as mpimg
fig, axs = plt.subplots(2,1,figsize=(4, 8))
#fig.suptitle('Vertically stacked subplots')
axs[0].grid()
axs[1].grid()
test_image = mpimg.imread('testImage.png')
imagebox = OffsetImage(test_image, zoom=1)
ab = AnnotationBbox(imagebox, (0.5,0.5))
axs[1].add_artist(ab)
axs[1].axis('off');
Or:
!curl -o testImage.png https://owncloud.tuwien.ac.at/index.php/s/3caJsb2PcwN7HdU/download
#based on https://matplotlib.org/stable/gallery/subplots_axes_and_figures/subplots_demo.html
# and https://www.moonbooks.org/Articles/How-to-insert-an-image-a-picture-or-a-photo-in-a-matplotlib-figure/
# and https://nbviewer.org/gist/fomightez/4c2116e50f080b1305c41b9ac70df124#Solution
# axis turned off for lower plot based on https://stackoverflow.com/a/10035974/8508004
import matplotlib.pyplot as plt
from matplotlib.offsetbox import TextArea, DrawingArea, OffsetImage, AnnotationBbox
import matplotlib.image as mpimg
# data to plot based on https://stackoverflow.com/a/17996099/8508004 and converting it
# to work with subplot method
fig, axs = plt.subplots(2,1)
plt.subplots_adjust(hspace=1.8) # to move the bottom plot down some so not covering the top small one
#fig.suptitle('Vertically stacked subplots')
axs[0].plot(range(15))
axs[0].set_xlim(-7, 7)
axs[0].set_ylim(-7, 7)
axs[0].set_aspect('equal')
axs[1].grid()
test_image = mpimg.imread('testImage.png')
imagebox = OffsetImage(test_image, zoom=1)
ab = AnnotationBbox(imagebox, (0.5,0.5))
axs[1].add_artist(ab)
axs[1].axis('off');
Or if want to save the figure something like:
!curl -o testImage.png https://owncloud.tuwien.ac.at/index.php/s/3caJsb2PcwN7HdU/download
#based on https://matplotlib.org/stable/gallery/subplots_axes_and_figures/subplots_demo.html
# and https://www.moonbooks.org/Articles/How-to-insert-an-image-a-picture-or-a-photo-in-a-matplotlib-figure/
# and https://nbviewer.org/gist/fomightez/4c2116e50f080b1305c41b9ac70df124#Solution
# axis turned off for lower plot based on https://stackoverflow.com/a/10035974/8508004
import matplotlib.pyplot as plt
from matplotlib.offsetbox import TextArea, DrawingArea, OffsetImage, AnnotationBbox
import matplotlib.image as mpimg
# data to plot based on https://stackoverflow.com/a/17996099/8508004 and converting it
# to work with subplot method
fig, axs = plt.subplots(2,1)
plt.subplots_adjust(hspace=0.3) # to move the bottom plot down some so not covering the top small one
#fig.suptitle('Vertically stacked subplots')
axs[0].plot(range(15))
axs[0].set_xlim(-7, 7)
axs[0].set_ylim(-7, 7)
axs[0].set_aspect('equal')
axs[1].grid()
test_image = mpimg.imread('testImage.png')
imagebox = OffsetImage(test_image, zoom=1)
ab = AnnotationBbox(imagebox, (0.5,0.5))
axs[1].add_artist(ab)
axs[1].axis('off')
# to accomodate this adjustment in the figure that gets saved via `plt.savefig()`, increase figure size
fig.set_size_inches((4, 7)) # from https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/figure.py; also see drevicko's comment
plt.savefig("stacked.png");
I'm not sure while the size changes on the top plot if you set the size so you can accomodate them but there's some honing on the right numbers needed there.
Edit on 2022-09-28:
I have found a solution for my case by browsing the help/py-code of matplotlib.offsetbox.AnnotationBbox:
The desired effect can be achieved by modifying the argument xybox of AnnotationBbox like so, for example
ab = AnnotationBbox(imagebox, xy = (1, 0), xybox = (2.0, 1.0), box_alignment=(1, 0))
Setting xybox = (2.0, 1.0), hence the x-value to 2.0 shifts the image far to the right of the plot area.
When I make matpoltlib images, I provide Astropy WCS object as the projection. This make makes grid lines as shown below.
However, when I make images for the entire visible horizon I get something like the below
The problem in the above image is that, since the grid lines do not intersect the figure axis, the grid values do not get plotted. I was wondering if there is a way to show the grid values inside the figure (something like what gets done in a mollweide projection)
EDIT 1
The below is my code
import matplotlib.pyplot as plt
from astropy.io import fits
from astropy.wcs import WCS
hdu = fits.open("image.fits")
wcs = WCS(hdu[0].header,naxis=2)
plt.subplot(111,projection=wcs)
plt.imshow(hdu[0].data[0,0,:,:],origin="lower")
plt.grid()
plt.show()
I'm trying to show a figure, in PyCharm, that is in my working directory, but it either doesn't work or only works with this code:
img = mpimg.imread('Figure_1.png')
plt.imshow(img)
plt.show()
with this result:
I just want the picture as it is, not inside another figure. This is the original picture for reference:
Some quick workarounds: to remove the axes, do plt.axes('off'). To make the picture fit the frame, set the aspect ratio to 'auto' and create a figure with the same aspect ratio as your original image (i'd say it's roughly 4:1?). Use tight_layoutto make sure all of your image is visible. I don't know if that's the official way, but that's how i do it, and it kinda works ;-)
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img = mpimg.imread('Figure_1.png')
f, a = plt.subplots(figsize=(16, 4))
plt.tight_layout()
plt.axis('off')
a.imshow(img)
plt.show()
I'm trying to use astropy 2.0.11 with python 2.7.15 to edit a fits image by applying a log stretch to it and change the contrast, and I have't been able to figure it out.
I've been trying to follow the tutorials on the astropy website for opening and manipulating fits files, but I'm wondering if the tutorials will only work for the latest version of astropy and on python 3?
Sorry about the organization of my code. This is prototype code and I'm just trying to test a few things and get this to work.
import time
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import astropy.visualization
from astropy.io import fits
from astropy.utils.data import download_file
from astropy.visualization import astropy_mpl_style
plt.style.use(astropy_mpl_style)
from astropy.utils.data import get_pkg_data_filename
def main():
#My own fits file
#fitsImage = get_pkg_data_filename("C:\\20180807T000456.fits")
fitsImage = download_file('http://data.astropy.org/tutorials/FITS-images/HorseHead.fits', cache=True )
hdu_list = fits.open(fitsImage)
hdu_list.info()
#norm = ImageNormalize(stretch=LogStretch())
image_data = fits.getdata(fitsImage)
print(type(image_data))
print(image_data.shape)
hdu_list.close()
plt.figure()
plt.imshow(image_data, cmap='gray', norm=LogNorm())
plt.colorbar()
# I chose the tick marks based on the histogram above
cbar = plt.colorbar(ticks=[5.e3,1.e4,2.e4])
cbar.ax.set_yticklabels(['5,000','10,000','20,000'])
time.sleep(10)
I am also unable to get the image to display with the plt.imshow()
Any insight would be helpful
You're so close! I ran your code in Python 2.7 and all you need to do is add
plt.show()
before time.sleep(10) (any reason you're including this?) and you get
Also, I don't think you need to include the colorbar and yticklabels, plt.imshow automatically adds the colorbar with the lognorm scale (I commented that section out when I got the image).
I have a FITS file of 4545x4545 pixels with a header containing its coordinate system. Since DS9 (another software to view and handle FITS images) handles the color map scaling better, I had the idea of:
open the FITS file using DS9 to tweak the color map of the image,
save this image in a PNG file,
load this PNG file in matplotlib and add the header from the original FITS file, so I can add the coordinate system to the PNG file.
But the coordinates are not showing correctly, because the pixelization changes to different values in every step. How can I do this correctly?
This the relevant part of my code:
from astropy.io import fits
import matplotlib.pyplot as plt
import aplpy
from wcsaxes import WCSAxes
from astropy import wcs
import sys
import matplotlib.image as mpimg
image_fits = 'image_in.fits'
image_png = 'image_in.png' # this came from the one before, has different pixelization
image_data_png = mpimg.imread(image_png)
image_head_fits = fits.getheader(image_fits)
hdu_list = fits.open(image_fits)
F = aplpy.FITSFigure(hdu_list, figure=plt.figure(1))
fig = plt.figure()
mywcs = wcs.WCS(image_head_fits)
ax = WCSAxes(fig,[0.1, 0.1, 0.8, 0.8],wcs=mywcs)
fig.add_axes(ax)
ax.imshow(image_data_png)
plt.savefig('image_out.png')