用python显示.fig Matlab文件

2024-06-26 11:25:11 发布

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

我得到了一个在Matlab中生成的.fig文件,我想在python环境中显示它,或者至少检索数据

我看到有人就这个问题提出了另一个问题,但我不允许在那里发表评论,因为没有足够的声誉

特别是用户Sascha提出了以下脚本:

def plotFig(filename,fignr=1):
    from scipy.io import loadmat
    from numpy import size
    from matplotlib.pyplot import plot,figure,hold,xlabel,ylabel,show,clf,xlim,legend
    d = loadmat(filename,squeeze_me=True, struct_as_record=False)
    ax1 = d['hgS_070000'].children
    if size(ax1) > 1:
        legs= ax1[1]
        ax1 = ax1[0]
    else:
        legs=0
    figure(fignr)
    clf()
    hold(True)
    counter = 0    
    for line in ax1.children:
        if line.type == 'graph2d.lineseries':
            if hasattr(line.properties,'Marker'):
                mark = "%s" % line.properties.Marker
                mark = mark[0]
            else:
                mark = '.'
            if hasattr(line.properties,'LineStyle'):
                linestyle = "%s" % line.properties.LineStyle
            else:
                linestyle = '-'
            if hasattr(line.properties,'Color'):
                r,g,b =  line.properties.Color
            else:
                r = 0
                g = 0
                b = 1
            if hasattr(line.properties,'MarkerSize'):
                marker_size = line.properties.MarkerSize
            else:
                marker_size = 1                
            x = line.properties.XData
            y = line.properties.YData
            plot(x,y,marker=mark,linestyle=linestyle,color=color(r,g,b),markersize=marker_size)
        elif line.type == 'text':
            if counter < 1:
                xlabel("%s" % line.properties.String,fontsize =16)
                counter += 1
            elif counter < 2:
                ylabel("%s" % line.properties.String,fontsize = 16)
                counter += 1        
    xlim(ax1.properties.XLim)
    if legs:        
        leg_entries = tuple(legs.properties.String)
        py_locs = ['upper center','lower center','right','left','upper right','upper left','lower right','lower left','best']
        MAT_locs=['North','South','East','West','NorthEast', 'NorthWest', 'SouthEast', 'SouthWest','Best']
        Mat2py = dict(zip(MAT_locs,py_locs))
        location = legs.properties.Location
        legend(leg_entries,loc=Mat2py[location])
    hold(False)
    show()

当我尝试在Spyder 4.1.5(python 3.7.7)中实现它时,会出现几个错误,但它们不会同时出现:

  1. 我得到的第一个错误是undefined name 'color'。Ander Biguri在下面的评论中解决了这个错误。然而,这个错误一解决,另一个错误就出现了

  2. 第二个错误是cannot import name 'hold' from 'matplotlib.pyplot':显然命令hold已经被弃用,因为它被认为是无用的。我可以通过注释该命令出现的代码行来避免错误。然后出现以下错误

  3. 'mat_struct' object has no attribute 'String'

我不知道如何解决这个问题。关键是我希望有一个运行的代码。 有人能帮忙吗


Tags: fromimportsizeif错误linecounterproperties