0

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.

Image in a subplot before and after disappearance

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.

  • I see a `[23]` in you images reminiscent of Jupyter. So I'll point out that often the viewport in Jupyter isn't sufficient for gauging how the image looks. I cannot tell if you posted all your code and so maybe you are taking the defaults for the most part. But what I want to get to saying is that for when you really care how it looks at full perspective & resolution, add in saving the figure with `.savefig()`, maybe something like `hostImage.savefig("my_image_test.png")`, & then open produced file separately in your local computer image viewing software at 100% zoom. – Wayne Aug 24 '22 at 15:14
  • This is one place where JupyterLab comes in handy, because it has an image file viewer built in. So once you save the image file, you can just double-click on the image in the file browser pane and then view the image in it's full glory where you can click to get to 100% or scroll around to see all if it, in case of large images. You really want to be looking at the produced image for most uses where you really need to see details. Often the direct output in Jupyter, & maybe your tech, is best just considered a preview. Don't know if it will help in this case though & cannot test without MRE. – Wayne Aug 24 '22 at 15:17
  • Regarding potential artefacts due Jupyter Lab (JL) view port: Checking on that showed me the contrary of what I would have expected: While the saved .png does not show the image in the subplot I can see it in JL. I tested if that still was an artifact due to JL and ran the same code on an independent command line, however the result was the same (no image in the .png). (P.S.: There was no 'hidden code' regarding my original post = I had posted all code - and also had the kernel in JL restarted in order to be sure everything be reset) – Thomas Lewis Aug 24 '22 at 15:42
  • Hmmm ... well maybe at least you made some progress trying JupyterLab? Minor aside: I needed to add `import mpl_toolkits.axisartist as axisartist` to get it to work. I'll post what I found in an answer. – Wayne Aug 24 '22 at 18:08
  • Sorry on the missing `axisartist`, I have added that line now in the code example. And also inserted a link to the image I had used for the MWE. – Thomas Lewis Aug 25 '22 at 11:42
  • I'll see what I can find using your specific image later. In the end, where do you want your image relative to the plot? – Wayne Aug 25 '22 at 12:45
  • Okay. That makes it clear. Do you care though if the approach uses `host_subplot`? I'm more familiar with the more typical subplot layout? – Wayne Aug 25 '22 at 16:18
  • In an further Update at the bottom of my answer, I added a simpler approach based on what you seem to really want. As I stated before, I'm not familiar with `host_subplot`. So I based it on more modern subplot stuff I knew about. Not sure why the slight difference in what is seen in the output vs the image produced but I suppose it comes down to what you are trying to produce and I think I provided options. – Wayne Aug 25 '22 at 17:51

2 Answers2

0

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.

Wayne
  • 6,607
  • 8
  • 36
  • 93
  • I'm using Matplotlib 3.5.1. I could run your code but not shift the image off: `AnnotationBbox(imagebox, (-0.2, 0), box_alignment=(0, 0))` hides the image. I was playing with 'left' in `figure.subplots_adjust(left=)` in combination with figsize but the image remained hidden. I have to admit I did not fully understand the logics of `figure.subplots_adjust(left=xx)`. Surpassing a certain value for left there's: `ValueError: left cannot be >= right.` However, increasing 'left' did not bring me towards what I want to achieve anyway (shift my image off the plot area), I just tried it. – Thomas Lewis Aug 25 '22 at 11:27
  • Thank you. I re-edited my original post (which has become quite long now) to better explain my use case and also explain why I was driven to use `host_subplot`. – Thomas Lewis Aug 26 '22 at 08:58
  • Reasonable, but that was not provided in your MRE. You could have made a toy example that included something way more akin to your real case plot. I'd suggest starting a new question where you put a toy example that better matches & a very specific title like "add an image below a plot with multiple y-axes & programmatically draw connections". I agree doing it all in matplotlib seems like the best path in that case. I suspect it can be done; I just don't have experience with extending something like your top plot that way. Basically package up what you added as the real question a bit better. – Wayne Aug 26 '22 at 19:09
  • 1
    First of all: Thanks for the large effort you have put into helping me. Secondly: I learned from your comment (thanks also for that) that when creating a MWE I should not try to simplify my 'real' underlying problem thus hiding it but better come up with it straight away. After all this makes it easier for potential helpers - even if their initial effort of understanding might be a bit elevated. – Thomas Lewis Aug 29 '22 at 09:24
0

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.