diff --git a/opppy/dump_utils.py b/opppy/dump_utils.py index 3f06d64..6695ead 100644 --- a/opppy/dump_utils.py +++ b/opppy/dump_utils.py @@ -123,7 +123,7 @@ def point_value_3d(data, x_key, y_key, z_key, value_key, x_value, y_value, z_val -def data2grid(data, x_key, y_key, value_key, npts=500, method='nearest', log_scale=False): +def data2grid(data, x_key, y_key, value_key, npts=500, method='nearest'): ''' This function takes a 2D data structure from dictionary and creates a 2D grid for each array by interpolating. This is useful for plotting. @@ -146,9 +146,6 @@ def data2grid(data, x_key, y_key, value_key, npts=500, method='nearest', log_sca grid_data = {} value = data[value_key] grid_data[value_key] = griddata((X, Y), value, (xi, yi), method).T - if(log_scale): - grid_data[value_key] = [[0.0 if val<=0.0 else math.log10(val) for val in vals] for vals in - grid_data[value_key]] grid_data[x_key] = xi grid_data[y_key] = yi @@ -156,8 +153,7 @@ def data2grid(data, x_key, y_key, value_key, npts=500, method='nearest', log_sca -def data2gridbox(data, x_key, y_key, value_key, xmin, ymin, xmax, ymax,npts=500, method='nearest', - log_scale=False): +def data2gridbox(data, x_key, y_key, value_key, xmin, ymin, xmax, ymax,npts=500, method='nearest'): ''' This function takes a 2D data structure from a data dictionary and creates a 2D grid for each array by interpolating in a user defined region. @@ -186,9 +182,6 @@ def data2gridbox(data, x_key, y_key, value_key, xmin, ymin, xmax, ymax,npts=500, grid_data = {} value = data[value_key] grid_data[value_key] = griddata((X, Y), value, (xi, yi), method).T - if(log_scale): - grid_data[value_key] = [[0.0 if val<=0.0 else math.log10(val) for val in vals] for vals in - grid_data[value_key]] grid_data[x_key] = xi grid_data[y_key] = yi @@ -196,8 +189,7 @@ def data2gridbox(data, x_key, y_key, value_key, xmin, ymin, xmax, ymax,npts=500, -def data2grid3Dslice(data, x_key, y_key, z_key, value_key, z_slice_value, npts=500,method='nearest', - log_scale=False): +def data2grid3Dslice(data, x_key, y_key, z_key, value_key, z_slice_value, npts=500,method='nearest'): ''' This function takes a 3D data structure from a data dictionary and creates a 2D grid for each array by interpolating. This is useful for plotting. @@ -222,9 +214,6 @@ def data2grid3Dslice(data, x_key, y_key, z_key, value_key, z_slice_value, npts=5 grid_data = {} V = data[value_key] grid_data[value_key] = griddata((X, Y, Z), V, (xi, yi, zi), method).T[0] - if(log_scale): - grid_data[value_key] = [[0.0 if val<=0.0 else math.log10(val) for val in vals] for vals in - grid_data[value_key]] grid_data[x_key] = xi.T[0] grid_data[y_key] = yi.T[0] @@ -418,8 +407,7 @@ def extract_series_line(data_list,series_key,value_key,dim_keys,point0_values,po return t, grid -def extract_series_2d(data_list, series_key, value_key, dim_keys, npts=500, method='nearest', - log_scale=False, box=[]): +def extract_series_2d(data_list, series_key, value_key, dim_keys, npts=500, method='nearest', box=[]): ''' This function extracts the data values along a specified line from a series of data dictionaries. @@ -448,10 +436,10 @@ def extract_series_2d(data_list, series_key, value_key, dim_keys, npts=500, meth data[value_key].shape[0] == data[dim_keys[1]].shape[0]): grid.append(data) else: - grid.append(data2grid(data, dim_keys[0], dim_keys[1], value_key, npts, method, log_scale)) + grid.append(data2grid(data, dim_keys[0], dim_keys[1], value_key, npts, method)) else: grid.append(data2gridbox(data, dim_keys[0], dim_keys[1], value_key, box[0], box[1], - box[2], box[3],npts,method, log_scale)) + box[2], box[3],npts,method)) t = {} t[series_key] = array(T) @@ -459,7 +447,7 @@ def extract_series_2d(data_list, series_key, value_key, dim_keys, npts=500, meth return t, grid def extract_series_2d_slice(data_list,series_key,value_key,dim_keys, slice_value, npts=500, - method='nearest', log_scale=False): + method='nearest'): ''' This function extracts the data values along a specified line from a series of data dictionaries. @@ -487,7 +475,7 @@ def extract_series_2d_slice(data_list,series_key,value_key,dim_keys, slice_value sys.exit(0) grid.append(data2grid3Dslice(data, dim_keys[0], dim_keys[1], dim_keys[2],value_key, - slice_value, npts, method, log_scale)) + slice_value, npts, method)) t = {} t[series_key] = array(T) diff --git a/opppy/interactive_utils.py b/opppy/interactive_utils.py index 2602ce2..544210b 100644 --- a/opppy/interactive_utils.py +++ b/opppy/interactive_utils.py @@ -320,7 +320,7 @@ def plot_output(self, args): try: fig = figure(figsize=(xsize,ysize)) except: - PyPloter.switch_backend('agg') + plt.switch_backend('agg') fig = figure(figsize=(xsize,ysize)) xlog_flag = 0 @@ -863,13 +863,13 @@ def plot_series_contour(self, args): sys.exit(0) tracer_t, tracer_grid = extract_series_2d_slice(dictionary_list, args.series_key, args.data_name, args.dimension_keys, args.z_slice_location, - args.number_of_points, args.interpolation_method, args.log_scale) + args.number_of_points, args.interpolation_method) else: if len(args.dimension_keys) != 2: print('Error: z_slice_location specified is not specified so length of dimension_keys must be 2') tracer_t, tracer_grid = extract_series_2d(dictionary_list, args.series_key, args.data_name, args.dimension_keys, args.number_of_points, - args.interpolation_method, args.log_scale) + args.interpolation_method) series_data = series_pair(tracer_t, tracer_grid) elif args.pickle_file is not None: dictionary = pickle.load(open(args.pickle_file,'rb')) @@ -885,13 +885,13 @@ def plot_series_contour(self, args): sys.exit(0) tracer_t, tracer_grid = extract_series_2d_slice(dictionary_list, args.series_key, args.data_name, args.dimension_keys, args.z_slice_location, - args.number_of_points, args.interpolation_method, args.log_scale) + args.number_of_points, args.interpolation_method) else: if len(args.dimension_keys) != 2: print('Error: z_slice_location specified is not specified so length of dimension_keys must be 2') tracer_t, tracer_grid = extract_series_2d(dictionary_list, args.series_key, args.data_name, args.dimension_keys, args.number_of_points, - args.interpolation_method, args.log_scale) + args.interpolation_method) series_data = series_pair(tracer_t, tracer_grid) if args.case_file is not None: dictionary_list = build_case_data_list(args.case_file, None, self.dump_parser, args.key_words) @@ -903,13 +903,13 @@ def plot_series_contour(self, args): sys.exit(0) tracer_t, tracer_grid = extract_series_2d_slice(dictionary_list, args.series_key, args.data_name, args.dimension_keys, args.z_slice_location, - args.number_of_points, args.interpolation_method, args.log_scale) + args.number_of_points, args.interpolation_method) else: if len(args.dimension_keys) != 2: print('Error: z_slice_location specified is not specified so length of dimension_keys must be 2') tracer_t, tracer_grid = extract_series_2d(dictionary_list, args.series_key, args.data_name, args.dimension_keys, args.number_of_points, - args.interpolation_method, args.log_scale) + args.interpolation_method) series_data = series_pair(tracer_t, tracer_grid) args.x_value_name= args.dimension_keys[0] @@ -1181,7 +1181,7 @@ def plot_interactive_tally(self, args): try: fig = figure(figsize=(xsize,ysize)) except: - PyPloter.switch_backend('agg') + plt.switch_backend('agg') fig = figure(figsize=(xsize,ysize)) xlog_flag = 0 diff --git a/opppy/plot_dictionary.py b/opppy/plot_dictionary.py index fcb31a1..09ffec7 100644 --- a/opppy/plot_dictionary.py +++ b/opppy/plot_dictionary.py @@ -13,7 +13,7 @@ plot_dictionary ''' -import matplotlib.pyplot as PyPloter +import matplotlib.pyplot as plt import matplotlib.axes as axes import string, sys, os import numpy as np @@ -25,6 +25,10 @@ from opppy.plotting_help import * +# Remove empty legend box on all plots +plt.rcParams['legend.frameon']=False +plt.rcParams['legend.edgecolor']='none' + class plot_dictionary(): ''' This class encapsolates a basic dictionary plotter. The important class @@ -95,7 +99,7 @@ def plot_dict(self, args, dictionaries, data_names): data_names names associated with the dictionaries to be plotted ''' if(args.hide_plot): - PyPloter.switch_backend('agg') + plt.switch_backend('agg') if len(dictionaries) is not len(data_names): print("Error: len of dictionaries do not match length of associated data names") @@ -209,19 +213,19 @@ def plot_dict(self, args, dictionaries, data_names): last_y.append(y[-1]) if(args.font_size is not None): - PyPloter.rcParams.update({'font_size':args.font_size}) + plt.rcParams.update({'font_size':args.font_size}) if(args.plot_max): last_y.append(sum(sorted(y,reverse=True)[0:2])/3.0) last_x.append(x[-1]) elif(data_line_color != '' and data_line_type != ''): - PyPloter.plot(x,y,label = data_name, linestyle = data_line_type, color = data_line_color) + plt.plot(x,y,label = data_name, linestyle = data_line_type, color = data_line_color) elif(data_line_color != '' ): - PyPloter.plot(x,y,label = data_name, color = data_line_color) + plt.plot(x,y,label = data_name, color = data_line_color) elif(data_line_type != '' ): - PyPloter.plot(x,y,label = data_name, linestyle = data_line_type) + plt.plot(x,y,label = data_name, linestyle = data_line_type) else: - PyPloter.plot(x,y,label = data_name) + plt.plot(x,y,label = data_name) interp_x = x[0] if(args.plot_arrival): @@ -246,44 +250,44 @@ def plot_dict(self, args, dictionaries, data_names): print(data_name, "max y value ", x[y.index(min(y))], min(y)) if(args.last_point_only): - PyPloter.plot(last_x, last_y, label = data_name) + plt.plot(last_x, last_y, label = data_name) elif(args.plot_arrival and found_arrival): - color=PyPloter.gca().lines[-1].get_color() - PyPloter.plot(interp_x, args.y_exceeds_value, linestyle=None, color=color, marker='o', label = data_name + " exceeds y="+str(args.y_exceeds_value)+" @ x="+str(interp_x)) + color=plt.gca().lines[-1].get_color() + plt.plot(interp_x, args.y_exceeds_value, linestyle=None, color=color, marker='o', label = data_name + " exceeds y="+str(args.y_exceeds_value)+" @ x="+str(interp_x)) elif(args.plot_max): print("max y= "+str(last_y)+" @ x="+str(last_x)) - color=PyPloter.gca().lines[-1].get_color() - PyPloter.plot(last_x, last_y, linestyle=None, color=color, marker='x', label = data_name + " max y="+str(last_y)+" @ x="+str(last_x)) + color=plt.gca().lines[-1].get_color() + plt.plot(last_x, last_y, linestyle=None, color=color, marker='x', label = data_name + " max y="+str(last_y)+" @ x="+str(last_x)) if(args.x_label is not None): - PyPloter.xlabel(args.x_label) + plt.xlabel(args.x_label) else: - PyPloter.xlabel(xname) + plt.xlabel(xname) if(args.x_limits is not None): - PyPloter.xlim(args.x_limits) + plt.xlim(args.x_limits) if(args.y_label is not None): - PyPloter.ylabel(args.y_label) + plt.ylabel(args.y_label) else: - PyPloter.ylabel(dictionary_name) + plt.ylabel(dictionary_name) if(args.y_limits is not None): - PyPloter.ylim(args.y_limits) + plt.ylim(args.y_limits) if(args.plot_grid): - PyPloter.grid() + plt.grid() - PyPloter.legend(loc='best') + plt.legend(loc='best') if(args.log_x): - PyPloter.xscale("log") + plt.xscale("log") if(args.log_y): - PyPloter.yscale("log") + plt.yscale("log") if(args.figure_name is not None): - fig = PyPloter.savefig(args.figure_name, dpi=args.figure_resolution) + fig = plt.savefig(args.figure_name, dpi=args.figure_resolution) print("Plot save as -- "+args.figure_name) elif(not args.hide_plot): warnings.filterwarnings("ignore") - PyPloter.show() + plt.show() diff --git a/opppy/plot_dump_dictionary.py b/opppy/plot_dump_dictionary.py index a9983b2..fe2a584 100644 --- a/opppy/plot_dump_dictionary.py +++ b/opppy/plot_dump_dictionary.py @@ -15,8 +15,9 @@ plot_3d_dump_dictionary ''' -import matplotlib.pyplot as PyPloter +import matplotlib.pyplot as plt import matplotlib.axes as axes +from matplotlib.colors import LogNorm, SymLogNorm from matplotlib.collections import PatchCollection from matplotlib.patches import Polygon from matplotlib.animation import FuncAnimation @@ -35,6 +36,10 @@ series_pair = namedtuple("pair", ['index', 'grid']) +# Remove empty legend box on all plots +plt.rcParams['legend.frameon']=False +plt.rcParams['legend.edgecolor']='none' + class plot_1d_dump_dictionary(): ''' This class encapsolates a basic dictionary plotter. The important class @@ -99,7 +104,7 @@ def plot_1d(self, args, dictionaries, data_names): data_names names associated with the dictionaries to be plotted ''' if(args.hide_plot): - PyPloter.switch_backend('agg') + plt.switch_backend('agg') if len(dictionaries) is not len(data_names): print("Error: len of dictionaries do not match length of associated data names") @@ -217,13 +222,13 @@ def plot_1d(self, args, dictionaries, data_names): elif(args.plot_arrival): continue elif(data_line_color != '' and data_line_type != ''): - PyPloter.plot(x,y,label = data_name, linestyle = data_line_type, color = data_line_color) + plt.plot(x,y,label = data_name, linestyle = data_line_type, color = data_line_color) elif(data_line_color != '' ): - PyPloter.plot(x,y,label = data_name, color = data_line_color) + plt.plot(x,y,label = data_name, color = data_line_color) elif(data_line_type != '' ): - PyPloter.plot(x,y,label = data_name, linestyle = data_line_type) + plt.plot(x,y,label = data_name, linestyle = data_line_type) else: - PyPloter.plot(x,y,label = data_name) + plt.plot(x,y,label = data_name) if(args.plot_arrival): last_x = x[0] @@ -243,41 +248,41 @@ def plot_1d(self, args, dictionaries, data_names): print(data_name, "max y value ", x[y.index(min(y))], min(y)) if(args.last_point_only): - PyPloter.plot(last_x, last_y, label = data_name) + plt.plot(last_x, last_y, label = data_name) elif(args.plot_arrival or args.plot_max): print(last_x, last_y) - PyPloter.plot(last_x, last_y, label = data_name) + plt.plot(last_x, last_y, label = data_name) if(args.x_label is not None): - PyPloter.xlabel(args.x_label) + plt.xlabel(args.x_label) else: - PyPloter.xlabel(xname) + plt.xlabel(xname) if(args.x_limits is not None): - PyPloter.xlim(args.x_limits) + plt.xlim(args.x_limits) if(args.y_label is not None): - PyPloter.ylabel(args.y_label) + plt.ylabel(args.y_label) else: - PyPloter.ylabel(yname) + plt.ylabel(yname) if(args.y_limits is not None): - PyPloter.ylim(args.y_limits) + plt.ylim(args.y_limits) if(args.plot_grid): - PyPloter.grid() + plt.grid() - PyPloter.legend(loc='best') + plt.legend(loc='best') if(args.log_x): - PyPloter.xscale("log") + plt.xscale("log") if(args.log_y): - PyPloter.yscale("log") + plt.yscale("log") if(args.figure_name is not None): - fig = PyPloter.savefig(args.figure_name, dpi=args.figure_resolution) + fig = plt.savefig(args.figure_name, dpi=args.figure_resolution) print("Plot save as -- "+args.figure_name) elif(not args.hide_plot): warnings.filterwarnings("ignore") - PyPloter.show() + plt.show() class plot_2d_dump_dictionary(): ''' @@ -348,7 +353,7 @@ def plot_2d(self, args, dictionary): dictionaries a list of dictionaries to be plotted ''' if(args.hide_plot): - PyPloter.switch_backend('agg') + plt.switch_backend('agg') data_name = args.data_name yname = args.y_value_name @@ -357,9 +362,8 @@ def plot_2d(self, args, dictionary): if(args.data_file_name is not None): outputfile = open(args.data_file_name+'_'+re.sub(r'[^\w]','',data_name)+'.dat', 'w') data = np.array(dictionary[data_name])*args.scale_value - if(args.log_scale): - bias = abs(min(data)); - data = [ log10(val+bias) if val+bias>0.0 else 0.0 for val in data] + vmin = data.min() + vmax = data.max() x = np.array(dictionary[xname])*args.scale_x y = np.array(dictionary[yname])*args.scale_y @@ -386,14 +390,11 @@ def plot_2d(self, args, dictionary): if(args.data_bounds): vmin = args.data_bounds[0] vmax = args.data_bounds[1] - else: - vmin = None - vmax = None if args.xy_verts_name is not None: xy_verts = dictionary[args.xy_verts_name] xy_verts = [ [[xy[0]*args.scale_x,xy[1]*args.scale_y] for xy in verts] for verts in xy_verts] - fig, ax = PyPloter.subplots() + fig, ax = plt.subplots() xmin = None xmax = None ymin = None @@ -430,6 +431,9 @@ def plot_2d(self, args, dictionary): collection = PatchCollection(patches, cmap='jet', snap=True) collection.set_array(np.array(data)) collection.set_clim(vmin,vmax) + if args.log_scale: + collection.set_norm(LogNorm(vmin,vmax) if vmin>0 else + SymLogNorm(linthresh=1.e-3,linscale=1.0,vmin=vmin,vmax=vmax)) if args.show_mesh: collection.set_edgecolors("black") ax.add_collection(collection) @@ -447,7 +451,7 @@ def plot_2d(self, args, dictionary): fig.colorbar(collection, ax=ax) elif(args.contour): - fig, ax = PyPloter.subplots() + fig, ax = plt.subplots() if args.x_limits is None: args.x_limits = [min(x),max(x)] if args.y_limits is None: @@ -455,9 +459,9 @@ def plot_2d(self, args, dictionary): ax.set_xlim(args.x_limits[0], args.x_limits[1]) ax.set_ylim(args.y_limits[0], args.y_limits[1]) ax.set_aspect('equal', adjustable='box') - PyPloter.tricontourf(x,y,data,cmap='jet',levels=args.contour_levels) + plt.tricontourf(x,y,data,cmap='jet',levels=args.contour_levels) elif(args.contour_lines): - fig, ax = PyPloter.subplots() + fig, ax = plt.subplots() if args.x_limits is None: args.x_limits = [min(x),max(x)] if args.y_limits is None: @@ -465,7 +469,7 @@ def plot_2d(self, args, dictionary): ax.set_xlim(args.x_limits[0], args.x_limits[1]) ax.set_ylim(args.y_limits[0], args.y_limits[1]) ax.set_aspect('equal', adjustable='box') - PyPloter.tricontour(x,y,data,cmap='jet',levels=args.contour_levels) + plt.tricontour(x,y,data,cmap='jet',levels=args.contour_levels) else: if args.x_limits is not None or args.y_limits is not None: if args.x_limits is None: @@ -474,7 +478,7 @@ def plot_2d(self, args, dictionary): args.y_limits = [min(y),max(y)] griddata = data2gridbox(dictionary, xname, yname, data_name, args.x_limits[0], args.y_limits[0], args.x_limits[1], args.y_limits[1], args.num_grid, - args.interp_method,args.log_scale) + args.interp_method) else: if(dictionary[data_name].ndim == 2 and dictionary[data_name].shape[1] == dictionary[xname].shape[0] and @@ -482,11 +486,16 @@ def plot_2d(self, args, dictionary): griddata = dictionary else: griddata = data2grid(dictionary, xname, yname, data_name, args.num_grid, - args.interp_method, args.log_scale) + args.interp_method) - PyPloter.imshow(griddata[data_name], vmin=vmin, vmax=vmax, extent=(griddata[xname].min(),griddata[xname].max(),griddata[yname].min(),griddata[yname].max()), origin='lower', cmap='jet') - PyPloter.colorbar() + if args.log_scale: + plt.imshow(griddata[data_name], norm=LogNorm(vmin,vmax) if vmin>0 else + SymLogNorm(linthresh=1.e-3,linscale=1.0,vmin=vmin,vmax=vmax), + extent=(griddata[xname].min(),griddata[xname].max(),griddata[yname].min(),griddata[yname].max()), origin='lower', cmap='jet') + else: + plt.imshow(griddata[data_name], vmin=vmin, vmax=vmax, extent=(griddata[xname].min(),griddata[xname].max(),griddata[yname].min(),griddata[yname].max()), origin='lower', cmap='jet') + plt.colorbar() if(args.find_max_value): @@ -495,30 +504,29 @@ def plot_2d(self, args, dictionary): print(data_name, "min value ", x[y.index(min(data))], x[y.index(min(data))], min(data)) if args.plot_title is not None: - PyPloter.title(bytes(args.plot_title, "utf-8").decode("unicode_escape")) + plt.title(bytes(args.plot_title, "utf-8").decode("unicode_escape")) else: - PyPloter.title(data_name) + plt.title(data_name) if(args.x_label is not None): - PyPloter.xlabel(bytes(args.x_label, "utf-8").decode("unicode_escape")) + plt.xlabel(bytes(args.x_label, "utf-8").decode("unicode_escape")) else: - PyPloter.xlabel(xname) + plt.xlabel(xname) if(args.y_label is not None): - PyPloter.ylabel(bytes(args.y_label, "utf-8").decode("unicode_escape")) + plt.ylabel(bytes(args.y_label, "utf-8").decode("unicode_escape")) else: - PyPloter.ylabel(yname) + plt.ylabel(yname) if(args.plot_grid): - PyPloter.grid() + plt.grid() - PyPloter.legend(loc='best') if(args.figure_name is not None): - fig = PyPloter.savefig(args.figure_name, bbox_inches='tight', dpi=args.figure_resolution) + fig = plt.savefig(args.figure_name, bbox_inches='tight', dpi=args.figure_resolution) print("Plot save as -- "+args.figure_name) elif(not args.hide_plot): warnings.filterwarnings("ignore") - PyPloter.show() + plt.show() class plot_3d_dump_dictionary(): @@ -590,7 +598,7 @@ def plot_3d_slice(self, args, dictionary): dictionaries a list of dictionaries to be plotted ''' if(args.hide_plot): - PyPloter.switch_backend('agg') + plt.switch_backend('agg') data_name = args.data_name xname = args.x_value_name @@ -600,6 +608,8 @@ def plot_3d_slice(self, args, dictionary): if(args.data_file_name is not None): outputfile = open(args.data_file_name+'_'+re.sub(r'[^\w]','',data_name)+'.dat', 'w') data = np.array(dictionary[data_name])*args.scale_value + vmin = data.min() + vmax = data.max() x = np.array(dictionary[xname])*args.scale_x y = np.array(dictionary[yname])*args.scale_y z = np.array(dictionary[zname])*args.scale_z @@ -628,17 +638,19 @@ def plot_3d_slice(self, args, dictionary): outputfile.close() griddata = data2grid3Dslice(dictionary, xname, yname, zname, data_name, args.z_slice, - args.num_grid, args.interp_method, args.log_scale) + args.num_grid, args.interp_method) if(args.data_bounds): vmin = args.data_bounds[0] vmax = args.data_bounds[1] + + if args.log_scale: + plt.imshow(griddata[data_name], norm=LogNorm(vmin,vmax) if vmin>0 else + SymLogNorm(linthresh=1.e-3,linscale=1.0,vmin=vmin,vmax=vmax), + extent=(griddata[xname].min(),griddata[xname].max(),griddata[yname].min(),griddata[yname].max()), origin='lower', cmap='jet') else: - vmin = None - vmax = None - - PyPloter.imshow(griddata[data_name], vmin=vmin,vmax=vmax, extent=(griddata[xname].min(),griddata[xname].max(),griddata[yname].min(),griddata[yname].max()), origin='lower', cmap='jet') - PyPloter.colorbar() + plt.imshow(griddata[data_name], vmin=vmin,vmax=vmax, extent=(griddata[xname].min(),griddata[xname].max(),griddata[yname].min(),griddata[yname].max()), origin='lower', cmap='jet') + plt.colorbar() if(args.find_max_value): print(data_name, "max value ", x[y.index(max(data))], x[y.index(max(data))], max(data)) @@ -646,30 +658,29 @@ def plot_3d_slice(self, args, dictionary): print(data_name, "min value ", x[y.index(min(data))], x[y.index(min(data))], min(data)) if args.plot_title is not None: - PyPloter.title(args.plot_title) + plt.title(args.plot_title) else: - PyPloter.title(data_name) + plt.title(data_name) if(args.x_label is not None): - PyPloter.xlabel(args.x_label) + plt.xlabel(args.x_label) else: - PyPloter.xlabel(xname) + plt.xlabel(xname) if(args.y_label is not None): - PyPloter.ylabel(args.y_label) + plt.ylabel(args.y_label) else: - PyPloter.ylabel(yname) + plt.ylabel(yname) if(args.plot_grid): - PyPloter.grid() + plt.grid() - PyPloter.legend(loc='best') if(args.figure_name is not None): - fig = PyPloter.savefig(args.figure_name, dpi=args.figure_resolution) + fig = plt.savefig(args.figure_name, dpi=args.figure_resolution) print("Plot save as -- "+args.figure_name) elif(not args.hide_plot): warnings.filterwarnings("ignore") - PyPloter.show() + plt.show() class plot_line_series_dictionary(): @@ -735,7 +746,7 @@ def plot_1d_series(self,args, series_pairs, data_names): series_pair a list of 1D series pairs(index, grid) data_names names associated with the dictionaries to be plotted ''' - fig = PyPloter.figure() + fig = plt.figure() def init_lines(): ''' Initial lines @@ -746,7 +757,7 @@ def init_lines(): data_names names associated with the dictionaries to be plotted ''' if(args.hide_plot): - PyPloter.switch_backend('agg') + plt.switch_backend('agg') if len(series_pairs) is not len(data_names): print("Error: len of dictionaries do not match length of associated data names") @@ -816,7 +827,7 @@ def init_lines(): print(yname, "min y value ", ymin) # initialize the figure and axes - axes = PyPloter.axes(xlim=(xmin,xmax), ylim=(ymin,ymax)) + axes = plt.axes(xlim=(xmin,xmax), ylim=(ymin,ymax)) lines = [] # initialize lines data @@ -872,29 +883,29 @@ def init_lines(): lines.append(line) if(args.x_label is not None): - PyPloter.xlabel(args.x_label) + plt.xlabel(args.x_label) else: - PyPloter.xlabel(xname) + plt.xlabel(xname) if(args.x_limits is not None): - PyPloter.xlim(args.x_limits) + plt.xlim(args.x_limits) if(args.y_label is not None): - PyPloter.ylabel(args.y_label) + plt.ylabel(args.y_label) else: - PyPloter.ylabel(yname) + plt.ylabel(yname) if(args.y_limits is not None): - PyPloter.ylim(args.y_limits) + plt.ylim(args.y_limits) if(args.plot_grid): - PyPloter.grid() + plt.grid() - PyPloter.legend(loc='best') + plt.legend(loc='best') if(args.log_x): - PyPloter.xscale("log") + plt.xscale("log") if(args.log_y): - PyPloter.yscale("log") + plt.yscale("log") return lines @@ -910,11 +921,11 @@ def animate(i): ani = FuncAnimation(fig, animate, frames=len(series_pairs[0].grid), blit=True) if(args.figure_name is not None): - ani.save(args.figure_name, fps=30, extra_args=['-vcodec', 'libx264']) + ani.save(args.figure_name, writer="pillow") print("Plot save as -- "+args.figure_name) elif(not args.hide_plot): warnings.filterwarnings("ignore") - PyPloter.show() + plt.show() class plot_2d_series_dictionary(): ''' @@ -987,7 +998,7 @@ def plot_2d_series(self,args, series_pair): series_pair a 2d series pairs(index, grid) data_names names associated with the dictionaries to be plotted ''' - fig = PyPloter.figure() + fig = plt.figure() def init_contour(): ''' Initial lines @@ -998,7 +1009,7 @@ def init_contour(): data_names names associated with the dictionaries to be plotted ''' if(args.hide_plot): - PyPloter.switch_backend('agg') + plt.switch_backend('agg') dname = args.data_name xname = args.x_value_name @@ -1020,11 +1031,6 @@ def init_contour(): bias = 0.0 for data, index_value in zip(series_data, series_pair.index[index_key]): v = np.array(data[dname]) - if(args.log_scale): - bias = v.min() - bias = 0.0 if bias>0.0 else abs(bias) - v = np.array([ [log10(val+bias) if (val+bias)>0.0 else 0.0 - for val in vals] for vals in v]) x = np.array(data[xname]) y = np.array(data[yname]) vmin = np.array([v.min(),vmin]).min() @@ -1068,48 +1074,56 @@ def init_contour(): print(dname, "min value ", vmin) # initialize the figure and axes - axes = PyPloter.axes(xlim=(xmin,xmax), ylim=(ymin,ymax)) + axes = plt.axes(xlim=(xmin,xmax), ylim=(ymin,ymax)) if(args.x_label is not None): - PyPloter.xlabel(args.x_label) + plt.xlabel(args.x_label) else: - PyPloter.xlabel(xname) + plt.xlabel(xname) if(args.x_limits is not None): - PyPloter.xlim(args.x_limits) + plt.xlim(args.x_limits) if(args.y_label is not None): - PyPloter.ylabel(args.y_label) + plt.ylabel(args.y_label) else: - PyPloter.ylabel(yname) + plt.ylabel(yname) if(args.y_limits is not None): - PyPloter.ylim(args.y_limits) + plt.ylim(args.y_limits) if(args.plot_grid): - PyPloter.grid() - - PyPloter.legend(loc='best') + plt.grid() if(args.data_bounds): - vmin = args.data_bounds[0] if not args.log_scale else log10(args.data_bounds[0]+bias) - vmax = args.data_bounds[1] if not args.log_scale else log10(args.data_bounds[1]+bias) + vmin = args.data_bounds[0] + vmax = args.data_bounds[1] - imshow = PyPloter.imshow(series_pair.grid[0][dname], extent=(xmin,xmax,ymin,ymax), vmin=vmin, vmax=vmax, origin='lower', animated=True, cmap='jet') - PyPloter.colorbar() + if args.log_scale: + imshow = plt.imshow(series_pair.grid[0][dname], extent=(xmin,xmax,ymin,ymax), + norm = LogNorm(vmin,vmax) if vmin>0 else + SymLogNorm(linthresh=1.e-3,linscale=1.0,vmin=vmin,vmax=vmax), origin='lower', animated=True, cmap='jet') + else: + imshow = plt.imshow(series_pair.grid[0][dname], extent=(xmin,xmax,ymin,ymax), vmin=vmin, vmax=vmax, origin='lower', animated=True, cmap='jet') + plt.colorbar() return imshow, xmin, xmax, ymin, ymax, vmin, vmax imshow, xmin, xmax, ymin, ymax, vmin, vmax = init_contour() ims = [] for data in series_pair.grid: - ims.append([PyPloter.imshow(data[args.data_name], extent=(xmin,xmax,ymin,ymax), vmin=vmin, vmax=vmax, origin='lower', animated=True, cmap='jet')]) + if args.log_scale: + ims.append([plt.imshow(data[args.data_name], extent=(xmin,xmax,ymin,ymax), + norm=LogNorm(vmin,vmax) if vmin>0 else + SymLogNorm(linthresh=1.e-3,linscale=1.0,vmin=vmin,vmax=vmax), origin='lower', animated=True, cmap='jet')]) + else: + ims.append([plt.imshow(data[args.data_name], extent=(xmin,xmax,ymin,ymax), vmin=vmin, vmax=vmax, origin='lower', animated=True, cmap='jet')]) ani = ArtistAnimation(fig, ims, interval=200, blit=True) if(args.figure_name is not None): - ani.save(args.figure_name, fps=30, extra_args=['-vcodec', 'libx264']) + ani.save(args.figure_name, writer="pillow") print("Plot save as -- "+args.figure_name) elif(not args.hide_plot): warnings.filterwarnings("ignore") - PyPloter.show() + plt.show() diff --git a/opppy/plotting_help.py b/opppy/plotting_help.py index 4eff7c5..bfab505 100644 --- a/opppy/plotting_help.py +++ b/opppy/plotting_help.py @@ -28,10 +28,13 @@ import sys import math import numpy as np -import matplotlib.pyplot as PyPloter +import matplotlib.pyplot as plt import re import ast +# Remove empty legend box on all plots +plt.rcParams['legend.frameon']=False +plt.rcParams['legend.edgecolor']='none' #################################################### # HELPER FUNCTIONS @@ -250,14 +253,14 @@ def logplot(xlog,ylog,x,y,label): ''' if xlog == 1: if ylog == 1: - PyPloter.loglog(x,y,label=label) + plt.loglog(x,y,label=label) else: - PyPloter.semilogx(x,y,label=label) + plt.semilogx(x,y,label=label) else: if ylog == 1: - PyPloter.semilogy(x,y,label=label) + plt.semilogy(x,y,label=label) else: - PyPloter.plot(x,y,label=label) + plt.plot(x,y,label=label) def add_plot_options(parser): ''' diff --git a/tests/test_dump_ploter.py b/tests/test_dump_ploter.py index 1975ebb..e22ca92 100644 --- a/tests/test_dump_ploter.py +++ b/tests/test_dump_ploter.py @@ -257,8 +257,7 @@ def test_contour_series_plot(self): names.append(filename) data.append(dump_parser.build_data_dictionary(filename)) - tracer_t, tracer_grid = extract_series_2d(data,'time',"temperature",['x','y'], npts=5, - log_scale=True) + tracer_t, tracer_grid = extract_series_2d(data,'time',"temperature",['x','y'], npts=5) series_data = series_pair(tracer_t, tracer_grid) contour_series_ploter = plot_2d_series_dictionary(); @@ -272,7 +271,7 @@ def test_contour_series_plot(self): contour_series_ploter.plot_2d_series(args, series_data) tracer_t, tracer_grid = extract_series_2d_slice(data,'time',"temperature",['z','y','x'], - 5.0, npts=10, log_scale=True ) + 5.0, npts=10) series_data = series_pair(tracer_t, tracer_grid) plot_string = '-x z -y y -d temperature -xlab test_x'