sidpy/viz/plot_utils/misc.py
def export_fig_data(fig, filename, include_images=False):
    """
    Export the data of all plots in the figure `fig` to a plain text file.

    Parameters
    ----------
    fig : matplotlib.figure.Figure
        The figure containing the data to be exported
    filename : str
        The filename of the output text file
    include_images : bool
        Should images in the figure also be exported

    Returns
    -------

    """
    # Get the data from the figure
    axes = fig.get_axes()
    axes_dict = dict()
    for ax in axes:
        ax_dict = dict()

        ims = ax.get_images()
        if len(ims) != 0 and include_images:
            im_dict = dict()

            for im in ims:
                # Image data
                im_lab = im.get_label()
                im_dict['data'] = im.get_array().data

                # X-Axis
                x_ax = ax.get_xaxis()
                x_lab = x_ax.label.get_label()
                if x_lab == '':
                    x_lab = 'X'

                im_dict[x_lab] = x_ax.get_data_interval()

                # Y-Axis
                y_ax = ax.get_yaxis()
                y_lab = y_ax.label.get_label()
                if y_lab == '':
                    y_lab = 'Y'

                im_dict[y_lab] = y_ax.get_data_interval()

                ax_dict['Images'] = {im_lab: im_dict}

        lines = ax.get_lines()
        if len(lines) != 0:
            line_dict = dict()

            xlab = ax.get_xlabel()
            ylab = ax.get_ylabel()

            if xlab == '':
                xlab = 'X Data'
            if ylab == '':
                ylab = 'Y Data'

            for line in lines:
                line_dict[line.get_label()] = {xlab: line.get_xdata(),
                                               ylab: line.get_ydata()}

            ax_dict['Lines'] = line_dict

        if ax_dict != dict():
            axes_dict[ax.get_title()] = ax_dict

    '''
    Now that we have the data from the figure, we need to write it to file.
    '''

    filename = os.path.abspath(filename)
    basename, ext = os.path.splitext(filename)
    folder, _ = os.path.split(basename)

    spacer = r'**********************************************\n'

    data_file = open(filename, 'w')

    data_file.write(fig.get_label() + '\n')
    data_file.write('\n')

    for ax_lab, ax in axes_dict.items():
        data_file.write('Axis: {} \n'.format(ax_lab))

        if 'Images' not in ax:
            continue
        for im_lab, im in ax['Images'].items():
            data_file.write('Image: {} \n'.format(im_lab))
            data_file.write('\n')
            im_data = im.pop('data')
            for row in im_data:
                row.tofile(data_file, sep='\t', format='%s')
                data_file.write('\n')
            data_file.write('\n')

            for key, val in im.items():
                data_file.write(key + '\n')

                val.tofile(data_file, sep='\n', format='%s')
                data_file.write('\n')

            data_file.write(spacer)

        if 'Lines' not in ax:
            continue
        for line_lab, line_dict in ax['Lines'].items():
            data_file.write('Line: {} \n'.format(line_lab))
            data_file.write('\n')

            dim1, dim2 = line_dict.keys()

            data_file.write('{} \t {} \n'.format(dim1, dim2))
            for val1, val2 in zip(line_dict[dim1], line_dict[dim2]):
                data_file.write('{} \t {} \n'.format(str(val1), str(val2)))

            data_file.write(spacer)

        data_file.write(spacer)

    data_file.close()

    Export the data of all plots in the figure `fig` to a plain text file.

    Parameters
    ----------
    fig : matplotlib.figure.Figure
        The figure containing the data to be exported
    filename : str
        The filename of the output text file
    include_images : bool
        Should images in the figure also be exported

    Returns
    -------

    
Export the data of all plots in the figure `fig` to a plain text file.
Parameters

fig : matplotlib.figure.Figure
The figure containing the data to be exported
filename : str
The filename of the output text file
include_images : bool
Should images in the figure also be exported

