def histograms(modelLogger): if not cc.cfg['plots']['histograms']: return if not os.path.exists(cc.cfg['plots']['histograms_dir']): os.makedirs(cc.cfg['plots']['histograms_dir']) cntEpochs = len(modelLogger.epochLogs) cntLayers = len(modelLogger.epochLogs[-1]['weights']) logVals = [ {'name':'weights','color':'blue'}, {'name':'updates','color':'red'}, {'name':'ratios','color':'green'} ] subplotRows = len(logVals) subplotCols = cntLayers for x in range(cntEpochs): subplotIdx = 1 plt.figure(figsize=(5*subplotCols,5*subplotRows)) plt.suptitle('Histograms per layer, epoch {}/{}'.format(x,cntEpochs-1), fontsize=14) for logVal in logVals: for i,layer in enumerate(modelLogger.epochLogs[x][logVal['name']]): histmin = layer.min() histmax = layer.max() plt.subplot(subplotRows, subplotCols, subplotIdx) plt.title('{}, {}'.format(modelLogger.model.layers[modelLogger.loggedLayers[i]].name,logVal['name'])) plt.hist(layer, range = (histmin, histmax), bins = 30, color = logVal['color']) subplotIdx+=1 plt.tight_layout() plt.subplots_adjust(top=0.9) plt.savefig('{}/hist_{e:03d}.png'.format(cc.cfg['plots']['histograms_dir'], e = x)) plt.close()
def plot_multiple_streams(self, fig, ax, datasets, colors, axis_font={}, title_font={}, tick_font={}, width_in=8.3 , plot_qaqc=0, scatter=False, **kwargs): # Plot a timeseries with multiple y-axes using multiple streams from uFrame # # Acknowledgment: This function is based on code written by Jae-Joon Lee, # URL= http://matplotlib.svn.sourceforge.net/viewvc/matplotlib/trunk/matplotlib/ # examples/pylab_examples/multiple_yaxis_with_spines.py?revision=7908&view=markup # # http://matplotlib.org/examples/axes_grid/demo_parasite_axes2.html if not axis_font: axis_font = axis_font_default if not title_font: title_font = title_font_default n_vars = len(datasets) if n_vars > 6: raise Exception('This code currently handles a maximum of 6 independent variables.') elif n_vars < 2: raise Exception('This code currently handles a minimum of 2 independent variables.') if scatter: kwargs['marker'] = 'o' # Generate the plot. # Use twinx() to create extra axes for all dependent variables except the first # (we get the first as part of the ax axes). y_axis = n_vars * [0] y_axis[0] = ax for i in range(1, n_vars): y_axis[i] = ax.twinx() ax.spines["top"].set_visible(False) self.make_patch_spines_invisible(y_axis[1]) self.set_spine_direction(y_axis[1], "top") # Define the axes position offsets for each 'extra' axis spine_directions = ["left", "right", "left", "right", "left", "right"] # Adjust the axes left/right accordingly if n_vars >= 4: if width_in < 8.3: # set axis location offset = [1.2, -0.2, 1.40, -0.40] # overwrite width l_mod = 0.3 r_mod = 0.8 else: offset = [1.10, -0.10, 1.20, -0.20] l_mod = 0.5 r_mod = 1.2 plt.subplots_adjust(left=l_mod, right=r_mod) elif n_vars == 3: offset = [1.20, -0.20, 1.40, -0.40] plt.subplots_adjust(left=0.0, right=0.7) count = 0 for i in range(2, n_vars): y_axis[i].spines[spine_directions[count+1]].set_position(("axes", offset[count])) self.make_patch_spines_invisible(y_axis[i]) self.set_spine_direction(y_axis[i], spine_directions[count+1]) count += 1 # Plot the data legend_handles = [] legend_labels = [] for ind, data in enumerate(datasets): xlabel = data['x_field'][0] ylabel = data['y_field'][0] xdata = data['x'][xlabel] ydata = data['y'][ylabel] # Handle the QAQC data qaqc = data['qaqc'][ylabel] if plot_qaqc >= 10: # Plot all of the qaqc flags results # qaqc_data = data['qaqc'][ylabel] pass elif plot_qaqc >= 1: # This is a case where the user wants to plot just one of the 9 QAQC tests ind = np.where(qaqc != plot_qaqc) qaqc[ind] = 0 else: qaqc = [] h, = y_axis[ind].plot(xdata, ydata, colors[ind], label=data['title'], **kwargs) if len(qaqc) > 0: bad_data = np.where(qaqc > 0) y_axis[ind].plot(xdata[bad_data], ydata[bad_data], marker='o', mfc='none', linestyle='None', markersize=6, markeredgewidth=2, mec='r') # Label the y-axis and set text color: # Been experimenting with other ways to handle tick labels with spines y_axis[ind].yaxis.get_major_formatter().set_useOffset(False) y_axis[ind].set_ylabel(ylabel.replace("_", " "), labelpad=10, **axis_font) y_axis[ind].yaxis.label.set_color(colors[ind]) y_axis[ind].spines[spine_directions[ind]].set_color(colors[ind]) if tick_font: labelsize = tick_font['labelsize'] y_axis[ind].tick_params(axis='y', labelsize=labelsize, colors=colors[ind]) legend_handles.append(h) legend_labels.append(data['title'][0:20]) self.get_time_label(ax, xdata) fig.autofmt_xdate() ax.legend(legend_handles, legend_labels) # ax.tick_params(axis='x', labelsize=10) # ax.set_title(title.replace("_", " "), y=1.05, **title_font) ax.grid(True) plt.tight_layout()
def plot_multiple_yaxes(self, fig, ax, xdata, ydata, colors, title, units=[], scatter=False, axis_font={}, title_font={}, tick_font={}, width_in=8.3, qaqc={}, **kwargs): # Plot a timeseries with multiple y-axes # # ydata is a python dictionary of all the data to plot. Key values are used as plot labels # # Acknowledgment: This function is based on code written by Jae-Joon Lee, # URL= http://matplotlib.svn.sourceforge.net/viewvc/matplotlib/trunk/matplotlib/ # examples/pylab_examples/multiple_yaxis_with_spines.py?revision=7908&view=markup # # http://matplotlib.org/examples/axes_grid/demo_parasite_axes2.html if not axis_font: axis_font = axis_font_default if not title_font: title_font = title_font_default n_vars = len(ydata) if n_vars > 6: raise Exception('This code currently handles a maximum of 6 independent variables.') elif n_vars < 2: raise Exception('This code currently handles a minimum of 2 independent variables.') if scatter: kwargs['marker'] = 'o' # Generate the plot. # Use twinx() to create extra axes for all dependent variables except the first # (we get the first as part of the ax axes). y_axis = n_vars * [0] y_axis[0] = ax for i in range(1, n_vars): y_axis[i] = ax.twinx() ax.spines["top"].set_visible(False) self.make_patch_spines_invisible(y_axis[1]) self.set_spine_direction(y_axis[1], "top") # Define the axes position offsets for each 'extra' axis spine_directions = ["left", "right", "left", "right", "left", "right"] # Adjust the axes left/right accordingly if n_vars >= 4: if width_in < 8.3: # set axis location offset = [1.2, -0.2, 1.40, -0.40] # overwrite width l_mod = 0.3 r_mod = 0.8 else: offset = [1.10, -0.10, 1.20, -0.20] l_mod = 0.5 r_mod = 1.2 plt.subplots_adjust(left=l_mod, right=r_mod) elif n_vars == 3: offset = [1.20, -0.20, 1.40, -0.40] plt.subplots_adjust(left=0.0, right=0.7) count = 0 for i in range(2, n_vars): y_axis[i].spines[spine_directions[count+1]].set_position(("axes", offset[count])) self.make_patch_spines_invisible(y_axis[i]) self.set_spine_direction(y_axis[i], spine_directions[count+1]) count += 1 # Plot the data for ind, key in enumerate(ydata): y_axis[ind].plot(xdata[key], ydata[key], colors[ind], **kwargs) if len(qaqc[key]) > 0: bad_data = np.where(qaqc[key] > 0) y_axis[ind].plot(xdata[key][bad_data], ydata[key][bad_data], marker='o', mfc='none', linestyle='None', markersize=6, markeredgewidth=2, mec='r') # Label the y-axis and set text color: # Been experimenting with other ways to handle tick labels with spines y_axis[ind].yaxis.get_major_formatter().set_useOffset(False) y_axis[ind].set_ylabel(key.replace("_", " ") + ' (' + units[ind] + ')', labelpad=10, **axis_font) y_axis[ind].yaxis.label.set_color(colors[ind]) y_axis[ind].spines[spine_directions[ind]].set_color(colors[ind]) if tick_font: labelsize = tick_font['labelsize'] y_axis[ind].tick_params(axis='y', labelsize=labelsize, colors=colors[ind]) self.get_time_label(ax, xdata['time']) fig.autofmt_xdate() # ax.tick_params(axis='x', labelsize=10) ax.set_title(title.replace("_", " "), y=1.05, **title_font) ax.grid(True) plt.tight_layout()
def plot_multiple_xaxes(self, ax, xdata, ydata, colors, ylabel='Depth (m)', title='', title_font={}, axis_font={}, tick_font={}, width_in=8.3, **kwargs): # Acknowledgment: This function is based on code written by Jae-Joon Lee, # URL= http://matplotlib.svn.sourceforge.net/viewvc/matplotlib/trunk/matplotlib/ # examples/pylab_examples/multiple_yaxis_with_spines.py?revision=7908&view=markup if not title_font: title_font = title_font_default if not axis_font: axis_font = axis_font_default n_vars = len(xdata) if n_vars > 6: raise Exception('This code currently handles a maximum of 6 independent variables.') elif n_vars < 2: raise Exception('This code currently handles a minimum of 2 independent variables.') # Generate the plot. # Use twiny() to create extra axes for all dependent variables except the first # (we get the first as part of the ax axes). x_axis = n_vars * [0] x_axis[0] = ax for i in range(1, n_vars): x_axis[i] = ax.twiny() ax.spines["top"].set_visible(False) self.make_patch_spines_invisible(x_axis[1]) self.set_spine_direction(x_axis[1], "top") offset = [1.10, -0.1, -0.20, 1.20] spine_directions = ["top", "bottom", "bottom", "top", "top", "bottom"] count = 0 for i in range(2, n_vars): x_axis[i].spines[spine_directions[count]].set_position(("axes", offset[count])) self.make_patch_spines_invisible(x_axis[i]) self.set_spine_direction(x_axis[i], spine_directions[count]) count += 1 # Adjust the axes left/right accordingly if n_vars >= 4: plt.subplots_adjust(bottom=0.2, top=0.8) elif n_vars == 3: plt.subplots_adjust(bottom=0.0, top=0.8) # Label the y-axis: ax.set_ylabel(ylabel, **axis_font) for ind, key in enumerate(xdata): x_axis[ind].plot(xdata[key], ydata, colors[ind], **kwargs) # Label the x-axis and set text color: x_axis[ind].set_xlabel(key.replace("_", " "), **axis_font) x_axis[ind].xaxis.label.set_color(colors[ind]) x_axis[ind].spines[spine_directions[ind]].set_color(colors[ind]) for obj in x_axis[ind].xaxis.get_ticklines(): # `obj` is a matplotlib.lines.Line2D instance obj.set_color(colors[ind]) obj.set_markeredgewidth(2) for obj in x_axis[ind].xaxis.get_ticklabels(): obj.set_color(colors[ind]) ax.invert_yaxis() ax.grid(True) if tick_font: ax.tick_params(**tick_font) ax.set_title(title.replace("_", " "), y=1.23, **title_font) plt.tight_layout()
def plot_multiple_streams(self, fig, ax, datasets, colors, axis_font={}, title_font={}, tick_font={}, width_in=8.3, plot_qaqc=0, scatter=False, **kwargs): # Plot a timeseries with multiple y-axes using multiple streams from uFrame # # Acknowledgment: This function is based on code written by Jae-Joon Lee, # URL= http://matplotlib.svn.sourceforge.net/viewvc/matplotlib/trunk/matplotlib/ # examples/pylab_examples/multiple_yaxis_with_spines.py?revision=7908&view=markup # # http://matplotlib.org/examples/axes_grid/demo_parasite_axes2.html if not axis_font: axis_font = axis_font_default if not title_font: title_font = title_font_default n_vars = len(datasets) if n_vars > 6: raise Exception( 'This code currently handles a maximum of 6 independent variables.' ) elif n_vars < 2: raise Exception( 'This code currently handles a minimum of 2 independent variables.' ) if scatter: kwargs['marker'] = 'o' # Generate the plot. # Use twinx() to create extra axes for all dependent variables except the first # (we get the first as part of the ax axes). y_axis = n_vars * [0] y_axis[0] = ax for i in range(1, n_vars): y_axis[i] = ax.twinx() ax.spines["top"].set_visible(False) self.make_patch_spines_invisible(y_axis[1]) self.set_spine_direction(y_axis[1], "top") # Define the axes position offsets for each 'extra' axis spine_directions = ["left", "right", "left", "right", "left", "right"] # Adjust the axes left/right accordingly if n_vars >= 4: if width_in < 8.3: # set axis location offset = [1.2, -0.2, 1.40, -0.40] # overwrite width l_mod = 0.3 r_mod = 0.8 else: offset = [1.10, -0.10, 1.20, -0.20] l_mod = 0.5 r_mod = 1.2 plt.subplots_adjust(left=l_mod, right=r_mod) elif n_vars == 3: offset = [1.20, -0.20, 1.40, -0.40] plt.subplots_adjust(left=0.0, right=0.7) count = 0 for i in range(2, n_vars): y_axis[i].spines[spine_directions[count + 1]].set_position( ("axes", offset[count])) self.make_patch_spines_invisible(y_axis[i]) self.set_spine_direction(y_axis[i], spine_directions[count + 1]) count += 1 # Plot the data legend_handles = [] legend_labels = [] for ind, data in enumerate(datasets): xlabel = data['x_field'][0] ylabel = data['y_field'][0] xdata = data['x'][xlabel] ydata = data['y'][ylabel] # Handle the QAQC data qaqc = data['qaqc'][ylabel] if plot_qaqc >= 10: # Plot all of the qaqc flags results # qaqc_data = data['qaqc'][ylabel] pass elif plot_qaqc >= 1: # This is a case where the user wants to plot just one of the 9 QAQC tests ind = np.where(qaqc != plot_qaqc) qaqc[ind] = 0 else: qaqc = [] h, = y_axis[ind].plot(xdata, ydata, colors[ind], label=data['title'], **kwargs) if len(qaqc) > 0: bad_data = np.where(qaqc > 0) y_axis[ind].plot(xdata[bad_data], ydata[bad_data], marker='o', mfc='none', linestyle='None', markersize=6, markeredgewidth=2, mec='r') # Label the y-axis and set text color: # Been experimenting with other ways to handle tick labels with spines y_axis[ind].yaxis.get_major_formatter().set_useOffset(False) y_axis[ind].set_ylabel(ylabel.replace("_", " "), labelpad=10, **axis_font) y_axis[ind].yaxis.label.set_color(colors[ind]) y_axis[ind].spines[spine_directions[ind]].set_color(colors[ind]) if tick_font: labelsize = tick_font['labelsize'] y_axis[ind].tick_params(axis='y', labelsize=labelsize, colors=colors[ind]) legend_handles.append(h) legend_labels.append(data['title'][0:20]) self.get_time_label(ax, xdata) fig.autofmt_xdate() ax.legend(legend_handles, legend_labels) # ax.tick_params(axis='x', labelsize=10) # ax.set_title(title.replace("_", " "), y=1.05, **title_font) ax.grid(True) plt.tight_layout()
def plot_multiple_yaxes(self, fig, ax, xdata, ydata, colors, title, units=[], scatter=False, axis_font={}, title_font={}, tick_font={}, width_in=8.3, qaqc={}, **kwargs): # Plot a timeseries with multiple y-axes # # ydata is a python dictionary of all the data to plot. Key values are used as plot labels # # Acknowledgment: This function is based on code written by Jae-Joon Lee, # URL= http://matplotlib.svn.sourceforge.net/viewvc/matplotlib/trunk/matplotlib/ # examples/pylab_examples/multiple_yaxis_with_spines.py?revision=7908&view=markup # # http://matplotlib.org/examples/axes_grid/demo_parasite_axes2.html if not axis_font: axis_font = axis_font_default if not title_font: title_font = title_font_default n_vars = len(ydata) if n_vars > 6: raise Exception( 'This code currently handles a maximum of 6 independent variables.' ) elif n_vars < 2: raise Exception( 'This code currently handles a minimum of 2 independent variables.' ) if scatter: kwargs['marker'] = 'o' # Generate the plot. # Use twinx() to create extra axes for all dependent variables except the first # (we get the first as part of the ax axes). y_axis = n_vars * [0] y_axis[0] = ax for i in range(1, n_vars): y_axis[i] = ax.twinx() ax.spines["top"].set_visible(False) self.make_patch_spines_invisible(y_axis[1]) self.set_spine_direction(y_axis[1], "top") # Define the axes position offsets for each 'extra' axis spine_directions = ["left", "right", "left", "right", "left", "right"] # Adjust the axes left/right accordingly if n_vars >= 4: if width_in < 8.3: # set axis location offset = [1.2, -0.2, 1.40, -0.40] # overwrite width l_mod = 0.3 r_mod = 0.8 else: offset = [1.10, -0.10, 1.20, -0.20] l_mod = 0.5 r_mod = 1.2 plt.subplots_adjust(left=l_mod, right=r_mod) elif n_vars == 3: offset = [1.20, -0.20, 1.40, -0.40] plt.subplots_adjust(left=0.0, right=0.7) count = 0 for i in range(2, n_vars): y_axis[i].spines[spine_directions[count + 1]].set_position( ("axes", offset[count])) self.make_patch_spines_invisible(y_axis[i]) self.set_spine_direction(y_axis[i], spine_directions[count + 1]) count += 1 # Plot the data for ind, key in enumerate(ydata): y_axis[ind].plot(xdata[key], ydata[key], colors[ind], **kwargs) if len(qaqc[key]) > 0: bad_data = np.where(qaqc[key] > 0) y_axis[ind].plot(xdata[key][bad_data], ydata[key][bad_data], marker='o', mfc='none', linestyle='None', markersize=6, markeredgewidth=2, mec='r') # Label the y-axis and set text color: # Been experimenting with other ways to handle tick labels with spines y_axis[ind].yaxis.get_major_formatter().set_useOffset(False) y_axis[ind].set_ylabel(key.replace("_", " ") + ' (' + units[ind] + ')', labelpad=10, **axis_font) y_axis[ind].yaxis.label.set_color(colors[ind]) y_axis[ind].spines[spine_directions[ind]].set_color(colors[ind]) if tick_font: labelsize = tick_font['labelsize'] y_axis[ind].tick_params(axis='y', labelsize=labelsize, colors=colors[ind]) self.get_time_label(ax, xdata['time']) fig.autofmt_xdate() # ax.tick_params(axis='x', labelsize=10) ax.set_title(title.replace("_", " "), y=1.05, **title_font) ax.grid(True) plt.tight_layout()
def plot_multiple_xaxes(self, ax, xdata, ydata, colors, ylabel='Depth (m)', title='', title_font={}, axis_font={}, tick_font={}, width_in=8.3, **kwargs): # Acknowledgment: This function is based on code written by Jae-Joon Lee, # URL= http://matplotlib.svn.sourceforge.net/viewvc/matplotlib/trunk/matplotlib/ # examples/pylab_examples/multiple_yaxis_with_spines.py?revision=7908&view=markup if not title_font: title_font = title_font_default if not axis_font: axis_font = axis_font_default n_vars = len(xdata) if n_vars > 6: raise Exception( 'This code currently handles a maximum of 6 independent variables.' ) elif n_vars < 2: raise Exception( 'This code currently handles a minimum of 2 independent variables.' ) # Generate the plot. # Use twiny() to create extra axes for all dependent variables except the first # (we get the first as part of the ax axes). x_axis = n_vars * [0] x_axis[0] = ax for i in range(1, n_vars): x_axis[i] = ax.twiny() ax.spines["top"].set_visible(False) self.make_patch_spines_invisible(x_axis[1]) self.set_spine_direction(x_axis[1], "top") offset = [1.10, -0.1, -0.20, 1.20] spine_directions = ["top", "bottom", "bottom", "top", "top", "bottom"] count = 0 for i in range(2, n_vars): x_axis[i].spines[spine_directions[count]].set_position( ("axes", offset[count])) self.make_patch_spines_invisible(x_axis[i]) self.set_spine_direction(x_axis[i], spine_directions[count]) count += 1 # Adjust the axes left/right accordingly if n_vars >= 4: plt.subplots_adjust(bottom=0.2, top=0.8) elif n_vars == 3: plt.subplots_adjust(bottom=0.0, top=0.8) # Label the y-axis: ax.set_ylabel(ylabel, **axis_font) for ind, key in enumerate(xdata): x_axis[ind].plot(xdata[key], ydata, colors[ind], **kwargs) # Label the x-axis and set text color: x_axis[ind].set_xlabel(key.replace("_", " "), **axis_font) x_axis[ind].xaxis.label.set_color(colors[ind]) x_axis[ind].spines[spine_directions[ind]].set_color(colors[ind]) for obj in x_axis[ind].xaxis.get_ticklines(): # `obj` is a matplotlib.lines.Line2D instance obj.set_color(colors[ind]) obj.set_markeredgewidth(2) for obj in x_axis[ind].xaxis.get_ticklabels(): obj.set_color(colors[ind]) ax.invert_yaxis() ax.grid(True) if tick_font: ax.tick_params(**tick_font) ax.set_title(title.replace("_", " "), y=1.23, **title_font) plt.tight_layout()