5

I hope I'm asking this question at the right place.

I have a for-loop, in that many figures are created. After the loop is finished I want to produce one more figure with three of those earlier created plots as suplots.

My code right now is like this:

import numpy as np
import matplotlib.pyplot as plt

def f(t):
    return np.exp(-t)*np.cos(2*np.pi*t)+(t/10)**2.

t1=np.arange(0.0,5.0,0.1)
t2=np.arange(0.0,5.0,0.02)


for i in range(2):
    fig= plt.figure(i)
    ax1=fig.add_subplot(111)
    plt.title('Jon Snow')
    kraft_plot,=ax1.plot(t1,np.sin(t1),color='purple')
    tyrion=ax1.axvline(2,color='darkgreen',ls='dashed')
    ax1.set_ylabel('Kraft [N]',color='purple',fontweight='bold')
    ax1.set_xlabel('Zeit [s]',fontweight='bold')
    ax2=ax1.twinx()
    strecke_plot,=ax2.plot(t2,t2/5,color='grey',label='Verlauf der Strecke')
    ax2.set_ylabel('Strecke [mm]',color='grey',fontweight='bold')
    ax1.legend((kraft_plot,tyrion,strecke_plot),('Jonny','Dwarf','andalltherest'),loc=2)

plt.show()

Can you help me? I it possible to save a whole figure/plot?

Cheers, Dalleaux.

Edit: It should look like this (the right part is what I want to achieve): The text in the right should be normal sclaed, obviously...

The Problem is, that i FIRST want to have the figures printet alone and then together (finally I want to save as pdf/png with three figures in it)

dalleaux
  • 527
  • 5
  • 11
  • Your question is at the right place, yes. However, it is not clear what exactly you are trying to achieve. Possibly this is simply due to the wording in the sense of what you call "three of those earlier created plots". What is a "plot" in your definition? As I see it, the code above creates 2 figures. Each figure has 2 subplots. Hence you end up with 4 plots in total. Is it that you want to use 3 of those 4 plots in a new figure? If so, how would you like to select which ones you use? – ImportanceOfBeingErnest Aug 24 '17 at 12:34
  • Hello @ImportanceOfBeingErnest, Well, I get 2 figures from this code with a view graphs in them. After that i want to have a third figure, where both of the origin figures are combined (one above the other). – dalleaux Aug 24 '17 at 12:44

1 Answers1

4

In matplotlib an axes (a subplot) is always part of exactly one figure. While there are options to copy an axes from one figure to another, it is a rather complicated procedure. Instead, you may simply recreate the plot as often as you need it in several figures. Using a function, which takes the axes to plot to as argument, makes this rather straight-forward.

In order to save all three figures in a pdf, you may use pdfPages as shown in the bottom of the code.

import numpy as np
import matplotlib.pyplot as plt

def f(t):
    return np.exp(-t)*np.cos(2*np.pi*t)+(t/10)**2.

t1=np.arange(0.0,5.0,0.1)
t2=np.arange(0.0,5.0,0.02)

def plot(ax, i):
    ax.set_title('Jon Snow')
    kraft_plot,=ax.plot(t1,np.sin(t1),color='purple')
    tyrion=ax.axvline(2,color='darkgreen',ls='dashed')
    ax.set_ylabel('Kraft [N]',color='purple',fontweight='bold')
    ax.set_xlabel('Zeit [s]',fontweight='bold')
    ax2=ax.twinx()
    strecke_plot,=ax2.plot(t2,t2/5,color='grey',label='Verlauf der Strecke')
    ax2.set_ylabel('Strecke [mm]',color='grey',fontweight='bold')
    ax.legend((kraft_plot,tyrion,strecke_plot),('Jonny','Dwarf','andalltherest'),loc=2)

figures=[]

for i in range(2):
    fig= plt.figure(i)
    ax1=fig.add_subplot(111)
    plot(ax1, i)
    figures.append(fig)

# create third figure
fig, (ax1,ax2) = plt.subplots(nrows=2)
plot(ax1, 0)
plot(ax2, 1)
figures.append(fig)

from matplotlib.backends.backend_pdf import PdfPages
with PdfPages('multipage_pdf.pdf') as pdf:
    for fig in figures:
        pdf.savefig(fig)


plt.show()

Three page pdf output:

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Thank you very much. I got it working, even it needed a little bit of work since my original code was slightly more complicated;) – dalleaux Aug 24 '17 at 15:51