在一个图形中绘制某些内容,稍后再用于另一图形 [英] Plot something in one figure, and use it again later for another figure

查看:78
本文介绍了在一个图形中绘制某些内容,稍后再用于另一图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我在正确的位置问这个问题.

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

我有一个for循环,其中创建了许多图形. 循环结束后,我想用先前创建的三个绘图中的三个作为辅助绘图,再生成一个图形.

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.

我现在的代码如下:

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?

干杯,达洛.

它应该看起来像这样(正确的部分是我想要实现的): 右边的文本应该是正常的空格,显然...

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

问题是,我首先想将数字单独打印,然后再一起打印(最后我想另存为pdf/png,其中包含三个数字)

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)

推荐答案

在matplotlib中,轴(子图)始终只是一个图形的一部分.虽然有将轴从一个图形复制到另一个图形的选项,但这是一个相当复杂的过程.取而代之的是,您可以根据需要在多个图中简单地重新创建绘图.使用一个函数,该函数将要绘制的轴作为参数,这使操作变得很简单.

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.

为了将所有三个图形保存为pdf,可以使用代码底部所示的pdfPages.

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()

三页pdf输出:

这篇关于在一个图形中绘制某些内容,稍后再用于另一图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