无法保存matplotlib.figure图形,画布为非

2024-05-26 16:29:15 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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' object has no attribute 'print_figure',因为fig.canvas不是。子图应该在图形画布上,所以它不应该是无的。我想我缺少了一个关于matplot figures画布的关键概念,如何更新fig.canvas以反映当前图形,以便使用fig.savefig(outputPath, dpi=300)?谢谢!


Tags: inadd图形fordatanamesvaluefig
3条回答

你的示例代码不够完整,我无法运行它来验证我的答案,这可能取决于“matfig”是如何定义的,但我猜你想要的是:

fig = matfig.figure(figsize=(16.8, 8.0))

不是:

fig = matfig.Figure(figsize=(16.8, 8.0))

图是应该调用/调用的模块方法。

图是所有plot元素的顶级容器,尽管它比这个复杂一些。

plt.figure为您做的事情之一是为您包装后端,包括设置画布。mpl的体系结构是Artist级对象知道如何设置自己,确保所有东西都位于彼此相对的正确位置等,然后当被要求时,将它们自己绘制到画布上。因此,即使您已经设置了子块和行,实际上您还没有使用画布。当你试图保存这个图形时,你要求画布要求所有的艺术家把他们自己画在上面。您还没有创建画布(特定于给定的后端),所以它会抱怨。

根据示例here,您需要创建一个可以嵌入到tk应用程序中的画布(从上一个问题开始)

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

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

如果不想将图形嵌入Tk中,可以使用OO所示的纯方法here(直接从链接中提取的代码):

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()方法而不是figure()方法。注意大写字母。在下面的示例代码中,如果您有figure=plt.figure(),您将得到问题中的错误。通过使用figure=plt.figure()为您创建画布。

这里有一个例子,其中也包括一些关于重新调整图像大小的小道消息,因为您也希望在这方面得到帮助。

#################################
# Importing Modules
#################################
import numpy
import matplotlib.pyplot as plt


#################################
# Defining Constants
#################################
x_triangle = [0.0, 6.0, 3.0]
y_triangle = [0.0, 0.0, 3.0 * numpy.sqrt(3.0)]
x_coords = [4.0]
y_coords = [1.0]
big_n = 5000
# file_obj = open('/Users/lego/Downloads/sierpinski_python.dat', 'w')
figure = plt.figure()
axes = plt.axes()



#################################
# Defining Functions
#################################
def interger_function():
    value = int(numpy.floor(1+3*numpy.random.rand(1)[0]))
    return value


def sierpinski(x_value, y_value, x_traingle_coords, y_triangle_coords):
    index_for_chosen_vertex = interger_function() - 1
    x_chosen_vertex = x_traingle_coords[index_for_chosen_vertex]
    y_chosen_vertex = y_triangle_coords[index_for_chosen_vertex]
    next_x_value = (x_value + x_chosen_vertex) / 2
    next_y_value = (y_value + y_chosen_vertex) / 2
    return next_x_value, next_y_value



#################################
# Performing Work
#################################

for i in range(0, big_n):
    result_from_sierpinski = sierpinski(x_coords[i], y_coords[i], x_triangle, y_triangle)
    x_coords.append(result_from_sierpinski[0])
    y_coords.append(result_from_sierpinski[1])

axes.plot(x_coords, y_coords, marker = 'o', color='darkcyan', linestyle='none')
plot_title_string = "Sierpinski Gasket with N = " + str(big_n)
plt.title(plot_title_string)
plt.xlabel('x coodinate')
plt.ylabel('y coordinate')
figure.set_figheight(10)
figure.set_figwidth(20)
file_path = '{0}.png'.format(plot_title_string)
figure.savefig(file_path, bbox_inches='tight')
plt.close()
# plt.show()

相关问题 更多 >

    热门问题