我们从Python开源项目中,提取了以下13个代码示例,用于说明如何使用matplotlib.pyplot.hexbin()。
def find_shootingPcts(shot_df, gridNum): x = shot_df.LOC_X[shot_df['LOC_Y']<425.1] y = shot_df.LOC_Y[shot_df['LOC_Y']<425.1] # Grabbing the x and y coords, for all made shots x_made = shot_df.LOC_X[(shot_df['SHOT_MADE_FLAG']==1) & (shot_df['LOC_Y']<425.1)] y_made = shot_df.LOC_Y[(shot_df['SHOT_MADE_FLAG']==1) & (shot_df['LOC_Y']<425.1)] #compute number of shots made and taken from each hexbin location hb_shot = plt.hexbin(x, y, gridsize=gridNum, extent=(-250,250,425,-50)); plt.close() hb_made = plt.hexbin(x_made, y_made, gridsize=gridNum, extent=(-250,250,425,-50)); plt.close() #compute shooting percentage ShootingPctLocs = hb_made.get_array() / hb_shot.get_array() ShootingPctLocs[np.isnan(ShootingPctLocs)] = 0 #makes 0/0s=0 shot_count_all = len(shot_df.index) # Returning all values return (ShootingPctLocs, hb_shot), shot_count_all
def make_2d_hist(data, name): f = plt.figure() X,Y = np.meshgrid(range(data.shape[0]), range(data.shape[1])) im = plt.pcolormesh(X,Y,data.transpose(), cmap='seismic') plt.colorbar(im, orientation='vertical') # plt.hexbin(data,data) # plt.show() f.savefig(pjoin(FLAGS.output_dir, name + '.png')) plt.close() # def make_2d_hexbin(data, name): # f = plt.figure() # X,Y = np.meshgrid(range(data.shape[0]), range(data.shape[1])) # plt.hexbin(X, data) # # plt.show() # f.savefig(pjoin(FLAGS.output_dir, name + '.png'))
def histogramnd(ax, data, **kwargs): """n-dimensional histogram seaborn based """ scatter_data_raw = data scatter_data_cols = ["x_%d" % (i,) for i in range(data.shape[1])] # prepare dataframe df = pd.DataFrame(scatter_data_raw, columns=scatter_data_cols) g = sns.PairGrid(df) # g.map_diag(plt.hist) g.map_diag(sns.kdeplot) g.map_offdiag(plt.hexbin, cmap="gray", gridsize=30, bins="log"); # logger.log(loglevel_debug, "dir(g)", dir(g)) # print g.diag_axes # print g.axes # for i in range(data.shape[1]): # for j in range(data.shape[1]): # 1, 2; 0, 2; 0, 1 # if i == j: # continue # # column gives x axis, row gives y axis, thus need to reverse the selection for plotting goal # # g.axes[i,j].plot(df["%s%d" % (self.cols_goal_base, j)], df["%s%d" % (self.cols_goal_base, i)], "ro", alpha=0.5) # g.axes[i,j].plot(df["x_%d" % (j,)], df["x_%d" % (i,)], "ro", alpha=0.5) plt.show() # run sns scattermatrix on dataframe # plot_scattermatrix(df, ax = None)
def plotHistogramStages(data, idx = (0,1), cls = -1, **args): """Plots joint distributions at each stage for an individual""" stages = stageIndex(data, cls = cls); stages = np.insert(np.append(stages, data.shape[0]), 0, 0); nstages = len(stages) - 1; for i in range(nstages): plt.subplot(1, nstages, i); plt.hexbin(data[stages[i]:stages[i+1],idx[0]], data[stages[i]:stages[i+1],idx[1]], **args);
def plot_voltage(network, boundaries=[]): """ Plot voltage at buses as hexbin Parameters ---------- network : PyPSA network container boundaries: list of 2 values, setting the lower and upper bound of colorbar Returns ------- Plot """ x = np.array(network.buses['x']) y = np.array(network.buses['y']) alpha = np.array(network.buses_t.v_mag_pu.loc[network.snapshots[0]]) fig,ax = plt.subplots(1,1) fig.set_size_inches(6,4) cmap = plt.cm.jet if not boundaries: plt.hexbin(x, y, C=alpha, cmap=cmap, gridsize=100) cb = plt.colorbar() elif boundaries: v = np.linspace(boundaries[0], boundaries[1], 101) norm = matplotlib.colors.BoundaryNorm(v, cmap.N) plt.hexbin(x, y, C=alpha, cmap=cmap, gridsize=100, norm=norm) cb = plt.colorbar(boundaries=v, ticks=v[0:101:10], norm=norm) cb.set_clim(vmin=boundaries[0], vmax=boundaries[1]) cb.set_label('Voltage Magnitude per unit of v_nom') network.plot(ax=ax,line_widths=pd.Series(0.5,network.lines.index), bus_sizes=0) plt.show()
def HexBin(xs, ys, **options): """Makes a scatter plot. xs: x values ys: y values options: options passed to pyplot.scatter """ options = _Underride(options, cmap=matplotlib.cm.Blues) pyplot.hexbin(xs, ys, **options)
def HexBin(xs, ys, **options): """Makes a scatter plot. xs: x values ys: y values options: options passed to plt.scatter """ options = _Underride(options, cmap=matplotlib.cm.Blues) plt.hexbin(xs, ys, **options)
def find_shootingPcts(shot_df, gridNum): x = shot_df.LOC_X[shot_df['LOC_Y']<425.1] y = shot_df.LOC_Y[shot_df['LOC_Y']<425.1] # Grabbing the x and y coords, for all made shots x_made = shot_df.LOC_X[(shot_df['SHOT_MADE_FLAG']==1) & (shot_df['LOC_Y']<425.1)] y_made = shot_df.LOC_Y[(shot_df['SHOT_MADE_FLAG']==1) & (shot_df['LOC_Y']<425.1)] #compute number of shots made and taken from each hexbin location hb_shot = plt.hexbin(x, y, gridsize=gridNum, extent=(-250,250,425,-50)); plt.close() hb_made = plt.hexbin(x_made, y_made, gridsize=gridNum, extent=(-250,250,425,-50)); plt.close() #compute shooting percentage ShootingPctLocs = hb_made.get_array() / hb_shot.get_array() ShootingPctLocs[np.isnan(ShootingPctLocs)] = 0 #makes 0/0s=0 shot_count_all = len(shot_df.index) # Returning all values return (ShootingPctLocs, hb_shot), shot_count_all #Drawing the outline of the court #Most of this code was recycled from Savvas Tjortjoglou [http://savvastjortjoglou.com]
def plot_hexbin(sol, var1, var2, save=False, save_as_png=True, fig_dpi=144): if save_as_png: save_as = 'png' else: save_as = 'pdf' MDL = sol.MDL filename = sol.filename.replace("\\", "/").split("/")[-1].split(".")[0] model = sol.get_model_type() if var1 == "R0": stoc1 = "R0" else: stoc1 = ''.join([i for i in var1 if not i.isdigit()]) stoc_num1 = [int(i) for i in var1 if i.isdigit()] try: x = MDL.trace(stoc1)[:,stoc_num1[0]-1] except: x = MDL.trace(stoc1)[:] if var2 == "R0": stoc2 = "R0" else: stoc2 = ''.join([i for i in var2 if not i.isdigit()]) stoc_num2 = [int(i) for i in var2 if i.isdigit()] try: y = MDL.trace(stoc2)[:,stoc_num2[0]-1] except: y = MDL.trace(stoc2)[:] xmin, xmax = min(x), max(x) ymin, ymax = min(y), max(y) fig, ax = plt.subplots(figsize=(4,4)) plt.grid(None) ax.set_xlim(xmin, xmax) ax.set_ylim(ymin, ymax) # plt.scatter(x, y) plt.hexbin(x, y, gridsize=20, cmap=plt.cm.Blues) plt.ticklabel_format(style='sci', axis='both', scilimits=(0,0)) plt.xticks(rotation=90) plt.locator_params(axis = 'y', nbins = 5) plt.locator_params(axis = 'x', nbins = 5) cb = plt.colorbar() cb.set_label('Number of observations') plt.yticks(fontsize=14) plt.xticks(fontsize=14) plt.ylabel("%s" %var2, fontsize=14) plt.xlabel("%s" %var1, fontsize=14) if save: save_where = '/Figures/Hexbins/%s/' %filename working_path = getcwd().replace("\\", "/")+"/" save_path = working_path+save_where print("\nSaving hexbin figure in:\n", save_path) if not path.exists(save_path): makedirs(save_path) fig.savefig(save_path+'Bivar-%s-%s_%s_%s.%s'%(model,filename,var1,var2,save_as), dpi=fig_dpi, bbox_inches='tight') plt.close(fig) return fig
def product_raw_stats(): df = pd.read_csv('/Users/srinath/playground/data-science/BimboInventoryDemand/train.csv') #df = pd.read_csv('/Users/srinath/playground/data-science/BimboInventoryDemand/trainitems300.csv') grouped = df.groupby(['Semana'])['Demanda_uni_equil'].mean() plt.figure(1, figsize=(20,10)) plt.subplot(321) plt.xlabel('Semana', fontsize=18) plt.ylabel('Mean', fontsize=18) #plt.yscale('log') #plt.xscale('log') plt.scatter(df['Semana'].values, df['Demanda_uni_equil'].values, alpha=0.5) grouped = df.groupby(['Semana'])['Demanda_uni_equil'].mean() plt.subplot(322) plt.xlabel('Slope', fontsize=18) plt.ylabel('Mean Error', fontsize=18) plt.scatter(grouped.index.values, grouped.values, alpha=0.5) grouped = df.groupby(['Semana', 'Producto_ID'])['Demanda_uni_equil'].mean() valuesDf = grouped.to_frame("Mean") valuesDf.reset_index(inplace=True) plt.subplot(323) plt.xlabel('Semana', fontsize=18) plt.ylabel('Mean Error', fontsize=18) plt.scatter(valuesDf['Semana'], valuesDf["Mean"], alpha=0.5) grouped = df.groupby(['Semana', 'Producto_ID'])['Demanda_uni_equil'].count() valuesDf = grouped.to_frame("count") valuesDf.reset_index(inplace=True) plt.subplot(324) X = valuesDf['Semana'] Y = valuesDf['Producto_ID'] plt.hexbin(valuesDf['Semana'], valuesDf['Producto_ID'], C=valuesDf['count'], cmap=CM.jet, gridsize=30, bins=50) plt.axis([X.min(), X.max(), Y.min(), Y.max()]) cb = plt.colorbar() cb.set_label('mean value') plt.tight_layout() plt.show()
def Histogram3D(xypoints, numbins): xpoints = map(lambda (x, y): x, xypoints) ypoints = map(lambda (x, y): y, xypoints) minx, maxx, miny, maxy = min(xpoints), max(xpoints), \ min(ypoints), max(ypoints) dx, dy = (maxx - minx) / float(numbins), (maxy - miny) / float(numbins) xedges = [0.0] + np.arange(minx, maxx + dx, dx) yedges = [0.0] + np.arange(miny, maxy + dy, dy) H, xedges, yedges = np.histogram2d(ypoints, xpoints, bins = (xedges, yedges), normed = False) print H, len(H), len(H[0]), type(H) print xedges, len(xedges) print yedges, len(yedges) plt.figure(1) plt.subplot(211) extent = [yedges[0], yedges[-1], xedges[-1], xedges[0]] plt.imshow(H, extent = extent, interpolation = 'nearest') plt.colorbar() plt.savefig('./output/foo-density-hist3d.png') plt.subplot(212) xidx = np.clip(np.digitize(xpoints, xedges), 0, H.shape[0]-1) yidx = np.clip(np.digitize(ypoints, yedges), 0, H.shape[1]-1) c = H[xidx, yidx] print c plt.scatter(xpoints, ypoints, c=c, alpha = 0.5) plt.colorbar() plt.savefig('./output/foo-density-scatter.png') plt.figure(2) plt.hexbin(xpoints, ypoints) plt.colorbar() plt.savefig('./output/foo-mpl-combined.png') hist_fxy = lambda x, y: get_xybin_value(x, y, xedges, yedges, H) P = plot3d(hist_fxy, (minx, maxx), (miny, maxy), adaptive = True, color = rainbow(60, 'rgbtuple'), max_bend = 0.1, max_depth = 15, mesh = False, aspect_ratio = 1) P.save('./output/foo-hist3d.png') list_plot3d_data = [] for x in xedges: for y in yedges: list_plot3d_data += [(x, y, get_xybin_value(x, y, xedges, yedges, H))] ## ## LP = list_plot3d(list_plot3d_data) LP.save('./output/foo-listplot3d-hist3d.png') ## def