首页 > 解决方案 > pyqtgraph中类似于matplotlib的`Gridspec`和`GridSpecFromSubplotSpec`的模块

问题描述

我已经确定执行代码中最耗时的步骤是将绘图保存到内存中。我正在使用matplotlib绘图和保存绘图。问题是我正在运行几个模拟并保存这些模拟产生的图;这项工作消耗了大量的计算时间。我已经证实确实是阴谋造成了损害。

看起来pyqtgraph渲染和保存图像的速度比matplotlib.我想知道的是否可以在 pyqtgraph 中实现类似于以下代码行的东西要快得多?

import matplotlib
matplotlib.use('Agg')
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
fig = plt.figure(figsize=(6, 2.5))
rows = 1
columns = 2

gs = gridspec.GridSpec(rows, columns, hspace=0.0,wspace=0.0)
aj=0
for specie in lines:

    for transition in species[specie].items():
        gss = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=gs[aj],hspace=0.0,height_ratios=[1, 3])

        ax0 = fig.add_subplot(gss[0])
        ax1 = fig.add_subplot(gss[1], sharex=ax0)
        ax0.plot(fitregs[specie+transition[0]+'_Vel'],fitregs['N_residue'],color='black',linewidth=0.85)                
        ax1.plot(fitregs[specie+transition[0]+'_Vel'],fitregs['N_flux'],color='black',linewidth=0.85)
        ax1.plot(fitregs[specie+transition[0]+'_Vel'],fitregs['Best_profile'],color='red',linewidth=0.85)

        ax1.xaxis.set_minor_locator(AutoMinorLocator())
        ax1.tick_params(which='both', width=1)
        ax1.tick_params(which='major', length=5)
        ax1.tick_params(which='minor', length=2.5)
        ax1.text(0.70,0.70,r'$\chi^{2}_{\nu}$'+'= {}'.format(round(red_chisqr[aj],2)),transform=ax1.transAxes)
        ax1.text(0.10,0.10,'{}'.format(specie+' '+transition[0]),transform=ax1.transAxes)
        ak=ak+1
        aj=aj+1
canvas = FigureCanvas(fig)
canvas.print_figure('fits.png')

上述代码的示例输出是例子

标签: python-2.7matplotlibpyqtgraph

解决方案


推荐阅读