3

I understand there are various ways to plot multiple graphs in one figure. One such way is using axes, e.g.

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([range(8)])
ax.plot(...)

Since I have a function that beautifies my graphs and subsequently returns a figure, I would like to use that figure to be plotted in my subplots. It should look similar to this:

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(figure1) # where figure is a plt.figure object
ax.plot(figure2)

This does not work but how can I make it work? Is there a way to put figures inside subplots or a workaround to plot multiple figures in one overall figure?

Any help on this is much appreciated. Thanks in advance for your comments.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Arne
  • 309
  • 2
  • 6
  • 12

2 Answers2

4

If the goal is just to customize individual subplots, why not change your function to change the current figure on the fly rather than return a figure. From matplotlib and seaborn, can you just change the plot settings as they are being plotted?

import numpy as np
import matplotlib.pyplot as plt

plt.figure()

x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)

y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)

plt.subplot(2, 1, 1)
plt.plot(x1, y1, 'ko-')
plt.title('A tale of 2 subplots')
plt.ylabel('Damped oscillation')

import seaborn as sns

plt.subplot(2, 1, 2)
plt.plot(x2, y2, 'r.-')
plt.xlabel('time (s)')
plt.ylabel('Undamped')

plt.show()

enter image description here

Perhaps I don't understand your question entirely. Is this 'beautification' function complex?...

nagordon
  • 1,307
  • 2
  • 13
  • 16
1

A possible solution is

import matplotlib.pyplot as plt

# Create two subplots horizontally aligned (one row, two columns)
fig, ax = plt.subplots(1,2)
# Note that ax is now an array consisting of the individual axis

ax[0].plot(data1) 
ax[1].plot(data2)

However, in order to work data1,2 needs to be data. If you have a function which already plots the data for you I would recommend to include an axis argument to your function. For example

def my_plot(data,ax=None):
    if ax == None:
        # your previous code
    else:
        # your modified code which plots directly to the axis
        # for example: ax.plot(data)

Then you can plot it like

import matplotlib.pyplot as plt

# Create two subplots horizontally aligned
fig, ax = plt.subplots(2)
# Note that ax is now an array consisting of the individual axis

my_plot(data1,ax=ax[0])
my_plot(data2,ax=ax[1])
plonser
  • 3,323
  • 2
  • 18
  • 22
  • Thanks a lot for your answer, but this is exactly what I am trying to circumvent. I do not want to plot data but a readily retrieved figure object. – Arne May 06 '15 at 09:36
  • 1
    @Arne: I have never met a builtin-function which includes two figures into a single figure. So, it would be necessary to extract all data from the figures objects and plot them again in the new figure using several axis. Although this is probably possible it is more complicated than simplfy giving the axis as an argument. – plonser May 06 '15 at 09:59