def plot(self, show_minima=False): #draw the minima as points import matplotlib.pyplot as plt fig = plt.figure(figsize=(6,7)) fig.set_facecolor('white') ax = fig.add_subplot(111, adjustable='box') ax.tick_params(axis='y', direction='out') ax.yaxis.tick_left() ax.spines['left'].set_color('black') ax.spines['left'].set_linewidth(0.5) ax.spines['top'].set_color('none') ax.spines['bottom'].set_color('none') ax.spines['right'].set_color('none') leaves = self.tree_graph.get_leaves() energies = [leaf.data["minimum"].energy for leaf in leaves] xpos = [leaf.data["x"] for leaf in leaves] if show_minima: ax.plot(xpos, energies, 'o') #draw the line segemnts for x, y in self.line_segments: ax.plot(x, y, 'k', linewidth=0.5) plt.xticks([]) plt.box(on=True)
def establishAxes( fig, categories, options, data ): axDict = {} data.backgroundAx = fig.add_axes( [ 0.0, 0.0, 1.0, 1.0 ] ) data.backgroundAx.yaxis.set_major_locator( pylab.NullLocator() ) data.backgroundAx.xaxis.set_major_locator( pylab.NullLocator() ) plt.box( on=False ) options.axLeft = 0.01 options.axRight = 0.99 options.width = options.axRight - options.axLeft options.axBottom = 0.1 options.axTop = 0.85 options.axHeight = options.axTop - options.axBottom margin = 0.017 width = ( options.width - ( len(categories) - 1 ) * margin ) / len(categories) xpos = options.axLeft sortedOrder = categories.keys() sortedOrder.sort() options.axDictSortedOrder = sortedOrder for c in sortedOrder: axDict[ c ] = fig.add_axes( [ xpos, options.axBottom, width, options.axHeight ] ) axDict[ c ].yaxis.set_major_locator( pylab.NullLocator() ) axDict[ c ].xaxis.set_major_locator( pylab.NullLocator() ) xpos += width + margin plt.box( on=False ) data.axDict = axDict return ( axDict )
def plot_bars(d_bar, d_err, title, keys, width = 0.1, ind = None, sp_num = None, colors = None, share_ax = None): sp_num = sp_num if sp_num else 111 ind = ind if ind else 1 colors = colors if colors else ['b','g','r','c','m','y'] if share_ax is None: ax = pyplot.subplot(sp_num) else: ax = pyplot.subplot(sp_num, sharey = share_ax, sharex = share_ax) n_mods = len(keys) offset = -width * (n_mods / 2) for i, mod in enumerate(keys): ax.bar(ind + offset + (width*i), d_bar[mod], width = width / 2., align = 'center', yerr = d_err[mod] if d_err else None, ecolor = 'k', color = colors[i % len(colors)]) #ax.autoscale(tight=False) pyplot.xticks([]) pyplot.box(on = False) pyplot.title(title, fontsize = 20, va='baseline') pyplot.legend(keys) return ax
def body_plot(Edge, Body): global n_fig # Determine if the output directory exists. If not, create the directory. if not os.path.exists('./movies'): os.makedirs('./movies') figure = plt.figure(1) figure.add_subplot(1, 1, 1, axisbg='1') # Change background color here plt.gca().set_aspect('equal') plt.gca().axes.get_xaxis().set_visible(False) plt.gca().axes.get_yaxis().set_visible(False) plt.box(on='off') # plt.plot(Body.AF.x_col[:Body.N/2], Body.cp[:Body.N/2]/100, 'g') # plt.plot(Body.AF.x_col[Body.N/2:], Body.cp[Body.N/2:]/100, 'b') plt.plot(Body.AF.x, Body.AF.z, 'k') plt.xlim((np.min(Body.AF.x)-0.125, np.min(Body.AF.x)+0.125)) plt.plot(Edge.x, Edge.z, 'g') plt.ylim((-0.05, 0.05)) figure.savefig('./movies/%05i.png' % (n_fig), format='png') plt.clf() n_fig += 1
def setAxes( fig, refCategories, options ): axDict = {} #Background axes: axDict[ 'bg' ] = fig.add_axes( [ 0.0, 0.0, 1.0, 1.0 ] ) axDict[ 'bg' ].xaxis.set_major_locator( pylab.NullLocator() ) axDict[ 'bg' ].yaxis.set_major_locator( pylab.NullLocator() ) pyplot.box( on=False ) #Set axes for the categories plots options.axleft = 0.01 options.axright = 0.99 options.width = options.axright - options.axleft options.axbottom = 0.1 options.axtop = 0.85 options.axheight = options.axtop - options.axbottom margin = 0.05 w = ( options.width - margin*(len(refCategories) - 1) )/len(refCategories) xpos = options.axleft refCategories.sort() options.sortedAxesNames = refCategories for c in refCategories: axDict[ c ] = fig.add_axes( [ xpos, options.axbottom, w, options.axheight ] ) axDict[ c ].xaxis.set_major_locator( pylab.NullLocator() ) axDict[ c ].yaxis.set_major_locator( pylab.NullLocator() ) xpos += w + margin pyplot.box( on = False ) return axDict
def plot2DGrid(scores, paramsToPlot, keysToPlot, scoreLabel, vrange): """ Plots a heatmap of scores, over the paramsToPlot :param scores: A list of scores, estimated using parallelizeScore :param paramsToPlot: The parameters to plot, chosen automatically by plotScores :param scoreLabel: The specified score label (dependent on scoring metric used) :param vrange: The visible range of the heatmap (range you wish the heatmap to be specified over) """ scoreGrid = np.reshape( scores, (len(paramsToPlot[keysToPlot[0]]), len(paramsToPlot[keysToPlot[1]]))) plt.figure(figsize=(int(round(len(paramsToPlot[keysToPlot[1]]) / 1.33)), int( round(len(paramsToPlot[keysToPlot[0]]) / 1.33)))) if vrange is not None: plt.imshow(scoreGrid, cmap='jet', vmin=vrange[0], vmax=vrange[1]) else: plt.imshow(scoreGrid, cmap='jet') plt.xlabel(keysToPlot[1]) plt.xticks( np.arange(len(paramsToPlot[keysToPlot[1]])), paramsToPlot[keysToPlot[1]]) plt.ylabel(keysToPlot[0]) plt.yticks( np.arange(len(paramsToPlot[keysToPlot[0]])), paramsToPlot[keysToPlot[0]]) if scoreLabel is not None: plt.title(scoreLabel) else: plt.title('Score') plt.colorbar() plt.box(on=False) plt.show()
def plot_pos_cm(rx, ry, sx, sy, M, S, j=0): """Plots the solved positions of the system at any time around the system's center of mass Parameters ---------- rx: array, x postitions of all stars ry: array, y postitions of all stars sx: array, x postitions of S sy: array, y postitions of S M,S: float, respective mass of galactic neucli j: float, time at which you are plotting Returns ------- Scatter plot of the system at any given time. """ plt.figure(figsize=(6,6)) plt.scatter(0,0,color= 'k', label= 'CM', marker = "+", s= 100) plt.scatter((sx[j]-((M*sx[j])/(M+S))), (sy[j]-((M*sy[j])/(M+S))), color = 'b',label ='S') plt.scatter((rx[j]-((S*sx[j])/(M+S))),(ry[j]-((S*sy[j])/(M+S))),color='g',marker="*", label='stars', s=6) plt.scatter((-(M*sx[j])/(M+S)),(-(M*sy[j])/(M+S)), color = 'c', label = "M") plt.legend(loc='upper left') plt.xlim(-65,65) plt.ylim(-65,65) plt.box(False) plt.tick_params(axis = 'x', top = 'off', bottom = "off", labelbottom= 'off') plt.tick_params(axis = 'y', right = 'off', left= "off", labelleft= 'off') plt.show()
def test32(self): '''testing the vanishing plots by having fixed plots of 0 some plants tried to test other class imports realized they don't help at all inital ideas have been asteriked out''' #http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure #http://matplotlib.org/api/backend_bases_api.html?highlight=interactive #import matplotlib.figure as Fig #ok = Fig.Figure(figsize=(3,3), frameon=True, tight_layout=True) fig = plt.figure(1) ax1 = fig.add_subplot(1,1,1) # or plt.subplot(1,1,1) plt.box('on') # makes the white box in the background ax2 = ax1.twinx() #ax2.ylim(-1,1) #plt.figure = (figsize=(8,6), dpi=80) #ax1 = plt.subplot(1,1,1) #plt.autoscale(enable = True, axis = 'x', tight = True) #plt.axes(axisbg = 'w',sharex) c=[] def graph(n_d, n_d1, n_d2): xval = n_d[-50:] ax1.cla() ax1.plot(xval, n_d1[-50:],"-m") ax2.cla() ax2.plot( xval, n_d2[-50:], "-r" ) #plt.ginput(n=1, timeout = 10, show_clicks=True, mouse_stop=2) plt.draw() f = open("xxx.csv", "r") for line in f: b= line.strip().split(',') c.append( (b[2], b[5], b[6]) ) e = array(c) graph( e[:,0], e[:,1], e[:,2] )
def plot_pos(rx, ry, sx, sy, j=0): """Plots the solved positions of the system at any time Parameters ---------- rx: array, x postitions of all stars ry: array, y postitions of all stars sx: array, x postitions of S sy: array, y postitions of S j: float, time at which you are plotting Returns ------- Scatter plot of the system at any given time. """ plt.figure(figsize=(6,6)) plt.scatter(0,0,color= 'c', label= 'M') plt.scatter(sx[j], sy[j], color = 'b', label = 'S') plt.scatter(rx[j], ry[j], color = 'g', marker = "*", label = 'stars', s=6) plt.legend(loc='upper left') plt.xlim(-65,65) plt.ylim(-65,65) plt.box(False) plt.tick_params(axis = 'x', top = 'off', bottom = "off", labelbottom= 'off') plt.tick_params(axis = 'y', right = 'off', left= "off", labelleft= 'off') plt.show()
def sub_retro(rx, ry, sx, sy, times): """Creates a 2 by 3 subplot of the system at 6 chosen times Parameters ---------- rx: array, x postitions of all stars ry: array, y postitions of all stars sx: array, x postitions of S sy: array, y postitions of S times: float, time at which you are plotting Returns ------- 2 by 3 subplot of the system at 6 chosen times """ fig, ax = plt.subplots(2,3, figsize=(12,8)) c=1 for i in range(2): for j in range(3): plt.sca(ax[i,j]) t=times[i,j] plt.scatter(0,0,color= 'c', label= 'M', s=40) plt.scatter(sx[t], sy[t], color = 'b', label = 'S', s=40) plt.scatter(rx[t], ry[t], color = 'g', label = 'stars', s=7) plt.xlabel(c+2, fontsize=15) plt.xlim(-45,45) plt.ylim(-45,45) plt.box(False) plt.tick_params(axis = 'x', top = 'off', bottom = "off", labelbottom= 'off') plt.tick_params(axis = 'y', right = 'off', left= "off", labelleft= 'off') c+=1 plt.tight_layout()
def plot_screen(screen, ax=None): """Plot a captured screenshot Parameters ---------- screen : array The N x M x 3 (or 4) array of screen pixel values. ax : matplotlib Axes | None If provided, the axes will be plotted to and cleared of ticks. If None, a figure will be created. Retruns ------- ax : matplotlib Axes The axes used to plot the image. """ screen = np.array(screen) if screen.ndim != 3 or screen.shape[2] not in [3, 4]: raise ValueError('screen must be a 3D array with 3 or 4 channels') if ax is None: plt.figure() ax = plt.axes([0, 0, 1, 1]) ax.imshow(screen) ax.set_xticks([]) ax.set_yticks([]) plt.box('off') return ax
def draw_network_graph(self, ax, graph_type='dot', p_vals=None, included_variables=[], size=[3.33, 2], resolution=100, cmap=mt.cm.jet, colorbar=True, show_regulation=True, **kwargs): g = GraphGenerator(self) dot_data = g.graph(graph_type=graph_type, p_vals=p_vals, cmap=cmap, included_variables=included_variables, show_regulation=show_regulation) dot_string = dot_data['description'] cmd = Popen(['dot', '-Tpng', '-Gdpi='+str(resolution)], stdin=PIPE, stdout=PIPE, stderr=PIPE) data, err = cmd.communicate(input=dot_string) if len(err) > 0: raise OSError, err if 'limits' in dot_data: if colorbar is True: c_ax,kw=mt.colorbar.make_axes(ax) zlim = dot_data['limits'] self.draw_function_colorbar(c_ax, zlim, cmap) c_ax.set_aspect(15./(zlim[1]-zlim[0])) f=StringIO.StringIO(data) cax=ax.imshow(mt.image.imread(f)) ax.xaxis.visible=False ax.yaxis.visible=False ax.set_xticks([]) ax.set_yticks([]) plt.box(False) f.close()
def draw(self): self._prepare_draw() plt.axis("off") plt.box("off") plt.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0) plt.show()
def preview( coords, topology, cscheme='contour8' ): 'preview function' if topology.ndims == 3: topology = topology.boundary from matplotlib import pyplot, collections if coords.shape[0] == 2: mesh( coords, topology, cscheme=cscheme ) elif coords.shape[0] == 3: polys = [ [] for i in range(4) ] for elem in topology: contour = coords( elem, cscheme ) polys[0].append( project3d( contour ).T ) polys[1].append( contour[:2].T ) polys[2].append( contour[1:].T ) polys[3].append( contour[::2].T ) for iplt, poly in enumerate( polys ): elements = collections.PolyCollection( poly, edgecolors='black', facecolors='none', linewidth=1, rasterized=True ) ax = pyplot.subplot( 2, 2, iplt+1 ) ax.add_collection( elements ) xmin, ymin = numpy.min( [ numpy.min(p,axis=0) for p in poly ], axis=0 ) xmax, ymax = numpy.max( [ numpy.max(p,axis=0) for p in poly ], axis=0 ) d = .02 * (xmax-xmin+ymax-ymin) pyplot.axis([ xmin-d, xmax+d, ymin-d, ymax+d ]) if iplt == 0: ax.get_xaxis().set_visible( False ) ax.get_yaxis().set_visible( False ) pyplot.box( 'off' ) else: pyplot.title( '?ZXY'[iplt] ) else: raise Exception( 'need 2D or 3D coordinates' ) pyplot.show()
def sub_direct(rx, ry, sx, sy, time): """Creates a 2 by 4 subplot of the system at 7 chosen times Parameters ---------- rx: array, x postitions of all stars ry: array, y postitions of all stars sx: array, x postitions of S sy: array, y postitions of S times: float, time at which you are plotting Returns ------- 2 by 4 subplot of the system at 7 chosen times """ c=1 fig, ax = plt.subplots(2,4, figsize=(20,10)) for i in range(2): for j in range(4): plt.sca(ax[i,j]) t = time[i,j] plt.scatter(0,0,color= 'b', label= 'M', s=30) plt.scatter(sx[t], sy[t], color = 'r', label = 'S', s=30) plt.scatter(rx[t], ry[t], color = 'g', label = 'stars', s=5) plt.xlabel(c-3, fontsize=15) plt.xlim(-60,60) plt.ylim(-60,60) plt.box(False) plt.tick_params(axis = 'x', top = 'off', bottom = "off", labelbottom= 'off') plt.tick_params(axis = 'y', right = 'off', left= "off", labelleft= 'off') c+=1 plt.tight_layout()
def show(self, name, **kwargs): im = self.load(name) plt.box("off") plt.xticks([]) plt.yticks([]) plt.imshow(im, **kwargs) plt.show()
def establishAxes( fig, options, data ): """ create one axes per chromosome """ axDict = {} options.axLeft = 0.08 options.axRight = 0.98 options.axWidth = options.axRight - options.axLeft options.axTop = 0.97 options.chrMargin = 0.02 if not ( options.stackFillBlocks or options.stackFillContigPaths or options.stackFillContigs or options.stackFillScaffPaths ): options.axBottom = 0.02 options.axHeight = options.axTop - options.axBottom else: options.axBottom = 0.06 options.axHeight = options.axTop - options.axBottom data.footerAx = fig.add_axes( [ 0.02, 0.01, 0.96, options.axBottom - 0.02] ) if not options.frames: plt.box( on=False ) curXPos = options.axLeft #data.labelAx = fig.add_axes( [ 0.02, options.axBottom, 0.08, options.axHeight] ) #if not options.frames: # plt.box( on=False ) for c in data.chrNames: w = (( data.chrLengthsByChrom[ c ] / float( data.genomeLength ) ) * ( options.axWidth - ( options.chrMargin * float( len( data.chrNames ) - 1) ))) axDict[ c ] = fig.add_axes( [ curXPos, options.axBottom, w , options.axHeight] ) curXPos += w + options.chrMargin if not options.frames: plt.box( on=False ) return ( axDict )
def tweak(): xmin, xmax, ymin, ymax = axis() xpad = 0.1 * (xmax - xmin) ypad = 0.1 * (ymax - ymin) axis([xmin - xpad, xmax + xpad, ymin - ypad, ymax + ypad]) tick_params(length=0) box("off")
def plot_scalp(densities, sensors, sensor_locs=None, plot_sensors=True, plot_contour=True, cmap=None, clim=None, smark='k.', linewidth=2, fontsize=8): if sensor_locs is None: sensor_locs = positions.POS_10_5 if cmap is None: cmap = plt.get_cmap('RdBu_r') # add densities if clim is None: cmax = np.max(np.abs(densities)) clim = [-cmax, cmax] locs = [positions.project_scalp(*sensor_locs[lab]) for lab in sensors] add_density(densities, locs, cmap=cmap, clim=clim, plot_contour=plot_contour) # setup plot MARGIN = 1.2 plt.xlim(-MARGIN, MARGIN) plt.ylim(-MARGIN, MARGIN) plt.box(False) ax = plt.gca() ax.set_aspect(1.2) ax.yaxis.set_ticks([],[]) ax.xaxis.set_ticks([],[]) # add details add_head(linewidth) if plot_sensors: add_sensors(sensors, locs, smark, fontsize)
def plot_large_profile(self, distances, elevations, length, seg_index='all', annotations=None): """ Plot a large sized profile. @param distances: list of distances for the plot @type distances: C{list} of C{float} @param elevations: list of elevations for the plot @type elevations: C{list} of C{float} @param length: overall length of plot @type length: C{float} @param seg_index: index of segment being plotted @type seg_index: C{int} @param annotations: annotations for the plot @type annotations: C{list} of L{Annotation} """ self._set_min_anno_dist(LARGE_WIDTH) plt.figure(1, figsize=(LARGE_WIDTH, LARGE_HEIGHT)) plt.xlabel('Distance (miles)', fontname=FONT_NAME, fontsize=LABEL_FONT_SIZE) plt.ylabel('Elevation (feet)', fontname=FONT_NAME, fontsize=LABEL_FONT_SIZE) if annotations is None: plt.title('Profile', fontname=FONT_NAME, fontsize=LABEL_FONT_SIZE) else: plt.box(False) plt.axhline(y=0.001, color='black') plt.axvline(color='black') self._plot_profile(distances, elevations, length, annotations) self._save_profile(seg_index, 'large')
def plot_connections(connections, sparsity, filename="../../sparseStudies/conn.png"): # code taken from: http://www.astroml.org/book_figures/appendix/fig_neural_network.html fig = plt.figure(facecolor='w') ax = fig.add_axes([0, 0, 1, 1], xticks=[], yticks=[]) plt.box(False) arrow_kwargs = dict(head_width=0.05, fc='black') #------------------------------------------------------------ # which layers should be plotted? layers = connections[:-1] # set layer spacing step = 2 numLayers = len(layers) start = (1 - numLayers)*step/2 end = (numLayers - 1)*step/2 xs = range(start, end + 1, step) xmin = start # set circle radius and minimum spacing between circles radius = 0.1 spacing = 8.0/29 #------------------------------------------------------------ # draw circles ys = [] ymin = 0 for j, layer in enumerate(layers): numNeurons = len(layer) start = (1 - numNeurons)*spacing/2 end = (numNeurons - 1)*spacing/2 if (start < ymin): ymin = start store_y = [] for y in np.arange(start, end + spacing/2, spacing): store_y.append(y) draw_circle(ax, (xs[j], y), radius, plt) ys.append(store_y) #------------------------------------------------------------ # draw connecting arrows for k, y_stored in enumerate(ys[:-1]): for i, y1 in enumerate(y_stored): for j, y2 in enumerate(ys[k+1]): if (connections[k][j][i] == 1): draw_connecting_arrow(ax, (xs[k], y1), radius, (xs[k+1], y2), radius, arrow_kwargs) #------------------------------------------------------------ # Add text labels plt.text(0, -(ymin-1), "Hidden Layers: sparsity = {}".format(sparsity), ha='center', va='top', fontsize=16) ax.set_aspect('equal') plt.xlim((xmin-1), -(xmin-1)) plt.ylim((ymin-2), -(ymin-2)) #plt.show() plt.savefig(filename, bbox_inches='tight')
def hilbert(args, options): if len(args) > 0: data = processFile(args[0], options) else: data = generateDemo(options) fig, pdf = initImage(8, 8, options) ax = establishAxis(fig, options) ax.set_aspect('equal') plt.box(on=False) drawData(ax, data, options) writeImage(fig, pdf, options)
def plotCentroids(time, centroids, stimTimes, time_ephys, ephys, scaled = False, colors = None, xlabel = '',ylabel = '', title = ''): """ Plots centroids resulting from some clustering method Parameters: time - Time vectors for centroids (optical samplign interval) centroids - Array of shape (M, N), where M is the number of centroids, and N is the # number of features (or time points) stimTimes - Times of stimulus onsets for overlaying vertical dashed lines time_ephys - Time axis for ephys data (usually sampled at higher rate) ephys - Ephys time series scaled - Boolean; If true, scales centroids individually, else scales jointly. colors - Array of shape (M,3) or (M,4). Colormap to use for plotting centroids """ import apCode.SignalProcessingTools as spt import seaborn as sns import numpy as np import matplotlib.pyplot as plt if scaled: centroids = spt.standardize(centroids,axis = 1) else: centroids = spt.standardize(centroids) ephys = spt.standardize(ephys) n_clusters = np.shape(centroids)[0] if np.any(colors == None): colors = np.array(sns.color_palette('colorblind',np.shape(centroids)[0])) elif np.shape(colors)[0] < np.shape(centroids)[0]: colors = np.tile(colors,(np.shape(centroids)[0],1)) colors = colors[:np.shape(centroids)[0],:] if np.any(time == None): time = np.arange(np.shape(centroids)[1]) plt.style.use(['dark_background','seaborn-poster']) for cc in np.arange(np.shape(centroids)[0]): plt.plot(time,centroids[cc,:]-np.mean(centroids[cc,:])-cc,color = colors[cc,:]) plt.plot(time_ephys,ephys-np.mean(ephys)-cc-1,color = colors[0,:]) yt = np.arange(n_clusters + 1) ytl = list(yt) ytl[-1] = 'ephys' plt.yticks(-yt, ytl) plt.xlabel(xlabel,fontsize = 16) plt.ylabel(ylabel, fontsize = 16) plt.box('off') plt.title(title, fontsize = 20) plt.grid('off') plt.xlim(time[0],time[-1]) for st in stimTimes: plt.axvline(x = st, ymin = 0, ymax =1, alpha = 0.3, color = 'w',linestyle = '--')
def draftplot(self, **kwargs): """Show a draft plot of the geometries. """ self.ax = plt.subplot(111) plt.box(on=None) if self.data is not None: kwargs['facecolor'] = 'data' kwargs['edgecolor'] = 'data' self.draw_legend((0, 1), integer=False) self.plot(**kwargs) plt.tight_layout() plt.show()
def genericCurve(options): x, y = generateVectors(options.level) fig, pdf = initImage(8, 8, options) ax = establishAxis(fig, options) ax.set_aspect('equal') plt.plot(x, y) ax.set_xlim([-0.5, (1 << options.level) - .5]) ax.set_ylim([-(1 << options.level) + .5, 0.5]) ax.set_xticks([]) ax.set_yticks([]) plt.box(on=False) writeImage(fig, pdf, options)
def formatting_demo(): """Customize the formatting of a simple 1D plot. """ # Plot the targets and raw samples plt.figure() plt.plot(X, Y, 'k-') plt.plot(X[:, None], Yn, 'r.') # We can choose from among several different common line styles: # solid : '-' # dashed: '--' # dash-dot: '-.' # dotted: ':' # And common marker styles: # circle: 'o' # cross: 'x' # plus: '+' # point: '.' # pixel: ',' # See the pyplot documentation for plt.plot() for more line and marker # options. # Set the axis labels and title plt.xlabel("Trial") plt.ylabel("Sample") plt.title("Formatting Demo", fontsize=titlesize) # The tick labels for the x-axis as [2, 4, 6, 8, 10]. This seems # undesirable -- we want them to be 1 through 10. How do we fix this? plt.xticks(X, X) # (tick values, tick labels) # How do you set the axis limits? (This can also be done using plt.xlim ad # plt.ylim) plt.axis(axis2d) # If you want finer resolution for where your points lie, you can turn on # grid lines (see docs for more options here) plt.grid(True) # Turn off grid lines plt.grid(False) # We can also turn on and off the box surrounding the axes. Let's turn them # off and enable grid lines: plt.box(on=False) plt.grid(True) # And turn the box on again: plt.box(on=True) plt.grid(False)
def establishAxis( options, data ): options.axLeft = 0.05 options.axWidth = 0.9 options.axTop = 0.95 options.axHeight = 0.9 options.axRight = options.axLeft + options.axWidth options.axBottom = options.axTop - options.axHeight options.margins = 0.015 data.ax = data.fig.add_axes( [ options.axLeft, options.axBottom, options.axWidth, options.axHeight ] ) data.ax.yaxis.set_major_locator( pylab.NullLocator() ) data.ax.xaxis.set_major_locator( pylab.NullLocator() ) if not options.frames: plt.box( on=False )
def InitVisual(self): ax = plt.gca() fig = plt.gcf() # Add Colorbar cax = fig.add_axes([0.95, 0.2, 0.02, 0.6]) plt.axis('off') cb = mpl.colorbar.ColorbarBase(cax, cmap=self.cmap, spacing='proportional') cb.set_label('Internet Traffic (Q)') plt.sca(ax) # Create Network Skeleton mid = (max([self.m,self.n,self.o]) - 1.)/2. Ix = np.linspace(mid-(self.m-1.)/2.,mid+(self.m-1.)/2.,self.m) Jx = np.linspace(mid-(self.n-1.)/2.,mid+(self.n-1.)/2.,self.n) Kx = np.linspace(mid-(self.o-1.)/2.,mid+(self.o-1.)/2.,self.o) Iy = 2. Jy = 1. Ky = 0. od = [] for i in range(self.m): for j in range(self.n): od.append([(Ix[i],Iy),(Jx[j],Jy)]) for j in range(self.n): for k in range(self.o): od.append([(Jx[j],Jy),(Kx[k],Ky)]) lc = mc.LineCollection(od, colors=(0,0,0,0), linewidths=10) ax.add_collection(lc) ax.set_xlim((0,2*mid)) ax.set_ylim((-.5,2.5)) ax.add_collection(lc) # Annotate Plot plt.box('off') plt.yticks([0,1,2],['Demand\nMarkets', 'Network\nProviders', 'Service\nProviders'], rotation=45) plt.xticks(Kx,['Market\n'+str(k+1) for k in range(self.o)]) plt.tick_params(axis='y',right='off') plt.tick_params(axis='x',top='off') return ax.collections
def test_pyplot_box(): fig, ax = plt.subplots() plt.box(False) assert not ax.get_frame_on() plt.box(True) assert ax.get_frame_on() plt.box() assert not ax.get_frame_on() plt.box() assert ax.get_frame_on()
def qq_plot(real, rand, filename, verbosefunction=False): if len(rand) == 0: if verbosefunction: print ("empty rand list for qq") return "empty" o = open(filename, "w") o.write("rand\t%s\trand_again\n" % filename) limit = min(len(rand), len(real)) real.sort() rand.sort() expected = [] observed = [] for i in range(limit): indexa = int(len(rand) * float(i) / limit) indexb = int(len(real) * float(i) / limit) a = rand[indexa] b = real[indexb] o.write("%s\t%s\t%s\n" % (a, b, a)) expected.append(a) observed.append(b) o.close() # ---------------------------- fig = plt.figure(figsize=(5, 5), dpi=300) ax = plt.axes() # [0,0,1,1], frameon=False) #fig.add_subplot(1,1,1) plt.box("off") # ax.set_title("Q:Q plot %s"%(supplementary_label)) # ax.set_yticklabels(ticklabels, size=12, color="red") #use the list defined above ax.xaxis.set_ticks_position('bottom') # remove ticks from top and right ax.yaxis.set_ticks_position('left') # remove ticks from top and right ax.set_xlabel("Expected coexpression scores", size=12) ax.set_ylabel("Observed coexpression scores", size=12) # ax.set_xscale("log") #use logarithmic x axis # ax.set_yscale("log") #use logarithmic x axis # steps=2 # ax.set_xticks(range(0, max(expected), steps)) # ax.set_yticks(range(0,max(observed), steps)) # ax.set_xticklabels([0,50,100], size=12) for i in range(len(expected)): ax.scatter([expected[i]], [expected[i]], color=["red"], alpha=0.7, marker="o", s=12) ax.scatter([expected[i]], [observed[i]], color=["blue"], alpha=0.7, marker="o", s=12) # axis limits if len(expected) > 0 and len(observed) > 0: plt.xlim(0, max(expected) + 1) plt.ylim(0, max(observed) + 1) fig.savefig("%s.svg" % (filename))
def main(): base_f = "1_out_method1.csv" baseline = np.array(pd.read_csv(base_f,header = None)) q = [] d = [] x = [] t = [] for i in range(1, 9): s=3*(i-1)-1 if(s==-1): s=0 out_file = str(s+1) + "_out_method1.csv" output = np.array(pd.read_csv(out_file,header = None)) queue_error = 0.0 dynamic_error = 0.0 for j in range(len(output)): queue = baseline[j][1] - output[j][1] queue=queue*queue dynamic = baseline[j][2] - output[j][2] dynamic=dynamic*dynamic queue_error = queue_error+queue dynamic_error = dynamic_error+dynamic queue_error = queue_error/len(output) dynamic_error = dynamic_error/len(output) x.append(s+1) q.append(queue_error) d.append(dynamic_error) runtime_file = "runtime_method1.csv" runtime = np.array(pd.read_csv(runtime_file,header=None)) for time in runtime: t.append(time[1]) plt.figure() plt.xlabel("Number of frames skipped") plt.ylabel("Avgerage squared error") plt.plot(x,q,label="Queue Density error",marker='o') plt.plot(x,d,label="Dynamic Density error",marker='o') #for i in range(len(x)): # plt.annotate("("+str(x[i])+","+str(q[i])+")",(x[i],q[i])); # plt.annotate("("+str(x[i])+","+str(d[i])+")",(x[i],d[i])); plt.legend() plt.grid() plt.savefig("plot1.png",dpi=200) plt.show() cell_text=[] for i in range(len(x)): cell_text.append([x[i],q[i],d[i]]) table=plt.table(cellText=cell_text,colLabels=['Frames skipped','Queue Density Error','Dynamic Density Error'],loc='center') ax=plt.gca() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.box(on=None) table.scale(1,1.5) plt.savefig("table1.png",dpi=200) plt.show() plt.figure() plt.xlabel("Number of frames skipped") plt.ylabel("Runtime(seconds)") plt.plot(x,t,marker='o') #for i in range(len(x)): # plt.annotate("("+str(x[i])+","+str(t[i])+")",(x[i],t[i])); plt.grid() plt.savefig("plot2.png",dpi=200) plt.show() cell_text=[] for i in range(len(x)): cell_text.append([x[i],t[i]]) table=plt.table(cellText=cell_text,colLabels=['Number of frames skipped','Runtime(seconds)'],loc='center') ax=plt.gca() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.box(on=None) table.scale(1,1.5) plt.savefig("table2.png",dpi=200) plt.show() out_file1 = str(1) + "_out_method1.csv" output1 = np.array(pd.read_csv(out_file1,header = None)) out_file6 = str(6) + "_out_method1.csv" output6 = np.array(pd.read_csv(out_file6,header = None)) out_file15 = str(15) + "_out_method1.csv" output15 = np.array(pd.read_csv(out_file15,header = None)) xax,yax1,yax2,yax3 = [],[],[],[] for i in range (len(output1)): xax.append(output1[i][0]) yax1.append(output1[i][2]) yax2.append(output6[i][2]) yax3.append(output15[i][2]) plt.figure() plt.xlabel("Frame number") plt.ylabel("Dynamic density") plt.plot(xax,yax1,label="Dynamic density when 0 frames skipped") plt.plot(xax,yax2,label="Dynamic density when 5 frames skipped") plt.plot(xax,yax3,label="Dynamic density when 14 frames skipped") plt.legend() plt.grid() plt.savefig("plot3.png",dpi=200) plt.show() q = list(map(lambda x:100.0/(1.0+x),q)) d = list(map(lambda x:100.0/(1.0+x),d)) plt.figure() plt.xlabel("Number of frames skipped") plt.ylabel("Utility Percentage") plt.plot(x,q,label="Queue Density utility percentage",marker='o') plt.plot(x,d,label="Dynamic Density utility percentage",marker='o') #for i in range(len(x)): # plt.annotate("("+str(x[i])+","+str(q[i])+")",(x[i],q[i])); # plt.annotate("("+str(x[i])+","+str(d[i])+")",(x[i],d[i])); plt.legend() plt.grid() plt.savefig("plot4.png",dpi=200) plt.show() cell_text=[] for i in range(len(x)): cell_text.append([x[i],q[i],d[i]]) table=plt.table(cellText=cell_text,colLabels=['Number of frames skipped','Queue Density Utility(%)','Dynamic Density Utility(%)'],loc='center') ax=plt.gca() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.box(on=None) table.scale(1,1.5) plt.savefig("table4.png",dpi=200) plt.show() plt.figure() plt.xlabel("Runtime (seconds)") plt.ylabel("Utility Percentage") plt.plot(t,q,label="Queue Density utility percentage",marker='o') plt.plot(t,d,label="Dynamic Density utility percentage",marker='o') #for i in range(len(x)): # plt.annotate("("+str(t[i])+","+str(q[i])+")",(t[i],q[i])); # plt.annotate("("+str(t[i])+","+str(d[i])+")",(t[i],d[i])); plt.legend() plt.grid() plt.savefig("plot5.png",dpi=200) plt.show() cell_text=[] for i in range(len(x)): cell_text.append([t[i],q[i],d[i]]) table = plt.table(cellText=cell_text,colLabels=['Runtime(sec)','Queue Density Utility(%)','Dynamic Density Utility(%)'],loc='center') ax=plt.gca() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.box(on=None) table.scale(1,1.5) plt.savefig("table5.png",dpi=200) plt.show()
def viewKekuleGrid(V, A, DB, L=None, C=None, rad=None, figname=None, cols=None, sizex=5, sizey=5, dpi=150, annotate=False): """ Visualize a single Kekule representation with vertices coordinates 'V', adjacency matrix 'A' and double-bonds 'DB'. If provided, constrained bonds 'C' are shown with the usual Kekule representation and radicals are marked with a dot next to the vertex. """ nA = len(A) nKek = len(DB) DB = np.array(DB, dtype=np.uint16) # define the grid for ploting the figures molsize = max(V[:, 0]) - min(V[:, 0]) + 4. if cols is None: cols = int(35 // molsize) rows = nKek // cols + nKek % cols fig = plt.figure() fig.set_size_inches(sizex, sizey) # set the radical markers (same for all Kekules) if rad: rad = np.array(rad, dtype=np.uint16) nrad = rad.size radmk = np.zeros(shape=[nrad, 2], dtype=np.float16) if nrad == 1: idx = np.transpose(np.nonzero(A[rad])) radmk = sp2lau.ptOrtho(V[idx[0]][0], V[rad], V[idx[1]][0]) else: for i, ir in enumerate(rad): idx = np.transpose(np.nonzero(A[ir])) radmk[i] = sp2lau.ptOrtho(V[idx[0]][0], V[ir], V[idx[1]][0]) # set constrained double bonds (same for all Kekules) if C: C = np.array(C, dtype=np.uint16) allC = C.flatten() else: C = np.empty(shape=[0, 0], dtype=np.uint16) allC = np.empty(shape=[0], dtype=np.uint16) for idb in range(nKek): plt.subplot2grid((rows, cols), (idb // cols, idb % cols)) for i in range(nA): idx = np.transpose(np.nonzero(A[i])) for j in idx: Vp = sp2ggr.checkPeriodic(V[i], V[j], L) for p in range(0, len(Vp), 2): plt.plot(Vp[p:p + 2, 0], Vp[p:p + 2, 1], c='k', ls='-', lw=1.5) kek = DB[idb] for ik in kek: Vp = sp2ggr.checkPeriodic(V[ik[0]], V[ik[1]], L) for p in range(0, len(Vp), 2): par = sp2lau.parallel(Vp[p], Vp[p + 1]) plt.plot((par[0][0], par[1][0]), (par[0][1], par[1][1]), c='r', ls='-', lw=1.5) # radicals if np.any(rad): if nrad == 1: plt.scatter(radmk[0], radmk[1], s=15, c='k', marker='o') else: plt.scatter(radmk[:, 0], radmk[:, 1], s=15, c='k', marker='o') # single bonds around all constrained vertices for ic in allC: idx = np.transpose(np.nonzero(A[ic])) for j in idx: Vp = sp2ggr.checkPeriodic(V[ic], V[j], L) for p in range(0, len(Vp), 2): plt.plot(Vp[p:p + 2, 0], Vp[p:p + 2, 1], c='#00FF37', ls='-', lw=1.5) # double bonds if len(C.shape) == 1: Vp = sp2ggr.checkPeriodic(V[C[0]], V[C[1]], L) for p in range(0, len(Vp), 2): par = sp2lau.parallel(Vp[p], Vp[p + 1]) plt.plot((par[0][0], par[1][0]), (par[0][1], par[1][1]), c='y', ls='-', lw=1.5) else: for icdb in C: Vp = sp2ggr.checkPeriodic(V[icdb[0]], V[icdb[1]], L) for p in range(0, len(Vp), 2): par = sp2lau.parallel(Vp[p], Vp[p + 1]) plt.plot((par[0][0], par[1][0]), (par[0][1], par[1][1]), c='y', ls='-', lw=1.5) if annotate: for i, iv in enumerate(V): plt.annotate(i, (iv[0], iv[1])) """ if idb%cols == 0: plt.ylabel('y [Ang]') else: plt.ylabel('') plt.tick_params(labelleft='off') plt.xlim(min(V[:, 0])-2., max(V[:, 0])+2.) plt.ylim(min(V[:, 1])-2., max(V[:, 1])+2.) plt.xlabel('x [Ang]') """ plt.box(False) plt.axis('off') plt.title('#%i (%i)' % (idb + 1, nKek)) plt.gca().set_aspect(1) if figname: plt.savefig(figname, dpi=dpi) plt.clf() plt.close(fig) else: plt.show()
ha='center', va='center') #円 circle, = plt.plot([0.5], [3.5], marker='o', color='#d3d3d3', markersize=40) #目盛りと枠の非表示 plt.tick_params(axis='both', which='both', bottom='off', top='off', labelbottom='off', right='off', left='off', labelleft='off') plt.box('off') theta_0 = np.array([ [np.nan, 1, 1, np.nan], #0 上,右,下,左 [np.nan, 1, 1, 1], #1 [np.nan, 1, np.nan, 1], #2 [np.nan, np.nan, np.nan, 1], #3 [1, np.nan, 1, np.nan], #4 [1, 1, np.nan, np.nan], #5 [np.nan, 1, 1, 1], #6 [np.nan, np.nan, 1, 1], #7 [1, np.nan, 1, np.nan], #8 [np.nan, np.nan, 1, np.nan], #9 [1, np.nan, 1, np.nan], #10 [1, np.nan, 1, np.nan], #11 [1, 1, np.nan, np.nan], #12
def getBackgroundExtract(lon, lat, chipSize, chipExtend, unique_dir, tms="Google", iformat='tif', withGeometry=False): """Generate an extract from either Google or Bing. It uses the WMTS standard to grab and compose, which makes it fast (does not depend on DIAS S3 store). Arguments: on, lat (float): longitude, latitude in decimal degrees chipSize (int): size of the chip in pixels chipExtend (float): size of the chip in meters unique_dir (str): the path for the file to be stored tms (str): tile map server iformat (str): File format '.tif' or '.png' """ lon, lat = float(lon), float(lat) chipSize = int(chipSize) chipExtend = float(chipExtend) if os.path.exists(unique_dir): # Check which chips are already in the unique_dir # (simple form of caching) cachedList = glob.glob(f"{unique_dir}/{tms}.tif") logging.debug("CACHED") logging.debug(cachedList) if len(cachedList) == 1: logging.debug("No new chips to be processed") return 0 else: logging.debug(f"Creating {unique_dir} on host") os.makedirs(unique_dir) point = ogr.Geometry(ogr.wkbPoint) point.AddPoint(lon, lat) source = osr.SpatialReference() source.ImportFromEPSG(4326) # Assign this projection to the geometry point.AssignSpatialReference(source) target = osr.SpatialReference() target.ImportFromEPSG(3857) transform = osr.CoordinateTransformation(source, target) # Reproject the point geometry to image projection point.Transform(transform) west = point.GetX() - chipExtend / 2 east = point.GetX() + chipExtend / 2 south = point.GetY() - chipExtend / 2 north = point.GetY() + chipExtend / 2 if os.path.isfile(f"tms/{tms.lower()}_tms.xml"): with rio.open(f"tms/{tms.lower()}_tms.xml") as TMS_dataset: bl = TMS_dataset.index(west, south, op=math.floor) tr = TMS_dataset.index(east, north, op=math.ceil) window = Window(bl[1], tr[0], tr[1] - bl[1], bl[0] - tr[0]) # image_size is a tuple (num_bands, h, w) output_dataset = np.empty(shape=(TMS_dataset.count, chipSize, chipSize), dtype=TMS_dataset.profile['dtype']) # Read all bands TMS_dataset.read(out=output_dataset, window=window) res = float(chipExtend) / chipSize transform = Affine.translation( west + res / 2, north - res / 2) * Affine.scale(res, -res) if iformat == 'tif': chipset = rio.open(f"{unique_dir}/{tms.lower()}.tif", 'w', driver='GTiff', width=output_dataset.shape[2], height=output_dataset.shape[1], count=TMS_dataset.count, crs=3857, transform=transform, dtype=TMS_dataset.profile['dtype']) elif iformat == 'png': chipset = rio.open(f"{unique_dir}/{tms.lower()}.png", 'w', driver='PNG', width=output_dataset.shape[2], height=output_dataset.shape[1], count=TMS_dataset.count, crs=3857, transform=transform, dtype=TMS_dataset.profile['dtype']) else: return False chipset.write(output_dataset) chipset.close() if withGeometry and iformat == 'png': from copy import copy from rasterio.plot import show import matplotlib.pyplot as plt from descartes import PolygonPatch from scripts import spatial_utils from scripts import db_queries def overlay_parcel(img, geom): """Create parcel polygon overlay""" patche = [ PolygonPatch(feature, edgecolor="yellow", facecolor="none", linewidth=2) for feature in geom['geom'] ] return patche datasets = db_queries.get_datasets() aoi, year, pid, ptype = withGeometry dataset = datasets[f'{aoi}_{year}'] pdata = db_queries.getParcelById(dataset, pid, ptype, withGeometry, False) if len(pdata) == 1: parcel = dict( zip(list(pdata[0]), [[] for i in range(len(pdata[0]))])) else: parcel = dict( zip(list(pdata[0]), [list(i) for i in zip(*pdata[1:])])) with rio.open(f"{unique_dir}/{tms.lower()}.png") as img: geom = spatial_utils.transform_geometry(parcel, 3857) patches = overlay_parcel(img, geom) for patch in patches: fig = plt.figure() ax = fig.gca() plt.axis('off') plt.box(False) ax.add_patch(copy(patch)) show(img, ax=ax) plt.savefig(f"{unique_dir}/{tms.lower()}.png", bbox_inches='tight') return True else: return False
fig, ax = plt.subplots(figsize=(10, 7)) ax.plot(c, u1, alpha=0.8, label=r'$\theta=%.2f$' % theta[0]) ax.plot(c, u2, alpha=0.8, label=r'$\theta=%.2f$' % theta[1]) ax.plot(c, u3, alpha=0.8, label=r'$\theta=%.2f$' % theta[2]) ax.plot(c, u4, alpha=0.8, label=r'$\theta=%.2f$' % theta[3]) ax.plot(c, u5, alpha=0.8, label=r'$\theta=%.2f$' % theta[4]) ### AXIS ax.axhline(0, color='k') # Add horizontal axis ax.axvline(0, color='k') # Add vertical axis ax.yaxis.set_major_locator(plt.NullLocator()) # Hide ticks ax.xaxis.set_major_locator(plt.NullLocator()) # Hide ticks ### TEXT plt.text(size - 0.10, -0.5, r'$c$', color='k') # x-axis label plt.text(-0.25, 4.5, r'$u(c)$') # y-axis label # SETTINGS plt.box(False) # Hide axis plt.legend(loc=0, frameon=False) plt.axis(v) plt.show() c "|***************************************************************************|" "|CELL #2|*******************************************************************|" "|***************************************************************************|" "1|IMPORT PACKAGES" import numpy as np # Package for scientific computing with Python import matplotlib.pyplot as plt # Matplotlib is a 2D plotting library "2|DEFINE PARAMETERS AND ARRAYS" # PARAMETERS
def build_single_graphs(timestamp, seq_pred_file): #feature names for the dev graph feat_names = [ "KmR", "DmE", "len", "A", "C", "D", "E", "F", "G", "H", "I", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "Y", "KpR", "DpE", "PmN", "PpN", "aro", "pI", "mem", "chr", "fld", "dis", "ent", "bet" ] #line cleaning function and then split on comma def arrayLine(line): line = line.replace("\n", "") line = line.replace("\r", "") line = line.replace(" ", "") return line.split(",") #open the sequence prediction file and extract features from jim's perl output with open(seq_pred_file, "r") as seq_file: for line in seq_file: if line.startswith("SEQUENCE DEVIATIONS"): clean_line = arrayLine(line) deviations = clean_line[2:] if line.startswith("SEQUENCE PREDICTIONS"): clean_line = arrayLine(line) scaledSol = clean_line[3] popSol = clean_line[4] if line.startswith("SEQUENCE PROFILE charge,>"): clean_line = arrayLine(line) charge_window = clean_line[2:] if line.startswith("SEQUENCE PROFILE Uversky"): clean_line = arrayLine(line) fold_window = clean_line[2:] #MAKE SOLUBILITY BAR GRAPH #get the solubility values extracted above sols = np.array([float(popSol), float(scaledSol)]) sol_names = np.array(["PopAvrSol", "QuerySol"]) plt.figure(figsize=(3, 5)) plt.bar("PopAvrSol", float(popSol), color="#454654") plt.bar("QuerySol", float(scaledSol), color="#8587a1") plt.box(on=None) plt.yticks(fontsize=15) plt.xticks([0, 1], sol_names, rotation=45, ha="center", fontsize=15) plt.ylim(0, 1) plt.title("Solubility", fontsize=20) plt.ylabel("Calculated value", fontsize=20) plt.axhline(0, c="black") plt.tight_layout() plt.tick_params(top=False, bottom=False, left=False, right=False, labelleft=True, labelbottom=True) plt.savefig("/tmp/run-" + timestamp + "/Solub-bargraph.png") plt.savefig("/tmp/run-" + timestamp + "/Solub-bargraph.svg") #MAKE DEVIATIONS GRAPH deviations = np.array([float(i) for i in deviations]) #extract devations above and below 0 devup = np.array([i >= 0 for i in deviations]) devdown = np.array([i < 0 for i in deviations]) len_graph = np.arange(len(feat_names)) plt.figure(figsize=(20, 3)) plt.bar(len_graph[devup], deviations[devup], color="#ffe80e") plt.bar(len_graph[devdown], deviations[devdown], color="#a6c600") plt.box(on=None) plt.axhline(0, c="black") plt.xticks(range(0, len(feat_names), 1), rotation=45) plt.xlim(-1, 35) plt.xticks(len_graph, feat_names, fontsize=15, ha="center") plt.yticks(fontsize=15) plt.title("Deviations from population average", fontsize=18) plt.tight_layout() plt.tick_params(top=False, bottom=False, left=False, right=False, labelleft=True, labelbottom=True) plt.savefig("/tmp/run-" + timestamp + "/Solub-devgraph.svg") plt.savefig("/tmp/run-" + timestamp + "/Solub-devgraph.png") #MAKE CHARGE GRAPH charge_window = np.array([float(i) for i in charge_window]) chargeup = np.array([i >= 0 for i in charge_window]) chargedown = np.array([i < 0 for i in charge_window]) len_graph = np.arange(len(charge_window)) len_feat_names = np.arange(1, len(charge_window) + 1) plt.figure(figsize=(20, 3)) plt.bar(len_graph[chargeup], charge_window[chargeup], color="#3892d0") plt.bar(len_graph[chargedown], charge_window[chargedown], color="#cc0000") plt.box(on=None) plt.axhline(0, c="black") plt.xticks(range(0, len(len_feat_names), 1), rotation=45) n_ten = int(len(charge_window) / 10) n_int = math.ceil(n_ten / 10) * 10 plt.xticks(np.arange(0, len(charge_window) + 1, n_int), fontsize=15) plt.yticks(fontsize=15) plt.xlim(-1, len(charge_window)) plt.title("Windowed charge score per amino acid", fontsize=18) plt.tight_layout() plt.tick_params(top=False, bottom=False, left=False, right=False, labelleft=True, labelbottom=True) plt.savefig("/tmp/run-" + timestamp + "/Solub-chargegraph.svg") plt.savefig("/tmp/run-" + timestamp + "/Solub-chargegraph.png") #MAKE FOLD GRAPH fold_window = np.array([float(i) for i in fold_window]) foldup = np.array([i >= 0 for i in fold_window]) folddown = np.array([i < 0 for i in fold_window]) len_graph = np.arange(len(fold_window)) len_feat_names = np.arange(1, len(fold_window) + 1) plt.figure(figsize=(20, 3)) plt.bar(len_graph[foldup], fold_window[foldup], color="#ffa838") plt.bar(len_graph[folddown], fold_window[folddown], color="#484cd7") plt.box(on=None) plt.axhline(0, c="black") plt.xticks(range(0, len(len_feat_names), 1), rotation=45) n_ten = int(len(fold_window) / 10) n_int = math.ceil(n_ten / 10) * 10 plt.xticks(np.arange(0, len(fold_window) + 1, n_int), fontsize=15) plt.yticks(fontsize=15) plt.xlim(-1, len(fold_window)) plt.title("Windowed fold propensity per amino acid", fontsize=18) plt.tight_layout() plt.tick_params(top=False, bottom=False, left=False, right=False, labelleft=True, labelbottom=True) plt.savefig("/tmp/run-" + timestamp + "/Solub-foldgraph.svg") plt.savefig("/tmp/run-" + timestamp + "/Solub-foldgraph.png") # save files with open("/tmp/run-" + timestamp + "/predicted_deviations.csv", "w") as deviationsCSV: for index, deviationValue in enumerate(deviations): deviationsCSV.write(feat_names[index]) deviationsCSV.write(",") deviationsCSV.write(str(deviationValue)) deviationsCSV.write("\n") with open("/tmp/run-" + timestamp + "/proteinsol_prediction.csv", "w") as solubilityCSV: for index, solubilityValue in enumerate(sols): solubilityCSV.write(sol_names[index]) solubilityCSV.write(",") solubilityCSV.write(str(solubilityValue)) solubilityCSV.write("\n") with open("/tmp/run-" + timestamp + "/charge_window.csv", "w") as chargewindowCSV: for index, chargeValue in enumerate(charge_window): chargewindowCSV.write(str(index)) chargewindowCSV.write(",") chargewindowCSV.write(str(chargeValue)) chargewindowCSV.write("\n") with open("/tmp/run-" + timestamp + "/fold_window.csv", "w") as foldwindowCSV: for index, foldValue in enumerate(fold_window): foldwindowCSV.write(str(index)) foldwindowCSV.write(",") foldwindowCSV.write(str(foldValue)) foldwindowCSV.write("\n")
def draw_multipletiles(tile_doy, tile_names=None, interp_doy=None, pdfsave_file=None): #-- global marker_size = 10 T = len(tile_doy) # no. of tiles plt.figure(figsize=(6, 1)) ax = plt.gca() for i in range(T): ndates = len(tile_doy[i]) yval = i / T if tile_names is None: plt.scatter(tile_doy[i], ndates * [yval], s=marker_size, zorder=1) else: plt.scatter(tile_doy[i], ndates * [yval], s=marker_size, zorder=1, label=tile_names[i]) xmin, xmax = plt.xlim() ymin, ymax = plt.ylim() # Arrow configuration hw = 1. / 10. * (ymax - ymin) # arrow head width hl = 1. / 40. * (xmax - xmin) # arrow head length ohg = 0.3 # arrow overhang for i in range(T): yval = i / T ax.arrow(xmin, yval, xmax - xmin, 0., color=mcolors.CSS4_COLORS['gray'], fc=mcolors.CSS4_COLORS['gray'], ec=mcolors.CSS4_COLORS['gray'], lw=1, head_width=hw, head_length=hl, overhang=ohg, length_includes_head=True, clip_on=False, zorder=-1) ax.set_ylim([-0.1, 2 / T + 0.15]) min_doy = min(min(tile_doy)) max_doy = max(max(tile_doy)) for idx, m in enumerate(month_names): if idx == 0: xm = 4 else: xm = np.cumsum(monthday)[idx - 1] if xm < min_doy or xm > max_doy: continue #-- if month bar desired [check draw_singletile] plt.annotate(m, (xm, (T - 1) / T + 0.15), xycoords='data', color=mcolors.CSS4_COLORS['gray']) #-- Dashed red lines for interpolated doy if not interp_doy is None: for dd in interp_doy: plt.plot([dd, dd], [0, (T - 1) / T], 'r--', dashes=(5, 10), lw=0.5, zorder=-1) #-- plt.xticks([]) ##-- decomment to hide doy values in the x-axis plt.yticks([]) plt.box(False) if not tile_names is None: plt.legend(bbox_to_anchor=(1.25, 1)) if not pdfsave_file is None: plt.savefig(pdfsave_file, bbox_inches='tight') plt.show()
def plot_robustness_to_vocab_size(hp, atleast, show=False): colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS) atleast_to_voc, atleast_to_nImages, atleast_to_precision = \ OrderedDict(), dict(), dict() metrics, fs, lw, ms = get_models_style() hp['seed'] = hp['max_seed'] tf_to_cH, tf_to_cDKL, tf_to_cSingleton, tf_to_cPX, tf_to_cMI, \ tf_to_confidence = dict(), dict(), dict(), dict(), dict(), dict() for t in atleast: hp['atleast'] = t param_string = utils.create_param_string(hp, True) # Read precision path_results = 'Results/robust/vocab/' + param_string + '/results.pkl' print('Read results from %s' % path_results) (precision, sem_p, recall, sem_r) = pickle.load(open(path_results, 'r')) atleast_to_precision[t] = { 'cH': precision['cH'][0][0], 'cDKL': precision['cDKL'][0][0], 'cSingleton': precision['cSingleton'][0][0], 'cPX': precision['cPX'][0][0], 'cMI': precision['cMI'][0][0], 'Confidence': precision['Confidence'][0][0] } tf_to_cH[t] = precision['cH'][0][0] tf_to_cDKL[t] = precision['cDKL'][0][0] tf_to_cSingleton[t] = precision['cSingleton'][0][0] tf_to_cPX[t] = precision['cPX'][0][0] tf_to_cMI[t] = precision['cMI'][0][0] tf_to_confidence[t] = precision['Confidence'][0][0] # Read vocabulary size. path_voc = 'Results/robust/vocab/' + param_string + \ '/avg_label_metrics.pkl' label_metrics = pickle.load(open(path_voc, 'r')) atleast_to_voc[t] = label_metrics.shape[0] fig, ax = plt.subplots(figsize=(8, 5)) voc_size = atleast_to_voc.values() plt.plot(voc_size, tf_to_cH.values(), color=colors['royalblue'], markersize=ms, marker=metrics[0]['marker'], label=metrics[0]['legend']) plt.plot(voc_size, tf_to_cDKL.values(), '-', markersize=ms, color=colors['lightcoral'], marker=metrics[1]['marker'], label=metrics[1]['legend']) plt.plot(voc_size, tf_to_cMI.values(), '-', markersize=ms, marker=metrics[2]['marker'], color=colors['skyblue'], label=metrics[2]['legend']) plt.plot(voc_size, tf_to_cSingleton.values(), '-', markersize=ms, marker=metrics[3]['marker'], color=colors['mediumpurple'], label=metrics[3]['legend']) plt.plot(voc_size, tf_to_cPX.values(), '-', markersize=ms, marker=metrics[4]['marker'], color=colors['palegreen'], label=metrics[4]['legend']) plt.plot(voc_size, tf_to_confidence.values(), '-', markersize=ms, marker=metrics[5]['marker'], color=colors['orange'], label=metrics[5]['legend']) plt.box(on=None) ax.legend(loc='upper right', frameon=False, fontsize=fs['legend'], ncol=3, bbox_to_anchor=(0.5, 0.8, 0.5, 0.5)) ax.set_ylabel('precision@1', fontsize=fs['axis']) ax.set_xlabel('vocabulary size', fontsize=fs['axis']) plt.rc('xtick', labelsize=fs['ticks']) plt.rc('ytick', labelsize=fs['ticks']) if show: plt.show() fn = 'Results/robust/vocab/vocab_' + param_string + '.png' plt.savefig(fn, bbox_inches='tight', facecolor='white') print('Write results to %s' % fn)
def plotDifficulty(self, **kwargs): mts_flag = (np.unique( (Condition & self).fetch('experiment_class')) == ['MatchToSample']) mp_flag = (np.unique( (Condition & self).fetch('experiment_class')) == ['MatchPort'] ) #only olfactory so far if mts_flag: cond_class = experiment.Condition.MatchToSample() elif mp_flag: cond_class = experiment.Condition.MatchPort() else: print('what experiments class?') return [] difficulties = (self * cond_class).fetch('difficulty') min_difficulty = np.min(difficulties) params = { 'probe_colors': [[1, 0, 0], [0, .5, 1]], 'trial_bins': 10, 'range': 0.9, 'xlim': (-1, ), 'ylim': (min_difficulty - 0.6, ), **kwargs } def plot_trials(trials, **kwargs): difficulties, trial_idxs = ((self & trials) * cond_class).fetch( 'difficulty', 'trial_idx') offset = ((trial_idxs - 1) % params['trial_bins'] - params['trial_bins'] / 2) * params['range'] * 0.1 plt.scatter(trial_idxs, difficulties + offset, zorder=10, **kwargs) # correct trials correct_trials = (self & behavior.Rewards.proj(rtime='time')).proj() # missed trials missed_trials = Trial.Aborted() & self # incorrect trials incorrect_trials = (self - missed_trials - correct_trials).proj() print('correct: {}, incorrect: {}, missed: {}'.format( len(correct_trials), len(incorrect_trials), len(missed_trials))) print('correct: {}, incorrect: {}, missed: {}'.format( len(np.unique(correct_trials.fetch('trial_idx'))), len(np.unique(incorrect_trials.fetch('trial_idx'))), len(np.unique(missed_trials.fetch('trial_idx'))))) # plot trials fig = plt.figure(figsize=(10, 5), tight_layout=True) plot_trials(correct_trials, s=4, c=np.array(params['probe_colors']) [(correct_trials * behavior.BehCondition.Trial() * behavior.MultiPort.Response() ).fetch('response_port', order_by='trial_idx') - 1]) plot_trials(incorrect_trials, s=4, marker='o', facecolors='none', edgecolors=[.3, .3, .3], linewidths=.5) plot_trials(missed_trials, s=.1, c=[[0, 0, 0]]) # plot info plt.xlabel('Trials') plt.ylabel('Difficulty') plt.title('Animal:%d Session:%d' % (Session() & self).fetch1('animal_id', 'session')) plt.yticks( range(int(min(plt.gca().get_ylim())), int(max(plt.gca().get_ylim())) + 1)) plt.ylim(params['ylim'][0]) plt.xlim(params['xlim'][0]) plt.gca().xaxis.set_ticks_position('none') plt.gca().yaxis.set_ticks_position('none') plt.box(False) plt.show()
def plot2D(listX, listY, type='line', z2D=(None, ), name='Untitled2D_Plot', fontSize=14, plotLabels=(None, ), alpha=1., contourLvl=10, cmap='plasma', xLabel='$x$', yLabel='$y$', zLabel='$z$', figDir='./', show=True, xLim=(None, ), yLim=(None, ), xyScale=('linear', 'linear'), saveFig=True, equalAxis=False): import matplotlib.pyplot as plt from Utilities import configurePlotSettings import numpy as np from warnings import warn if isinstance(listX, np.ndarray): listX, listY = (listX, ), (listY, ) # if len(listX) != len(listY): # warn('\nThe number of provided x array does not match that of y. Not plotting!\n', stacklevel = 2) # return if plotLabels[0] is None: plotLabels = (type, ) * len(listX) lines, markers, colors = configurePlotSettings(lineCnt=len(listX), fontSize=fontSize) # plt.figure(name) fig, ax = plt.subplots(1, 1, num=name) if (z2D[0] is not None): x, y = np.array(listX[0]), np.array(listY[0]) if len(np.array(x).shape) == 1: warn( '\nX and Y are 1D, contour/contourf requires mesh grid. Converting X and Y to mesh grid automatically...\n', stacklevel=2) x2D, y2D = np.meshgrid(x, y, sparse=True) else: x2D, y2D = x, y if type is 'contour': plt.contour(x2D, y2D, z2D, levels=contourLvl, cmap=cmap, extend='both') else: plt.contourf(x2D, y2D, z2D, levels=contourLvl, cmap=cmap, extend='both') else: for i in range(len(listX)): x, y = listX[i], listY[i] if type is 'scatter': plt.scatter(x, y, lw=0, label=plotLabels[i], alpha=alpha, color=colors[i]) else: plt.plot(x, y, ls=lines[i], label=plotLabels[i], color=colors[i], marker=markers[i], alpha=alpha) plt.xlabel(xLabel), plt.ylabel(yLabel) if equalAxis: ax.set_aspect('equal', 'box') if xLim[0] is not None: plt.xlim(xLim) if yLim[0] is not None: plt.ylim(yLim) plt.xscale(xyScale[0]), plt.yscale(xyScale[1]) if len(listX) > 1: if len(listX) > 3: nCol = 2 else: nCol = 1 plt.legend(loc='best', shadow=True, fancybox=False, ncol=nCol) if z2D[0] is None: plt.grid(which='both', alpha=0.5) else: cb = plt.colorbar(orientation='horizontal') cb.set_label(zLabel) cb.outline.set_visible(False) plt.box(False) # fig.canvas.draw() # labels = [item.get_text() for item in ax.get_xticklabels()] # labels[1] = 'Testing' # ax.set_xticklabels(labels) # plt.annotate('ajsfbs', (1850, 0)) # ax.spines['top'].set_visible(False) # ax.spines['right'].set_visible(False) # ax.spines['bottom'].set_visible(False) # ax.spines['left'].set_visible(False) plt.tight_layout() if saveFig: plt.savefig(figDir + '/' + name + '.png', transparent=True, bbox_inches='tight', dpi=1000) if show: plt.show()
def scatter_matrix(df, colx, coly, cols, color=['grey', 'black'], ratio=10, font='Helvetica', save=False, save_name='Default'): ''' Goal: This function create an scatter plot for categorical variables. It's useful to compare two lists with elements in common. Input: - df: required. pandas DataFrame with at least two columns with categorical variables you want to relate, and the value of both (if it's just an adjacent matrix write 1) - colx: required. The name of the column to display horizontaly - coly: required. The name of the column to display vertically - cols: required. The name of the column with the value between the two variables - color: optional. Colors to display in the visualization, the length can be two or three. The two first are the colors for the lines in the matrix, the last one the font color and markers color. default ['grey','black'] - ratio: optional. A ratio for controlling the relative size of the markers. default 10 - font: optional. The font for the ticks on the matrix. default 'Helvetica' - save: optional. True for saving as an image in the same path as the code. default False - save_name: optional. The name used for saving the image (then the code ads .png) default: "Default" Output: No output. Matplotlib object is not shown by default to be able to add more changes. ''' # Create a dict to encode the categeories into numbers (sorted) colx_codes = dict( zip(df[colx].sort_values().unique(), range(len(df[colx].unique())))) coly_codes = dict( zip(df[coly].sort_values(ascending=False).unique(), range(len(df[coly].unique())))) # Apply the encoding df[colx] = df[colx].apply(lambda x: colx_codes[x]) df[coly] = df[coly].apply(lambda x: coly_codes[x]) # Prepare the aspect of the plot plt.rcParams['xtick.bottom'] = plt.rcParams['xtick.labelbottom'] = False plt.rcParams['xtick.top'] = plt.rcParams['xtick.labeltop'] = True plt.rcParams['font.sans-serif'] = font plt.rcParams['xtick.color'] = color[-1] plt.rcParams['ytick.color'] = color[-1] plt.box(False) # Plot all the lines for the background for num in range(len(coly_codes)): plt.hlines(num, -1, len(colx_codes) + 1, linestyle='dashed', linewidth=1, color=color[num % 2], alpha=0.5) for num in range(len(colx_codes)): plt.vlines(num, -1, len(coly_codes) + 1, linestyle='dashed', linewidth=1, color=color[num % 2], alpha=0.5) # Plot the scatter plot with the numbers plt.scatter(df[colx], df[coly], s=df[cols] * ratio, zorder=2, color=color[-1]) # Change the ticks numbers to categories and limit them plt.xticks(ticks=list(colx_codes.values()), labels=colx_codes.keys(), rotation=90) plt.yticks(ticks=list(coly_codes.values()), labels=coly_codes.keys()) plt.xlim(xmin=-1, xmax=len(colx_codes)) plt.ylim(ymin=-1, ymax=len(coly_codes)) # Save if wanted if save: plt.savefig(save_name + '.png')
def pitch(bg_color='#FFFFFF', line_color='#000000', dpi=144): # Background cleanup plt.rcParams['figure.figsize'] = (10.5, 6.8) plt.rcParams['figure.dpi'] = dpi plt.rcParams['figure.facecolor'] = bg_color plt.xticks([]) plt.yticks([]) plt.box(False) plt.scatter(50, 50, s=1000000, marker='s', color=bg_color) # Set plotting limit plt.xlim([-5, 105]) plt.ylim([-5, 105]) # Outside lines plt.axvline(0, ymin=0.0455, ymax=0.9545, linewidth=3, color=line_color) plt.axvline(100, ymin=0.0455, ymax=0.9545, linewidth=3, color=line_color) plt.axhline(0, xmin=0.0455, xmax=0.9545, linewidth=3, color=line_color) plt.axhline(100, xmin=0.0455, xmax=0.9545, linewidth=3, color=line_color) # Midfield line plt.axvline(50, ymin=0.0455, ymax=0.9545, linewidth=1, color=line_color) # Goals plt.axvline(0, ymin=0.4511, ymax=0.5489, linewidth=5, color=line_color) plt.axvline(100, ymin=0.4511, ymax=0.5489, linewidth=5, color=line_color) plt.axvline(-1, ymin=0.4511, ymax=0.5489, linewidth=5, color=line_color) plt.axvline(101, ymin=0.4511, ymax=0.5489, linewidth=5, color=line_color) # Small Box ## (Width-SmallboxWidth)/2/ScaleTo100, (Margin+(Width-SmallboxWidth)/2/ScaleTo100)/(100+Margins) ## (68-7.32-11)/2/0.68, (5+((68-7.32-11)/2/.68))/110 ## (5+5.5/1.05)/110, 5.25/1.05 plt.axvline(5.24, ymin=0.3775, ymax=0.6225, linewidth=1, color=line_color) plt.axvline(94.76, ymin=0.3775, ymax=0.6225, linewidth=1, color=line_color) plt.axhline(36.53, xmin=0.0455, xmax=0.0931, linewidth=1, color=line_color) plt.axhline(63.47, xmin=0.0455, xmax=0.0931, linewidth=1, color=line_color) plt.axhline(36.53, xmin=0.9069, xmax=0.9545, linewidth=1, color=line_color) plt.axhline(63.47, xmin=0.9069, xmax=0.9545, linewidth=1, color=line_color) # Big Box plt.axvline(15.72, ymin=0.2306, ymax=0.7694, linewidth=1, color=line_color) plt.axhline(20.37, xmin=0.0455, xmax=0.1883, linewidth=1, color=line_color) plt.axhline(79.63, xmin=0.0455, xmax=0.1883, linewidth=1, color=line_color) plt.axvline(84.28, ymin=0.2306, ymax=0.7694, linewidth=1, color=line_color) plt.axhline(20.37, xmin=0.8117, xmax=0.9545, linewidth=1, color=line_color) plt.axhline(79.63, xmin=0.8117, xmax=0.9545, linewidth=1, color=line_color) # Penalty and starting spots and arcs plt.scatter([10.4762, 89.5238, 50], [50, 50, 50], s=1, color=line_color) e1 = Arc((10.4762, 50), 17.5, 27, theta1=-64, theta2=64, fill=False, color=line_color) e2 = Arc((89.5238, 50), 17.5, 27, theta1=116, theta2=244, fill=False, color=line_color) e3 = Arc((50, 50), 17.5, 27, fill=False, color=line_color) plt.gcf().gca().add_artist(e1) plt.gcf().gca().add_artist(e2) plt.gcf().gca().add_artist(e3)
def micompanyify(data, highlight=-1, plottype=None, ascending=True, strfmt=None, **kwargs): ''' Nicer visualization for basic plot, returning the axis object Automatically chooses plot type and nicely makes up the plot area based on learnings from the MIcompany training Parameters ---------- data: must be a pandas.Series or pandas.DataFrame highlight: the index or list of indices of the data point you want to highlight plottype: inferred from the data, but can be overridden: 'bar' (horizontal bar), 'bar_timeseries' (vertical bars), 'line_timeseries', 'waterfall' (builddown), 'waterfall_buildup', 'composition_comparison' or 'scatter' ascending: sorting direction. By default largest values are shown at the top, but False is possible, or None to leave the sorting as is strfmt: how to format accompanying data labels above bars, e.g. ".2f" or ".1%" **kwargs: will be passed to pd.Dataframe.plot() Returns ------- :class:`matplotlib.axes.Axes` object with the plot on it ''' if not isinstance(data, pd.DataFrame) and not isinstance(data, pd.Series): raise TypeError( f'Data is not of type Series or DataFrame, but type {type(data)}') data = data.squeeze( ) # DataFrame with single column should be treated as Series if plottype is None: plottype = pick_plottype(data) orient = 'h' if plottype in ['bar_timeseries', 'line_timeseries'] else 'v' if strfmt is None: if (isinstance(data, pd.DataFrame) and data.apply(is_percentage_series).all()) or\ (isinstance(data, pd.Series) and is_percentage_series(data)): strfmt = '.1%' else: strfmt = '.2f' color = define_colors(highlight, data, plottype) if plottype in [ 'scatter', 'piechart', 'composition_comparison', 'bar_timeseries', 'line_timeseries' ] or not isinstance(data, pd.Series): ascending = None if ascending is not None: data = data.sort_values(ascending=ascending) if plottype == 'bar': ax = data.plot(kind='barh', color=color, **kwargs) if isinstance(data, pd.Series): plot_values_above_bar(data, orient=orient, strfmt=strfmt) else: for i, col in enumerate(data.columns): plot_values_above_bar(data[col], orient=orient, strfmt=strfmt, bar_nr=i, nr_bars=len(data.columns)) elif plottype == 'waterfall': ax, data, blank = plot_waterfall(data, color=color, **kwargs) plot_values_above_bar((data + blank), data, ax=ax, strfmt=strfmt, orient=orient) elif plottype == 'waterfall_buildup': ax, data, blank = plot_waterfall(data, color=color, buildup=True, **kwargs) if pd.options.plotting.backend == 'matplotlib': plot_values_above_bar((data + blank), data, ax=ax, strfmt=strfmt, orient=orient) elif plottype == 'bar_timeseries': ax = data.plot(kind='bar', color=color, **kwargs) if isinstance(data, pd.Series): plot_values_above_bar(data, orient=orient, strfmt=strfmt) else: for i, col in enumerate(data.columns): plot_values_above_bar(data[col], orient=orient, strfmt=strfmt, bar_nr=i, nr_bars=len(data.columns)) elif plottype == 'line_timeseries': ax = data.plot(color=color, **kwargs) elif plottype == 'scatter': x = data.iloc[:, 0] y = data.iloc[:, 1] try: size = data.iloc[:, 2] except IndexError: size = None ax = sns.scatterplot(x=x, y=y, size=size, color='grey', **kwargs) elif plottype == 'composition_comparison': ax = data.transpose().plot(kind='barh', stacked=True, color=color, **kwargs) data_begin = data.cumsum().shift().fillna(0) data_end = data.cumsum() location = data_begin.add(data_end).div(2) if not isinstance(highlight, int): raise TypeError( 'Can only highlight one line in composition comparison') plot_values_above_bar(location.iloc[highlight, :], data.iloc[highlight, :], textcolor='white', orient=orient, strfmt=strfmt) elif plottype == 'piechart': raise TypeError('A piechart? Are you kidding me?') else: raise NotImplementedError( f'plottype {plottype} not available, choose "bar", or "waterfall"') plt.box(False) if orient == 'v' and plottype != 'scatter': plt.xticks([]) elif orient == 'h' and plottype not in ['line_timeseries', 'scatter']: plt.yticks([]) return ax
def Pie_Active_Fraction(index): print("Doing " + str(index)) global states global clrs ax.clear() plt.title("Distribution of Active Cases") cases = df2_cases[states].loc[3 * index] dead = df2_dead[states].loc[3 * index + 2] recovered = df2_recovered[states].loc[3 * index + 1] active = (cases - (dead + recovered)) actv = active tot_rec = df2_recovered["India"].loc[3 * index + 1] tot_cases = df2_cases["India"].loc[3 * index] tot_dead = df2_dead["India"].loc[3 * index + 2] tot_act = tot_cases - (tot_dead + tot_rec) active = np.divide(active * 100, tot_act) date = df_cases["Date"].loc[3 * index] ind_count = round(tot_rec, 2) st = [] for i in range(len(states)): st.append(states[i] + " (" + str(int(actv[i])) + ")") wedges, texts, autotexts = ax.pie(active, labels=st, autopct='%1.1f%%', colors=clrs, startangle=90, shadow=True, wedgeprops={ "edgecolor": "black", 'linewidth': 0 }, pctdistance=0.85) plt.setp(autotexts, size=7, weight="bold") plt.setp(texts, size=9) centre_circle = plt.Circle((0, 0), 0.65, fc='white') tx = plt.text(0.5, 0.6, "Gururaj Kadiri", transform=ax.transAxes, color='#777777', size=10, ha="center", weight=800) tx = plt.text(0.5, 0.5, str(date), transform=ax.transAxes, color='#777777', size=25, ha="center", weight=800) tx = plt.text(0.5, 0.4, str(int(tot_act)), transform=ax.transAxes, color='#008080', size=15, ha="center", weight=800) fig = plt.gcf() fig.gca().add_artist(centre_circle) fig.gca().add_artist(tx) plt.box(False)
def viewSpectraStack( projData: ProjectData, site: str, meas: str, **kwargs ) -> Union[plt.figure, None]: """View spectra stacks for a measurement Parameters ---------- projData : projecData The project data site : str The site to view meas: str The measurement of the site to view chans : List[str], optional Channels to plot declevel : int, optional Decimation level to plot numstacks : int, optional The number of windows to stack coherences : List[List[str]], optional A list of coherences to add, specified as [["Ex", "Hy"], ["Ey", "Hx"]] specdir : str, optional String that specifies spectra directory for the measurement show : bool, optional Show the spectra plot save : bool, optional Save the plot to the images directory plotoptions : Dict, optional Dictionary of plot options Returns ------- matplotlib.pyplot.figure or None A matplotlib figure unless the plot is not shown and is saved, in which case None and the figure is closed. """ options = {} options["chans"] = [] options["declevel"] = 0 options["numstacks"] = 10 options["coherences"] = [] options["specdir"] = projData.config.configParams["Spectra"]["specdir"] options["show"] = True options["save"] = False options["plotoptions"] = plotOptionsSpec() options = parseKeywords(options, kwargs) projectText( "Plotting spectra stack for measurement {} and site {}".format(meas, site) ) specReader = getSpecReader(projData, site, meas, **options) # channels dataChans = specReader.getChannels() if len(options["chans"]) > 0: dataChans = options["chans"] numChans = len(dataChans) # get windows numWindows = specReader.getNumWindows() sampleFreqDec = specReader.getSampleFreq() f = specReader.getFrequencyArray() # calculate num of windows to stack in each set stackSize = int(np.floor(1.0 * numWindows / options["numstacks"])) # calculate number of rows - in case interested in coherences too nrows = ( 2 if len(options["coherences"]) == 0 else 2 + np.ceil(1.0 * len(options["coherences"]) / numChans) ) # setup the figure plotfonts = options["plotoptions"]["plotfonts"] cmap = colorbarMultiline() fig = plt.figure(figsize=options["plotoptions"]["figsize"]) st = fig.suptitle( "Spectra stack, fs = {:.6f} [Hz], decimation level = {:2d}, windows in each set = {:d}".format( sampleFreqDec, options["declevel"], stackSize ), fontsize=plotfonts["suptitle"], ) st.set_y(0.98) # do the stacking for iP in range(0, options["numstacks"]): stackStart = iP * stackSize stackStop = min(stackStart + stackSize, numWindows) color = cmap(iP/options["numstacks"]) # dictionaries to hold data for this section stackedData = {} ampData = {} phaseData = {} powerData = {} # assign initial zeros for c in dataChans: stackedData[c] = np.zeros(shape=(specReader.getDataSize()), dtype="complex") ampData[c] = np.zeros(shape=(specReader.getDataSize()), dtype="complex") phaseData[c] = np.zeros(shape=(specReader.getDataSize()), dtype="complex") for c2 in dataChans: powerData[c + c2] = np.zeros( shape=(specReader.getDataSize()), dtype="complex" ) # now stack the data and create nice plots for iW in range(stackStart, stackStop): winData = specReader.readBinaryWindowLocal(iW) for c in dataChans: stackedData[c] += winData.data[c] ampData[c] += np.absolute(winData.data[c]) phaseData[c] += np.angle(winData.data[c]) * (180.0 / np.pi) # get coherency data for c2 in dataChans: powerData[c + c2] += winData.data[c] * np.conjugate( winData.data[c2] ) if iW == stackStart: startTime = winData.startTime if iW == stackStop - 1: stopTime = winData.stopTime # scale powers and stacks ampLim = options["plotoptions"]["amplim"] for idx, c in enumerate(dataChans): stackedData[c] = stackedData[c] / (stackStop - stackStart) ampData[c] = ampData[c] / (stackStop - stackStart) phaseData[c] = phaseData[c] / (stackStop - stackStart) for c2 in dataChans: # normalisation powerData[c + c2] = 2 * powerData[c + c2] / (stackStop - stackStart) # normalisation powerData[c + c2][[0, -1]] = powerData[c + c2][[0, -1]] / 2 # plot ax1 = plt.subplot(nrows, numChans, idx + 1) plt.title("Amplitude {}".format(c), fontsize=plotfonts["title"]) h = ax1.semilogy( f, ampData[c], color=color, label="{} to {}".format( startTime.strftime("%m-%d %H:%M:%S"), stopTime.strftime("%m-%d %H:%M:%S"), ), ) if len(ampLim) > 2: ax1.set_ylim(ampLim) else: ax1.set_ylim(0.01, 1000) ax1.set_xlim(0, sampleFreqDec / 2.0) if isMagnetic(c): ax1.set_ylabel("Amplitude [nT]", fontsize=plotfonts["axisLabel"]) else: ax1.set_ylabel("Amplitude [mV/km]", fontsize=plotfonts["axisLabel"]) ax1.set_xlabel("Frequency [Hz]", fontsize=plotfonts["axisLabel"]) plt.grid(True) # set tick sizes for label in ax1.get_xticklabels() + ax1.get_yticklabels(): label.set_fontsize(plotfonts["axisTicks"]) # plot phase ax2 = plt.subplot(nrows, numChans, numChans + idx + 1) plt.title("Phase {}".format(c), fontsize=plotfonts["title"]) ax2.plot( f, phaseData[c], color=color, label="{} to {}".format( startTime.strftime("%m-%d %H:%M:%S"), stopTime.strftime("%m-%d %H:%M:%S"), ), ) ax2.set_ylim(-180, 180) ax2.set_xlim(0, sampleFreqDec / 2.0) ax2.set_ylabel("Phase [degrees]", fontsize=plotfonts["axisLabel"]) ax2.set_xlabel("Frequency [Hz]", fontsize=plotfonts["axisLabel"]) plt.grid(True) # set tick sizes for label in ax2.get_xticklabels() + ax2.get_yticklabels(): label.set_fontsize(plotfonts["axisTicks"]) # plot coherences for idx, coh in enumerate(options["coherences"]): c = coh[0] c2 = coh[1] cohNom = np.power(np.absolute(powerData[c + c2]), 2) cohDenom = powerData[c + c] * powerData[c2 + c2] coherence = cohNom / cohDenom ax = plt.subplot(nrows, numChans, 2 * numChans + idx + 1) plt.title("Coherence {} - {}".format(c, c2), fontsize=plotfonts["title"]) ax.plot( f, coherence, color=color, label="{} to {}".format( startTime.strftime("%m-%d %H:%M:%S"), stopTime.strftime("%m-%d %H:%M:%S"), ), ) ax.set_ylim(0, 1.1) ax.set_xlim(0, sampleFreqDec / 2) ax.set_ylabel("Coherence", fontsize=plotfonts["axisLabel"]) ax.set_xlabel("Frequency [Hz]", fontsize=plotfonts["axisLabel"]) plt.grid(True) # set tick sizes for label in ax.get_xticklabels() + ax.get_yticklabels(): label.set_fontsize(plotfonts["axisTicks"]) # fig legend and layout ax = plt.gca() h, l = ax.get_legend_handles_labels() fig.tight_layout(rect=[0.01, 0.01, 0.98, 0.81]) # legend legax = plt.axes(position=[0.01, 0.82, 0.98, 0.12], in_layout=False) plt.tick_params(left=False, labelleft=False, bottom=False, labelbottom="False") plt.box(False) legax.legend(h, l, ncol=4, loc="upper center", fontsize=plotfonts["legend"]) # plot show and save if options["save"]: impath = projData.imagePath filename = "spectraStack_{}_{}_dec{}_{}".format( site, meas, options["declevel"], options["specdir"] ) savename = savePlot(impath, filename, fig) projectText("Image saved to file {}".format(savename)) if options["show"]: plt.show(block=options["plotoptions"]["block"]) if not options["show"] and options["save"]: plt.close(fig) return None return fig
def plot_circuit(circuit: Circuit, cell_size: int = 6, to_file: Optional[str] = None, trajectories: List[Tuple[float, State, Action]] = None): max_extent = max(circuit.track.shape) x_bounds = (-cell_size, cell_size * (max_extent + 1)) y_bounds = (-cell_size, cell_size * (max_extent + 1)) fig = plt.figure() ax = fig.add_subplot(111) ax.set_xlim(x_bounds) ax.set_ylim(y_bounds) ax.xaxis.set_visible(False) ax.yaxis.set_visible(False) trajectory_colors = ['yellow', 'red', 'blue', 'purple'] def trajectory_color(idx: int) -> str: return trajectory_colors[idx % len(trajectory_colors)] def color(cell_type: int): if cell_type == TRACK: return 'white' elif cell_type == START: return 'brown' elif cell_type == FINISH: return 'green' else: raise ValueError("Unknown call type") for x in range(circuit.track.shape[0]): for y in range(circuit.track.shape[1]): if circuit.track[x, y] != EMPTY: polygon = plt.Rectangle((x * cell_size, y * cell_size), cell_size, cell_size, facecolor=color(circuit.track[x, y]), edgecolor='black', linewidth=1.0) ax.add_patch(polygon) if trajectories is not None: l = [] for idx, trajectory in enumerate(trajectories): x_off = (idx % 2) * (cell_size // 2) y_off = ((idx // 2) % 2) * (cell_size // 2) g = 0. for state, _, reward, _ in trajectory: x = state.position[0] y = state.position[1] polygon = plt.Rectangle( (x * cell_size + x_off, y * cell_size + y_off), cell_size // 2, cell_size // 2, facecolor=trajectory_color(idx), edgecolor='black', linewidth=0.0) ax.add_patch(polygon) g += reward l.append((polygon, int(g))) plt.legend([e[0] for e in l], [e[1] for e in l]) plt.box(on=None) if to_file: plt.savefig(to_file) else: plt.show() plt.close()
def viewSpectra( projData: ProjectData, site: str, meas: str, **kwargs ) -> Union[plt.figure, None]: """View spectra for a measurement Parameters ---------- projData : projecData The project data site : str The site to view meas: str The measurement of the site to view chans : List[str], optional Channels to plot declevel : int, optional Decimation level to plot plotwindow : int, str, Dict, optional Windows to plot (local). If int, the window with local index plotwindow will be plotted. If string and "all", all the windows will be plotted if there are less than 20 windows, otherwise 20 windows throughout the whole spectra dataset will be plotted. If a dictionary, needs to have start and stop to define a range. specdir : str, optional String that specifies spectra directory for the measurement show : bool, optional Show the spectra plot save : bool, optional Save the plot to the images directory plotoptions : Dict, optional Dictionary of plot options Returns ------- matplotlib.pyplot.figure or None A matplotlib figure unless the plot is not shown and is saved, in which case None and the figure is closed. """ options = {} options["chans"]: List[str] = [] options["declevel"]: int = 0 options["plotwindow"]: Union[int, Dict, str] = [0] options["specdir"]: str = projData.config.configParams["Spectra"]["specdir"] options["show"]: bool = True options["save"]: bool = False options["plotoptions"]: Dict = plotOptionsSpec() options = parseKeywords(options, kwargs) projectText("Plotting spectra for measurement {} and site {}".format(meas, site)) specReader = getSpecReader(projData, site, meas, **options) # channels dataChans = specReader.getChannels() if len(options["chans"]) > 0: dataChans = options["chans"] numChans = len(dataChans) # get windows numWindows = specReader.getNumWindows() sampleFreqDec = specReader.getSampleFreq() # get the window data windows = options["plotwindow"] if isinstance(windows, str) and windows == "all": if numWindows > 20: windows = list( np.linspace(0, numWindows, 20, endpoint=False, dtype=np.int32) ) else: windows = list(np.arange(0, numWindows)) elif isinstance(windows, int): windows = [windows] # if an integer, make it into a list elif isinstance(windows, dict): windows = list(np.arange(windows["start"], windows["stop"] + 1)) # create a figure plotfonts = options["plotoptions"]["plotfonts"] cmap = colorbarMultiline() fig = plt.figure(figsize=options["plotoptions"]["figsize"]) for iW in windows: if iW >= numWindows: break color = cmap(iW/numWindows) winData = specReader.readBinaryWindowLocal(iW) winData.view( fig=fig, chans=dataChans, label="{} to {}".format( winData.startTime.strftime("%m-%d %H:%M:%S"), winData.stopTime.strftime("%m-%d %H:%M:%S"), ), plotfonts=plotfonts, color=color, ) st = fig.suptitle( "Spectra plot, site = {}, meas = {}, fs = {:.2f} [Hz], decimation level = {:2d}".format( site, meas, sampleFreqDec, options["declevel"] ), fontsize=plotfonts["suptitle"], ) st.set_y(0.98) # put on axis labels etc for idx, chan in enumerate(dataChans): ax = plt.subplot(numChans, 1, idx + 1) plt.title("Amplitude {}".format(chan), fontsize=plotfonts["title"]) if len(options["plotoptions"]["amplim"]) == 2: ax.set_ylim(options["plotoptions"]["amplim"]) ax.set_xlim(0, specReader.getSampleFreq() / 2.0) plt.grid(True) # fig legend and formatting ax = plt.gca() h, l = ax.get_legend_handles_labels() fig.tight_layout(rect=[0.02, 0.02, 0.77, 0.92]) # legend axis legax = plt.axes(position=[0.77, 0.02, 0.23, 0.88], in_layout=False) plt.tick_params(left=False, labelleft=False, bottom=False, labelbottom="False") plt.box(False) legax.legend(h, l, loc="upper left", fontsize=plotfonts["legend"]) # plot show and save if options["save"]: impath = projData.imagePath filename = "spectraData_{}_{}_dec{}_{}".format( site, meas, options["declevel"], options["specdir"] ) savename = savePlot(impath, filename, fig) projectText("Image saved to file {}".format(savename)) if options["show"]: plt.show(block=options["plotoptions"]["block"]) if not options["show"] and options["save"]: plt.close(fig) return None return fig
import numpy as np import matplotlib.pyplot as plt import matplotlib plt.box(False) import os from progressbar import * os.system('rm *.png') n_points = 20 points = np.random.rand(n_points, 2)*10 # points = np.loadtxt('points.txt'); n_points=100 force = np.zeros((n_points, 2)) vel = np.zeros((n_points, 2)) linewidth = np.zeros((n_points, n_points)) speed = 3 nx = 4 mv1 = np.random.rand(n_points, 2)*speed-speed/2 mv2 = np.random.rand(n_points, 2)*speed-speed/2 mv3 = -mv1 - mv2 export = False if export: matplotlib.use('macosx') fadeout = 50 its = 3000 spe = 500 plot = False widgets = ['Generating: ', Percentage(), ' ', Bar(marker='=',left='[',right=']'), ' ', ETA(), '']
def draw_singletile(tile_doy, tile_names=None, interp_doy=None, pdfsave_file=None): #-- global marker_size = 10 monthbarsize = 0.025 ndates = len(tile_doy) plt.figure(figsize=(6, 1)) ax = plt.gca() if tile_names is None: plt.scatter(tile_doy, ndates * [0], s=marker_size, zorder=1) else: plt.scatter(tile_doy, ndates * [0], s=marker_size, zorder=1, label=tile_names[i]) xmin, xmax = plt.xlim() ymin, ymax = plt.ylim() # Arrow configuration hw = 1. / 2 * (ymax - ymin) # arrow head width hl = 1. / 30. * (xmax - xmin) # arrow head length ohg = 0.3 ax.arrow(xmin, 0, xmax - xmin + 15, 0., color=mcolors.CSS4_COLORS['gray'], fc=mcolors.CSS4_COLORS['gray'], ec=mcolors.CSS4_COLORS['gray'], lw=1, head_width=hw, head_length=hl, overhang=ohg, length_includes_head=True, clip_on=False, zorder=-1) ax.set_ylim([-0.05, 0.15]) min_doy = min(tile_doy) max_doy = max(tile_doy) plt.plot([0, 0], [-monthbarsize, monthbarsize], mcolors.CSS4_COLORS['orange']) #-- ~month bar [check] for idx, m in enumerate(month_names): if idx == 0: xm = 4 else: xm = np.cumsum(monthday)[idx - 1] if xm < min_doy or xm > max_doy: continue plt.plot([np.cumsum(monthday)[idx], np.cumsum(monthday)[idx]], [-monthbarsize, monthbarsize], mcolors.CSS4_COLORS['orange']) #-- ~month bar [check] plt.annotate(m, (xm, 0.05), xycoords='data', color=mcolors.CSS4_COLORS['orange']) #-- Dashed red lines for interpolated doy if not interp_doy is None: for dd in interp_doy: plt.plot([dd, dd], [0, (T - 1) / T], 'r--', dashes=(5, 10), lw=0.5, zorder=-1) plt.xticks([]) plt.yticks([]) plt.box(False) if not tile_names is None: plt.legend(bbox_to_anchor=(1.25, 1)) if not pdfsave_file is None: plt.savefig(pdfsave_file, bbox_inches='tight') plt.show()
def racing_barchart(year): dff = df[df['year'].eq(year)].sort_values(by='value', ascending=True).tail(10) ax.barh(dff['name'], dff['value'], color=[colors[group_lk[x]] for x in dff['name']]) dx = dff['value'].max() / 200 for i, (value, name) in enumerate(zip(dff['value'], dff['name'])): #print(value-dx,value+dx,i) ax.text(value - dx, i, name, size=14, weight=600, ha='right', va='bottom') ax.text(value - dx, i - .25, group_lk[name], size=10, color='#444444', ha='right', va='baseline') ax.text(value + dx, i, f'{value:,.0f}', size=14, ha='left', va='center') ax.text(1, 0.4, year, transform=ax.transAxes, color='#777777', size=46, ha='right', weight=800) ax.text(0, 1.06, 'Population (X thousand)', transform=ax.transAxes, size=12, color='#777777') ax.xaxis.set_major_formatter(ticker.StrMethodFormatter('{x:,.0f}')) ax.xaxis.set_ticks_position('top') ax.tick_params(axis='x', colors='#777777', labelsize=12) ax.set_yticks([]) ax.margins(0, 0.01) ax.grid(which='major', axis='x', linestyle='-') ax.set_axisbelow(True) ax.text(0, 1.10, 'The most populous cities in the world from 1500 to 2019', transform=ax.transAxes, size=20, weight=600, ha='left') ax.text(1, 0, 'by Developers Hub', transform=ax.transAxes, ha='right', color='#777777', bbox=dict(facecolor='white', alpha=0.8, edgecolor='white')) plt.box(False)
def plotCentroids(time, centroids, stimTimes, time_ephys, ephys, scaled=False, colors=None, xlabel='', ylabel='', title=''): """ Plots centroids resulting from some clustering method Parameters: time - Time vectors for centroids (optical samplign interval) centroids - Array of shape (M, N), where M is the number of centroids, and N is the # number of features (or time points) stimTimes - Times of stimulus onsets for overlaying vertical dashed lines time_ephys - Time axis for ephys data (usually sampled at higher rate) ephys - Ephys time series scaled - Boolean; If true, scales centroids individually, else scales jointly. colors - Array of shape (M,3) or (M,4). Colormap to use for plotting centroids """ import apCode.SignalProcessingTools as spt import seaborn as sns import numpy as np import matplotlib.pyplot as plt if scaled: centroids = spt.standardize(centroids, axis=1) else: centroids = spt.standardize(centroids) ephys = spt.standardize(ephys) n_clusters = np.shape(centroids)[0] if np.any(colors == None): colors = np.array( sns.color_palette('colorblind', np.shape(centroids)[0])) elif np.shape(colors)[0] < np.shape(centroids)[0]: colors = np.tile(colors, (np.shape(centroids)[0], 1)) colors = colors[:np.shape(centroids)[0], :] if np.any(time == None): time = np.arange(np.shape(centroids)[1]) plt.style.use(['dark_background', 'seaborn-poster']) for cc in np.arange(np.shape(centroids)[0]): plt.plot(time, centroids[cc, :] - np.mean(centroids[cc, :]) - cc, color=colors[cc, :]) plt.plot(time_ephys, ephys - np.mean(ephys) - cc - 1, color=colors[0, :]) yt = np.arange(n_clusters + 1) ytl = list(yt) ytl[-1] = 'ephys' plt.yticks(-yt, ytl) plt.xlabel(xlabel, fontsize=16) plt.ylabel(ylabel, fontsize=16) plt.box('off') plt.title(title, fontsize=20) plt.grid('off') plt.xlim(time[0], time[-1]) for st in stimTimes: plt.axvline(x=st, ymin=0, ymax=1, alpha=0.3, color='w', linestyle='--')
wake_ep = loadEpoch(data_directory, 'wake', episodes) if ns > 1: sleep_ep = loadEpoch(data_directory, 'sleep') # from functions import computeAngularTuningCurves tuning_curves = computeAngularTuningCurves(spikes, position['ry'], wake_ep, 60) from functions import smoothAngularTuningCurves tuning_curves = smoothAngularTuningCurves(tuning_curves, 10, 2) #Determine the number of raws raws = round(len(spikes) / 5) plt.figure(figsize=(40, 200)) for i, n in enumerate(selection): ax = plt.subplot(2, raws + 1, i + 1, projection='polar') plt.plot(tuning_curves[n], color='lightblue') plt.box(on=0) plt.thetagrids(labels=None) # plt.title('Neuron' + ' ' + str(i) , loc ='center', pad=25) plt.subplots_adjust(wspace=0.4, hspace=2, top=0.85) plt.show() plt.savefig(data_directory + '/plots' + '/HD.pdf') """ plt.figure(figsize=(20,100)) for i, n in enumerate(tuning_curves.columns): plt.subplot(5,raws,i+1) plt.polar(tuning_curves[n], color = 'darkorange') plt.title('Neuron' + ' ' + str(i) , loc ='center', pad=2) plt.xticks(['N', '', 'W', '', 'S', 'E', '']) plt.subplots_adjust(wspace=0.4, hspace=1, top = 1.3) plt.show() """
def QvalueFitting(data, title, plot=True, fitting=True): '''2成分の減衰振動を仮定して、Q値を求める関数。 [メモ] boundに張り付いていない場合フィットできている気がする。張り付いていると、フィットはできていない。違う周波数を仮定したり、項を増やすとかそういうのが必要な感じがする。 Parameter --------- data : miyopy.types.timeseries.Timeseries miyopyのTimeseriesクラス。なんちゃってStep応答の時系列をフィットするので、t=0で値がゼロになっていないとフィッティングができない。なので、予めデータの開始時刻を切り取る必要があることに注意。 title : str タイトル。切り取ったデータのタイトル。../event/以下にこのタイトル名のディレクトリが作られる。 plot : bool Trueならプロットをする。デフォルトではTrue。 fitting : bool Trueならフィッティングをする。デフォルトではTrue。 Return ------ f0 : Q0 : f0 : Q0 : ''' def func(t, a0, tau0, f0, phi0, b0, a1, tau1, f1, phi1): y = b0 + a0 * np.exp(-(t) / tau0) * np.cos(2 * np.pi * (f0) * t + np.deg2rad(phi0)) y += a1 * np.exp(-(t) / tau1) * np.cos(2 * np.pi * (f1) * t + np.deg2rad(phi1)) return y time = np.arange(len(data.timeseries)) / data._fs data.timeseries = data.timeseries - data.timeseries[0] try: bounds = { 'Other': ([-10, 0, 0.9, -180, -10, -1, 0, 3.5, -180], [+10, 5, 1.3, +180, +10, +1, 5, 3.9, +180]), '20K': ([-10, 0, 1.0, -180, -10, -1, 0, 3.6, -180], [+10, 5, 1.2, +180, +10, +1, 5, 8.8, +180]) } if '20K' in title: popt, pcov = curve_fit( func, time, data.timeseries, bounds=bounds['20K'], absolute_sigma=False, ) else: popt, pcov = curve_fit( func, time, data.timeseries, bounds=bounds['Other'], absolute_sigma=False, ) except Exception as e: print e exit() text = text_param(popt, pcov, title, data) f0, Q0 = popt[2], popt[1] * popt[2] * np.pi f1, Q1 = popt[7], popt[6] * popt[7] * np.pi plot_cov(pcov, data, title) if plot: plt.figure(figsize=(20, 7)) plt.subplot(121) plt.plot(time, data.timeseries, label=data._name) plt.xlabel('Time [sec] ') plt.ylabel('Value') plt.title(title) plt.plot(data._time, func(data._time, *popt), '-', markersize=1, label='fit') plt.legend() plt.subplot(122) plt.tick_params(labelbottom="off", bottom="off") plt.tick_params(labelleft="off", left="off") plt.box("off") plt.text(0.0, -0.0, text, fontsize=15, fontname='monospace') fname = '{0}/Timeseries_{2}_{1}.png'.format( title, data._name.replace('K1:', ''), data._t0) plt.savefig(fname) plt.close() cmd = 'open {0}'.format(fname) ret = subprocess.check_call(cmd.split(" ")) return f0, Q0, f1, Q1
def plot_taylor_axes(axes, cax, option): ''' Plot axes for Taylor diagram. Plots the x & y axes for a Taylor diagram using the information provided in the AXES dictionary returned by the GET_TAYLOR_DIAGRAM_AXES function. INPUTS: axes : data structure containing axes information for Taylor diagram cax : handle for plot axes option : data structure containing option values. (Refer to GET_TAYLOR_DIAGRAM_OPTIONS function for more information.) option['colcor'] : CORs grid and tick labels color (Default: blue) option['colrms'] : RMS grid and tick labels color (Default: green) option['colstd'] : STD grid and tick labels color (Default: black) option['numberpanels'] : number of panels (quadrants) to use for Taylor diagram option['tickrms'] : RMS values to plot gridding circles from observation point option['titlecor'] : title for CORRELATION axis option['titlerms'] : title for RMS axis option['titlestd'] : title for STD axis OUTPUTS: ax: returns a list of handles of axis labels Author: Peter A. Rochford Acorn Science & Innovation [email protected] Created on Dec 3, 2016 Revised on Dec 31, 2018 Author: Peter A. Rochford Symplectic, LLC www.thesymplectic.com [email protected] ''' ax = [] axlabweight = 'bold' fontSize = rcParams.get('font.size') + 2 lineWidth = rcParams.get('lines.linewidth') if option['numberpanels'] == 1: # Single panel if option['titlestd'] == 'on': handle = plt.ylabel('Standard Deviation', color=option['colstd'], fontweight=axlabweight, fontsize=fontSize) ax.append(handle) if option['titlecor'] == 'on': pos1 = 45 DA = 15 lab = 'Correlation Coefficient' c = np.fliplr([np.linspace(pos1 - DA, pos1 + DA, len(lab))])[0] dd = 1.1 * axes['rmax'] for ii, ith in enumerate(c): handle = plt.text(dd * np.cos(ith * np.pi / 180), dd * np.sin(ith * np.pi / 180), lab[ii]) handle.set(rotation=ith - 90, color=option['colcor'], horizontalalignment='center', verticalalignment='bottom', fontsize=fontSize, fontweight=axlabweight) ax.append(handle) if option['titlerms'] == 'on': lab = 'RMSD' pos1 = option['titlermsdangle'] DA = 10 c = np.fliplr([np.linspace(pos1 - DA, pos1 + DA, len(lab))])[0] if option['tickrms'][0] > 0: dd = 0.8 * option['tickrms'][0] + 0.2 * option['tickrms'][1] else: dd = 0.8 * option['tickrms'][1] + 0.2 * option['tickrms'][2] # Adjust spacing of label letters if on too small an arc posFraction = dd / axes['rmax'] if posFraction < 0.35: DA = 2 * DA c = np.fliplr([np.linspace(pos1 - DA, pos1 + DA, len(lab))])[0] # Write label in a circular arc for ii, ith in enumerate(c): xtextpos = axes['dx'] + dd * np.cos(ith * np.pi / 180) ytextpos = dd * np.sin(ith * np.pi / 180) handle = plt.text(xtextpos, ytextpos, lab[ii]) handle.set(rotation=ith - 90, color=option['colrms'], horizontalalignment='center', verticalalignment='top', fontsize=fontSize, fontweight=axlabweight) ax.append(handle) else: # Double panel if option['titlestd'] == 'on': handle = plt.xlabel('Standard Deviation', color=option['colstd'], fontweight=axlabweight, fontsize=fontSize) ax.append(handle) if option['titlecor'] == 'on': pos1 = 90 DA = 25 lab = 'Correlation Coefficient' c = np.fliplr([np.linspace(pos1 - DA, pos1 + DA, len(lab))])[0] dd = 1.1 * axes['rmax'] # Write label in a circular arc for ii, ith in enumerate(c): handle = plt.text(dd * np.cos(ith * np.pi / 180), dd * np.sin(ith * np.pi / 180), lab[ii]) handle.set(rotation=ith - 90, color=option['colcor'], horizontalalignment='center', verticalalignment='bottom', fontsize=fontSize, fontweight=axlabweight) ax.append(handle) if option['titlerms'] == 'on': lab = 'RMSD' pos1 = option['titlermsdangle'] DA = 10 c = np.fliplr([np.linspace(pos1 - DA, pos1 + DA, len(lab))])[0] if option['tickrms'][0] > 0: dd = 0.7 * option['tickrms'][0] + 0.3 * option['tickrms'][1] else: dd = 0.7 * option['tickrms'][1] + 0.3 * option['tickrms'][2] # Adjust spacing of label letters if on too small an arc posFraction = dd / axes['rmax'] if posFraction < 0.35: DA = 2 * DA c = np.fliplr([np.linspace(pos1 - DA, pos1 + DA, len(lab))])[0] for ii, ith in enumerate(c): xtextpos = axes['dx'] + dd * np.cos(ith * np.pi / 180) ytextpos = dd * np.sin(ith * np.pi / 180) handle = plt.text(xtextpos, ytextpos, lab[ii]) handle.set(rotation=ith - 90, color=option['colrms'], horizontalalignment='center', verticalalignment='bottom', fontsize=fontSize, fontweight=axlabweight) ax.append(handle) # Set color of tick labels to that specified for STD contours plt.gca().tick_params(axis='x', colors=option['colstd']) plt.gca().tick_params(axis='y', colors=option['colstd']) # VARIOUS ADJUSTMENTS TO THE PLOT: cax.set_aspect('equal') plt.box(on=None) # set axes limits, set ticks, and draw axes lines if option['numberpanels'] == 2: xtick = [-option['tickstd'], option['tickstd']] xtick = np.concatenate((-option['tickstd'][1:], option['tickstd']), axis=None) xtick = np.sort(xtick) plt.xticks(xtick) axislim = [axes['rmax'] * x for x in [-1, 1, 0, 1]] plt.axis(axislim) plt.plot([-axes['rmax'], axes['rmax']], [0, 0], color=axes['tc'], linewidth=lineWidth + 1) plt.plot([0, 0], [0, axes['rmax']], color=axes['tc']) # hide y-axis line plt.gca().axes.get_yaxis().set_visible(False) else: ytick, ylab = plt.yticks() ytick = list(filter(lambda x: x >= 0 and x <= axes['rmax'], ytick)) axislim = [axes['rmax'] * x for x in [0, 1, 0, 1]] plt.axis(axislim) plt.xticks(ytick) plt.yticks(ytick) plt.plot([0, axes['rmax']], [0, 0], color=axes['tc'], linewidth=lineWidth + 2) plt.plot([0, 0], [0, axes['rmax']], color=axes['tc'], linewidth=lineWidth + 1) return ax
# load packages from matplotlib import pyplot as plt import numpy as np from sympy import * from matplotlib.animation import FuncAnimation print("Ready") # define figure and axes and set some graphing parameters fig, ax = plt.subplots(figsize=(4, 4)) ax.set_aspect(1) plt.rc('font', size=16) marker_style = dict(linestyle='none', markersize=10, markeredgecolor='k') plt.box(on=None) fig.set_facecolor('#c0c0c0') plt.rcParams['savefig.facecolor'] = '#c0c0c0' ax.spines['bottom'].set_position('zero') ax.spines['left'].set_position('zero') # plot the axes (min_plot, max_plot) = (0, 10) plt.xticks(np.arange(min_plot, max_plot + 1, 2), fontsize=10) plt.yticks(np.arange(min_plot, max_plot + 1, 2), fontsize=10) plt.xlim([min_plot - 1, max_plot + 1]) plt.ylim([min_plot - 1, max_plot + 1]) ax.annotate('', xy=(min_plot, max_plot + 1), xytext=(min_plot, min_plot - 0.1),
def establishAxes(fig, options, data): axDict = {} options.axLeft = 0.11 options.axWidth = 0.85 if options.mode in ([ 'blocks', 'contigs', 'contigPaths', 'scaffPaths', 'scaffolds' ]): if options.mode == 'contigPaths' or options.mode == 'scaffPaths': axDict['crazy'] = None axDict['main'] = fig.add_axes( [options.axLeft, 0.07, options.axWidth, 0.66]) plt.box(on=False) axDict['blowUp'] = fig.add_axes( [options.axLeft, 0.75, options.axWidth, 0.20]) plt.box(on=False) else: axDict['main'] = fig.add_axes( [options.axLeft, 0.07, options.axWidth, 0.58]) plt.box(on=False) axDict['crazy'] = fig.add_axes([ options.axLeft, 0.68, # 0.655 options.axWidth, 0.06 ]) # 0.085 plt.box(on=False) axDict['blowUp'] = fig.add_axes( [options.axLeft, 0.75, options.axWidth, 0.20]) plt.box(on=False) else: axDict['main'] = fig.add_axes( [options.axLeft, 0.07, options.axWidth, 0.60]) plt.box(on=False) axDict['blowUp'] = fig.add_axes( [options.axLeft, 0.71, options.axWidth, 0.27]) plt.box(on=False) data.axDict = axDict return (axDict)
def save_dreams(basedir, agent, data, embed, image_pred, obs_type='lidar', summary_length=5, skip_frames=10): """ Perform dreaming and save the imagined sequences as images. `basedir`: base log dir where the images will be stored `agent`: instance of dreamer `embed`: tensor of embeds, shape (B, E) where B is the episode length `data`: dictionary of observed data, it contains observation and camera images `image_pred`: distribution of predicted reconstructions `obs_type`: observation type, either lidar or lidar_occupancy """ imagedir = basedir / "images" imagedir.mkdir(parents=True, exist_ok=True) if obs_type == 'lidar': truth = data['lidar'][:1] + 0.5 recon = image_pred.mode()[:1] init, _ = agent._dynamics.observe(embed[:1, :summary_length], data['action'][:1, :summary_length]) init = {k: v[:, -1] for k, v in init.items()} prior = agent._dynamics.imagine(data['action'][:1, summary_length:], init) openl = agent._decode(agent._dynamics.get_feat(prior)).mode() model = tf.concat([recon[:, :summary_length] + 0.5, openl + 0.5], 1) truth_img = tools.lidar_to_image(truth) model_img = tools.lidar_to_image(model) elif obs_type == 'lidar_occupancy': truth_img = data['lidar_occupancy'][:1] recon = image_pred.mode()[:1] recon = tf.cast(recon, tf.float32) # concatenation requires same type init, _ = agent._dynamics.observe(embed[:1, :summary_length], data['action'][:1, :summary_length]) init = {k: v[:, -1] for k, v in init.items()} prior = agent._dynamics.imagine(data['action'][:1, summary_length:], init) openl = agent._decode(agent._dynamics.get_feat(prior)).mode() openl = tf.cast(openl, tf.float32) model_img = tf.concat( [recon[:, :summary_length], openl], 1) # note: recon and open_l is already 0 or 1, no need scaling else: raise NotImplementedError( f"save dreams not implemented for {obs_type}") timestamp = time.time() plt.box(False) plt.axis(False) plt.ion() for imgs, prefix in zip([data['image'], truth_img, model_img], ["camera", "true", "recon"]): for ep in range(imgs.shape[0]): for t in range(0, imgs.shape[1], skip_frames): # plot black/white without borders plt.imshow(imgs[ep, t, :, :, :], cmap='binary') plt.savefig( f"{imagedir}/frame_{timestamp}_{obs_type}_{prefix}_{ep}_{t}.png", bbox_inches='tight', transparent=True, pad_inches=0)
fontSIZE = 21 import matplotlib as mpl label_size = fontSIZE mpl.rcParams['xtick.labelsize'] = label_size mpl.rcParams['ytick.labelsize'] = label_size stress_plot = [1. / 4000 * x for x in tau] essi_stress_plot = [1. / 4000 * x for x in stress] plt.plot(essiGamma, essi_stress_plot, 'k-', label=' ESSI', linewidth=5.0) plt.plot(gamma, stress_plot, 'r--', label='Input', linewidth=5.0) plt.legend(loc=2, prop={'size': fontSIZE}) plt.xlabel('Strain / (unitless)', fontsize=fontSIZE) plt.ylabel('Stress / (kPa)', fontsize=fontSIZE) plt.title('Material Behavior: Stress-Strain', fontsize=fontSIZE) plt.grid() plt.box() plt.savefig('full-loop.pdf', transparent=True, bbox_inches='tight') plt.show() # # ============================================ # # Figure 3 # # Plot the G/Gmax # # ============================================ # # avoid the divide by zero # stress[0]=stress[1] # strain[0]=strain[1] # essiG = [a/b/2. for a,b in zip(stress, strain)] # essiGGmax = [item/Gmax for item in essiG] # plt.semilogx(essiGamma, essiGGmax, label='ESSI') # plt.semilogx(gamma , GGmax , label='Input')
def plotDifficulty(self, **kwargs): conds = (self * Condition()).fetch('cond_tuple') min_difficulty = np.min([cond['difficulty'] for cond in conds]) params = { 'probe_colors': [[1, 0, 0], [0, .5, 1]], 'trial_bins': 10, 'range': 0.9, 'xlim': (-1, ), 'ylim': (min_difficulty - 0.6, ), **kwargs } def plot_trials(trials, **kwargs): conds, trial_idxs = ((Trial & trials) * Condition()).fetch( 'cond_tuple', 'trial_idx') offset = ((trial_idxs - 1) % params['trial_bins'] - params['trial_bins'] / 2) * params['range'] * 0.1 difficulties = [cond['difficulty'] for cond in conds] plt.scatter(trial_idxs, difficulties + offset, zorder=10, **kwargs) # correct trials correct_trials = ((LiquidDelivery * self).proj( selected='ABS(time - end_time)<200 AND (time - start_time)>0') & 'selected > 0') # missed trials missed_trials = (self & AbortedTrial).proj() # incorrect trials incorrect_trials = ((self - correct_trials) - missed_trials).proj() print('correct: {}, incorrect: {}, missed: {}'.format( len(correct_trials), len(incorrect_trials), len(missed_trials))) print('correct: {}, incorrect: {}, missed: {}'.format( len(np.unique(correct_trials.fetch('trial_idx'))), len(np.unique(incorrect_trials.fetch('trial_idx'))), len(np.unique(missed_trials.fetch('trial_idx'))))) # plot trials fig = plt.figure(figsize=(10, 5), tight_layout=True) plot_trials(correct_trials, s=4, c=np.array( params['probe_colors'])[correct_trials.fetch('probe') - 1]) plot_trials(incorrect_trials, s=4, marker='o', facecolors='none', edgecolors=[.3, .3, .3], linewidths=.5) plot_trials(missed_trials, s=.1, c=[[0, 0, 0]]) # plot info plt.xlabel('Trials') plt.ylabel('Difficulty') plt.title('Animal:%d Session:%d' % (Session() & self).fetch1('animal_id', 'session')) plt.yticks( range(int(min(plt.gca().get_ylim())), int(max(plt.gca().get_ylim())) + 1)) plt.ylim(params['ylim'][0]) plt.xlim(params['xlim'][0]) plt.gca().xaxis.set_ticks_position('none') plt.gca().yaxis.set_ticks_position('none') plt.box(False) plt.show()
print single.color_scheme latent_dim = int(sys.argv[1]) vae = VAE(784, [500], latent_dim) vae.train(X, batch_size=1000, num_epochs=100, rerun=False, model_filename='nine_objects_l_%d' % latent_dim) #latent_z = np.random.uniform(low=-6, high=6, size=(10000, 40)) latent_z = vae.encode(X) x_reconstructed = vae.decode(latent_z) #for i in range(len(x_reconstructed)): for n, i in enumerate( np.random.randint(low=0, high=x_reconstructed.shape[0], size=1000)): print i plt.clf() plt.imshow(x_reconstructed[i].reshape(28, 28), cmap='Greys') plt.tick_params(axis='both', which='both', bottom='off', top='off', labelbottom='off', right='off', left='off', labelleft='off') plt.box(on=False) plt.savefig('/home/mukarram/novel_rec/l_%d_v_%d.pdf' % (latent_dim, n + 1), bbox_inches='tight')