Plot something in one figure, and use it again later for another figure

一曲冷凌霜 提交于 2019-11-28 10:09:32

问题


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)


回答1:


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:



来源:https://stackoverflow.com/questions/45861656/plot-something-in-one-figure-and-use-it-again-later-for-another-figure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!