plot_2d_2.py 939 B

123456789101112131415161718192021222324
  1. def plot_trajectory(proj_file, dir_file, show=False):
  2. """ Plot optimization trajectory on the plane spanned by given directions."""
  3. assert exists(proj_file), 'Projection file does not exist.'
  4. f = h5py.File(proj_file, 'r')
  5. fig = plt.figure()
  6. plt.plot(f['proj_xcoord'], f['proj_ycoord'], marker='.')
  7. plt.tick_params('y', labelsize='x-large')
  8. plt.tick_params('x', labelsize='x-large')
  9. f.close()
  10. if exists(dir_file):
  11. f2 = h5py.File(dir_file,'r')
  12. if 'explained_variance_ratio_' in f2.keys():
  13. ratio_x = f2['explained_variance_ratio_'][0]
  14. ratio_y = f2['explained_variance_ratio_'][1]
  15. plt.xlabel('1st PC: %.2f %%' % (ratio_x*100), fontsize='xx-large')
  16. plt.ylabel('2nd PC: %.2f %%' % (ratio_y*100), fontsize='xx-large')
  17. f2.close()
  18. fig.savefig(proj_file + '.pdf', dpi=300, bbox_inches='tight', format='pdf')
  19. if show: plt.show()