Commit 9b1309af authored by Unknown's avatar Unknown
Browse files

Update the export fig function

parent f51d0b25
......@@ -1482,9 +1482,9 @@ def save_fig_filebox_button(fig, filename):
layout={'width': '50%'})
save_button = widgets.Button(description='Save figure')
def _save_fig(junk):
def _save_fig():
save_path = os.path.join(file_dir, filename)
fig.save_fig(save_path)
fig.save_fig(save_path, dpi='figure')
print('Figure saved to "{}".'.format(save_path))
widget_box = widgets.HBox([name_box, save_button])
......@@ -1494,14 +1494,18 @@ def save_fig_filebox_button(fig, filename):
return widget_box
def export_fig_data(fig, basename='junk', ext='.dat', include_images=False):
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
basename
include_images
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
-------
......@@ -1518,7 +1522,25 @@ def export_fig_data(fig, basename='junk', ext='.dat', include_images=False):
im_dict = dict()
for im in ims:
im_dict[im.get_label()] = im.get_array().data
# Image data
im_lab = im.get_label()
im_dict[im_lab] = 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[im_lab + 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[im_lab + y_lab] = y_ax.get_data_interval()
ax_dict['Images'] = im_dict
......@@ -1543,5 +1565,55 @@ def export_fig_data(fig, basename='junk', ext='.dat', include_images=False):
if ax_dict != dict():
axes_dict[ax.get_title()] = ax_dict
basename = os.path.abspath(basename)
folder, _ = os.path.split(basename)
\ No newline at end of file
'''
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))
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)
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()
return
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment