1

What i want

I want to combine two matplotlib figures in one new subplot. The two figures are returned from visualization functions of libraries i don't want to or can't change myself(rebuild from source etc.). Also it should be not a hack-around but rather be a nice generic matplotlib solution.

The pseudo code looks like the following.

Pseudo code

import matplotlib.pyplot as plt

from library1 import magic_visualization_1
from library2 import magic_visualization_2

# Data of type some_crazy_data_type_of_the_library e.g. no simple x,y coords
data = ...

fig1 = magic_visualization_1(data)    # type is: <class 'matplotlib.figure.Figure'>
fig2 = magic_visualization_2(data)    # type is: <class 'matplotlib.figure.Figure'>

fig, axs = plt.subplots(2, 1, figsize=(10, 5))
# Somehow add fig1
# Somehow add fig2
plt.show()

# or like

fig = plt.figure(figsize=(10, 5))
gridspec = fig.add_gridspec(2, 1, left=0.05, right=0.95, wspace=0.1, hspace=0.15)
# Somehow add fig1
# Somehow add fig2
plt.show()

Example images

The two example figures: fig1, fig2

Photoshoped result

I should look like this(i made this by hand with gimp/photoshop) fig1 on top of fig2

What i tried

The best idea i found was deepcopying every figure into the new subfigures but that feels to much like a hack-around.

Also i tried this solution with copying the two figures content.

Result: vertical concatenation of the two figures

Henning
  • 11
  • 1
  • Welcome @henning, could you add the images inline so that readers don't have to press on links? Also, it seems your result is close to what you want, have you tried turning off the axes and switching both images around? – Stereo Jan 03 '23 at 10:10
  • Thanks @Stereo for your response. I don't have enough reputation to post images. The order of the images is not important. As i said it is more like a question if matplotlib has a best practice for that. I can create a solution but i have to fiddle around(turn off axes, etc. to look like i want it) or use a software(photoshop/gimp) afterwards. It seems like a very common use case to me to combine pre-created figures into one new figure. – Henning Jan 03 '23 at 11:27
  • If you need to do this often, I'd probably recommend using OpenCV or PIL, loading both images and superposing them. – Stereo Jan 03 '23 at 13:24

0 Answers0