Returns
Export the data of all plots in the figure ` fig ` to a plain text file . Parameters fig : matplotlib . figure . Figure The figure containing the data to be exported filename : str The filename of the output text file include_images : bool Should images in the figure also be exported Returns
def export_fig_data(fig, filename, include_images=False):
    axes = fig.get_axes()
    axes_dict = dict()
    for ax in axes:
        ax_dict = dict()
        ims = ax.get_images()
        if len(ims) != 0 and include_images:
            im_dict = dict()
            for im in ims:
                im_lab = im.get_label()
                im_dict['data'] = im.get_array().data
                x_ax = ax.get_xaxis()
                x_lab = x_ax.label.get_label()
                if x_lab == '':
                    x_lab = 'X'
                im_dict[x_lab] = x_ax.get_data_interval()
                y_ax = ax.get_yaxis()
                y_lab = y_ax.label.get_label()
                if y_lab == '':
                    y_lab = 'Y'
                im_dict[y_lab] = y_ax.get_data_interval()
                ax_dict['Images'] = {im_lab: im_dict}
        lines = ax.get_lines()
        if len(lines) != 0:
            line_dict = dict()
            xlab = ax.get_xlabel()
            ylab = ax.get_ylabel()
            if xlab == '':
                xlab = 'X Data'
            if ylab == '':
                ylab = 'Y Data'
            for line in lines:
                line_dict[line.get_label()] = {xlab: line.get_xdata(),
                                               ylab: line.get_ydata()}
            ax_dict['Lines'] = line_dict
        if ax_dict != dict():
            axes_dict[ax.get_title()] = ax_dict
    filename = os.path.abspath(filename)
    basename, ext = os.path.splitext(filename)
    folder, _ = os.path.split(basename)
    spacer = r'**********************************************\n'
    data_file = open(filename, 'w')
    data_file.write(fig.get_label() + '\n')
    data_file.write('\n')
    for ax_lab, ax in axes_dict.items():
        data_file.write('Axis: {} \n'.format(ax_lab))
        if 'Images' not in ax:
            continue
        for im_lab, im in ax['Images'].items():
            data_file.write('Image: {} \n'.format(im_lab))
            data_file.write('\n')
            im_data = im.pop('data')
            for row in im_data:
                row.tofile(data_file, sep='\t', format='%s')
                data_file.write('\n')
            data_file.write('\n')
            for key, val in im.items():
                data_file.write(key + '\n')
                val.tofile(data_file, sep='\n', format='%s')
                data_file.write('\n')
            data_file.write(spacer)
        if 'Lines' not in ax:
            continue
        for line_lab, line_dict in ax['Lines'].items():
            data_file.write('Line: {} \n'.format(line_lab))
            data_file.write('\n')
            dim1, dim2 = line_dict.keys()
            data_file.write('{} \t {} \n'.format(dim1, dim2))
            for val1, val2 in zip(line_dict[dim1], line_dict[dim2]):
                data_file.write('{} \t {} \n'.format(str(val1), str(val2)))
            data_file.write(spacer)
        data_file.write(spacer)
    data_file.close()
def export_fig_data ( fig , filename , include_images = False ) : axes = fig . get_axes ( ) axes_dict = dict ( ) for ax in axes : ax_dict = dict ( ) ims = ax . get_images ( ) if len ( ims ) != 0 and include_images : im_dict = dict ( ) for im in ims : im_lab = im . get_label ( ) im_dict [ 'data' ] = im . get_array ( ) . data x_ax = ax . get_xaxis ( ) x_lab = x_ax . label . get_label ( ) if x_lab == '' : x_lab = 'X' im_dict [ x_lab ] = x_ax . get_data_interval ( ) y_ax = ax . get_yaxis ( ) y_lab = y_ax . label . get_label ( ) if y_lab == '' : y_lab = 'Y' im_dict [ y_lab ] = y_ax . get_data_interval ( ) ax_dict [ 'Images' ] = { im_lab : im_dict } lines = ax . get_lines ( ) if len ( lines ) != 0 : line_dict = dict ( ) xlab = ax . get_xlabel ( ) ylab = ax . get_ylabel ( ) if xlab == '' : xlab = 'X Data' if ylab == '' : ylab = 'Y Data' for line in lines : line_dict [ line . get_label ( ) ] = { xlab : line . get_xdata ( ) , ylab : line . get_ydata ( ) } ax_dict [ 'Lines' ] = line_dict if ax_dict != dict ( ) : axes_dict [ ax . get_title ( ) ] = ax_dict '''
    Now that we have the data from the figure, we need to write it to file.
    ''' filename = os . path . abspath ( filename ) basename , ext = os . path . splitext ( filename ) folder , _ = os . path . split ( basename ) spacer = r'**********************************************\n' data_file = open ( filename , 'w' ) data_file . write ( fig . get_label ( ) + '\n' ) data_file . write ( '\n' ) for ax_lab , ax in axes_dict . items ( ) : data_file . write ( 'Axis: {} \n' . format ( ax_lab ) ) if 'Images' not in ax : continue for im_lab , im in ax [ 'Images' ] . items ( ) : data_file . write ( 'Image: {} \n' . format ( im_lab ) ) data_file . write ( '\n' ) im_data = im . pop ( 'data' ) for row in im_data : row . tofile ( data_file , sep = '\t' , format = '%s' ) data_file . write ( '\n' ) data_file . write ( '\n' ) for key , val in im . items ( ) : data_file . write ( key + '\n' ) val . tofile ( data_file , sep = '\n' , format = '%s' ) data_file . write ( '\n' ) data_file . write ( spacer ) if 'Lines' not in ax : continue for line_lab , line_dict in ax [ 'Lines' ] . items ( ) : data_file . write ( 'Line: {} \n' . format ( line_lab ) ) data_file . write ( '\n' ) dim1 , dim2 = line_dict . keys ( ) data_file . write ( '{} \t {} \n' . format ( dim1 , dim2 ) ) for val1 , val2 in zip ( line_dict [ dim1 ] , line_dict [ dim2 ] ) : data_file . write ( '{} \t {} \n' . format ( str ( val1 ) , str ( val2 ) ) ) data_file . write ( spacer ) data_file . write ( spacer ) data_file . close ( )
Export the data of all plots in the figure `fig` to a plain text file.
Export the data of all plots in the figure ` fig ` to a plain text file .
"""
    Export the data of all plots in the figure `fig` to a plain text file.

    Parameters
    ----------
    fig : matplotlib.figure.Figure
        The figure containing the data to be exported
    filename : str
        The filename of the output text file
    include_images : bool
        Should images in the figure also be exported

    Returns
    -------

    """ # Get the data from the figure # Image data # X-Axis # Y-Axis '''
    Now that we have the data from the figure, we need to write it to file.
    '''
fig
filename
include_images