MatPlotLib在python中非常慢

时间:2016-10-18 18:41:51

标签: python matplotlib

import matplotlib
matplotlib.use('TkAgg')
def generate_graph(self,subject,target,filename):
    x_data = range(0, len(self.smooth_hydro))
    mslen = len([i[1] for i in self.master_seq.items()][0])
    diff=(mslen-len(self.smooth_hydro))/2
    x1_data = range(0,len(self.smooth_groups.items()[0][-1]))
    x2_data = range(0,mslen)
    plt.figure()
    plt.axhline(y=0, color='black')
    plt.ylim(-3, 3)
    plt.xlim(right=mslen)
    plt.plot(x_data, self.smooth_hydro, linewidth=1.0, label="hydrophobicity", color='r')
    plt.plot(x_data, self.smooth_amphi, linewidth=1.0, label="amphipathicity", color='g')
    for pos in self.hmmtop:
        plt.axvline(x=pos-1-diff, ymin=-2, ymax = 0.1, linewidth=1, color='black',alpha=0.2)

    plt.axvspan(subject[0]-diff,subject[1]-diff, facecolor="orange", alpha=0.2)
    plt.axvspan(target[0]-diff,target[1]-diff, facecolor="orange", alpha=0.2)

    plt.legend(loc='upper center', bbox_to_anchor=(0.5, 1.05),
              ncol=3, fancybox=True, shadow=True)
    plt.xlabel("Residue Number")
    plt.ylabel("Value")
    width = (0.0265)*len(self.master_seq[0]) if mslen > 600 else 15
    plt.grid('on')
    plt.savefig(self.out+'/graphs/'+filename+'.png')
    plt.clf()
    plt.cla()
    plt.close()

我反复调用此函数,但生成的图像非常慢。有人可以帮我优化这段代码,以便它可以更快地运行吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

我在另一个post找到了这个答案。全部归功于Luke

  

Matplotlib制作出色的出版品质图形,但不是很好   很好地优化了速度。有各种各样的python绘图   设计时考虑到速度的包: