无法保存matplotlib.figure图,画布为“无" [英] Unable to save matplotlib.figure Figure, canvas is None

查看:57
本文介绍了无法保存matplotlib.figure图,画布为“无"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑(假设代码运行没有错误):

Consider (Assume code runs without error):

import matplotlib.figure as matfig

    ind = numpy.arange(N)
    width = 0.50;
    fig = matfig.Figure(figsize=(16.8, 8.0))
    fig.subplots_adjust(left=0.06, right = 0.87)
    ax1 = fig.add_subplot(111)
    prev_val = None
    fig.add_axes(ylabel = 'Percentage(%)',xlabel='Wafers',title=title,xticks=(ind+width/2.0,source_data_frame['WF_ID']))
    fig.add_axes(ylim=(70,100))

    for key,value in bar_data.items():
        ax1.bar(ind,value, width,color='#40699C', bottom=prev_val)
        if prev_val:
            prev_val = [a+b for (a,b) in zip(prev_val,value)]
        else:
            prev_val=value

    names= []
    for i in range(0,len(col_data.columns)):
        names.append(col_data.columns[i])
    ax1.legend(names,bbox_to_anchor=(1.15, 1.02))

我现在想用 fig.savefig(outputPath,dpi = 300)保存我的身材,但是我得到 AttributeError:'NoneType'对象没有属性'print_figure',因为 fig.canvas 为无".子图应该在数字画布上,所以它不应该是 None.我想我缺少关于 matplot 图形画布的关键概念.如何更新 fig.canvas 以反映当前图形,以便我可以使用 fig.savefig(outputPath, dpi=300)?谢谢!

I now want to save my figure with fig.savefig(outputPath, dpi=300), but I get AttributeError: 'NoneType' object has no attribute 'print_figure', because fig.canvas is None. The sub plots should be on the figures canvas, so it shouldn't be None. I think i'm missing a key concept about matplot figures canvas.How can I update fig.canvas to reflect the current Figure, so i can use fig.savefig(outputPath, dpi=300)? Thanks!

推荐答案

plt.figure 为您做的一件事就是为您处理后端,其中包括设置画布.mpl的体系结构是 Artist 级别的对象知道如何设置自己,确保所有东西都相对于彼此在正确的位置等,然后在被问到时将它们自己绘制到画布上.因此,即使您已经设置了子图和线,您实际上还没有使用过画布.当您尝试保存图形时,您是在要求画布要求所有艺术家在其上绘制自己.您尚未创建画布(特定于给定后端),因此它会抱怨.

One of the things that plt.figure does for you is wrangle the backend for you, and that includes setting up the canvas. The way the architecture of mpl is the Artist level objects know how to set themselves up, make sure everything is in the right place relative to each other etc and then when asked, draw them selves onto the canvas. Thus, even though you have set up subplots and lines, you have not actually used the canvas yet. When you try to save the figure you are asking the canvas to ask all the artists to draw them selves on to it. You have not created a canvas (which is specific to a given backend) so it complains.

按照此处的示例,您需要创建一个可以嵌入到您的 tk 应用程序中的画布(从你的最后一个问题)

Following the example here you need to create a canvas you can embed in your tk application (following on from your last question)

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
canvas = FigureCanvasTkAgg(f, master=root)

canvas 是一个 Tk 小部件,可以添加到gui中.

canvas is a Tk widget and can be added to a gui.

如果您不想在 Tk 中嵌入您的图形,您可以使用 此处(直接从链接中提取的代码):

If you don't want to embed your figure in Tk you can use the pure OO methods shown here (code lifted directly from link):

from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure

fig = Figure()
canvas = FigureCanvas(fig)
ax = fig.add_subplot(111)
ax.plot([1,2,3])
ax.set_title('hi mom')
ax.grid(True)
ax.set_xlabel('time')
ax.set_ylabel('volts')
canvas.print_figure('test')

这篇关于无法保存matplotlib.figure图,画布为“无"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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