我们从Python开源项目中,提取了以下20个代码示例,用于说明如何使用matplotlib.pyplot.hlines()。
def create_graph(): logfile = 'result/log' xs = [] ys = [] ls = [] f = open(logfile, 'r') data = json.load(f) print(data) for d in data: xs.append(d["iteration"]) ys.append(d["main/accuracy"]) ls.append(d["main/loss"]) plt.clf() plt.cla() plt.hlines(1, 0, np.max(xs), colors='r', linestyles="dashed") # y=-1, 1?????? plt.title(r"loss/accuracy") plt.plot(xs, ys, label="accuracy") plt.plot(xs, ls, label="loss") plt.legend() plt.savefig("result/log.png")
def lineplot(x=None, y=None, data=None, order=None, palette=None, linewidth=0, ax=None, orient="v", **kwargs): if ax is None: ax = plt.gca() if not order: order = y.unique() order = list(sorted(order)) func = plt.vlines if orient=="v" else plt.hlines for i, lev in enumerate(order): func(x[y == lev], i + 0.025, i + 0.975, colors=palette[i], linewidth=1) if len(order) < 2: ax.set(yticks=[]) else: ax.set(yticks=[0.5 + n for n in range(len(order))]) ax.set(yticklabels=order) ax.set(xlim=(0, max(x))) ax.set(ylim=(len(order), 0)) return ax
def linear_regression(): lr = LinearRegression() lr.fit(X_train, y_train) # Look at predictions on training and validation set print("RMSE on Training set :", rmse_cv(lr, train_split, y).mean()) y_train_pred = lr.predict(train_split) print('rmsle calculate by self:', rmsle(list(np.exp(y) - 1), list(np.exp(y_train_pred) - 1))) plt.scatter(y_train_pred, y_train_pred - y, c="blue", marker="s", label="Training data") plt.title("Linear regression") plt.xlabel("Predicted values") plt.ylabel("Residuals") plt.legend(loc="upper left") plt.hlines(y=0, xmin=10.5, xmax=13.5, color="red") plt.show() # Plot predictions plt.scatter(y_train_pred, y, c="blue", marker="s", label="Training data") plt.title("Linear regression") plt.xlabel("Predicted values") plt.ylabel("Real values") plt.legend(loc="upper left") plt.plot([10.5, 13.5], [10.5, 13.5], c="red") plt.show() return lr
def GDBT_regression(X=train_df_munged,Y=label_df['SalePrice']): est = GradientBoostingRegressor(n_estimators=50,max_depth=3,learning_rate=0.1) X_train,X_test,Y_train,Y_test = train_test_split(X,Y,test_size=0.3,random_state=0) est.fit(X_train,Y_train) y_train_pred = est.predict(X_test) plt.scatter(y_train_pred,y_train_pred - Y_test,c = 'blue',marker='s', label='error on training data') plt.title("Linear regression with GDBT") plt.xlabel("Predicted values") plt.ylabel("Residuals") plt.legend(loc="upper left") plt.hlines(y=0, xmin=10.5, xmax=13.5, color="red") plt.show() # Plot predictions plt.scatter(Y_test, y_train_pred, c="blue", marker="s", label="Training data") plt.title("Linear regression with GDBT") plt.xlabel("Predicted values") plt.ylabel("Real values") plt.legend(loc="upper left") plt.plot([10.5, 13.5], [10.5, 13.5], c="red") plt.show() print('rmse value:',rmse(Y_test,y_train_pred)) return est
def plot_samples(S, axis_list=None): plt.scatter(S[:, 0], S[:, 1], s=2, marker='o', zorder=10, color='steelblue', alpha=0.5) if axis_list is not None: colors = ['orange', 'red'] for color, axis in zip(colors, axis_list): axis /= axis.std() x_axis, y_axis = axis # Trick to get legend to work plt.plot(0.1 * x_axis, 0.1 * y_axis, linewidth=2, color=color) plt.quiver(0, 0, x_axis, y_axis, zorder=11, width=0.01, scale=6, color=color) plt.hlines(0, -3, 3) plt.vlines(0, -3, 3) plt.xlim(-3, 3) plt.ylim(-3, 3) plt.xlabel('x') plt.ylabel('y')
def graph_barcode(data, ph, homology_group=0): persistence = ph.transform(data) # this function just produces the barcode graph for each homology group xstart = [s[1][0] for s in persistence if s[0] == homology_group] xstop = [s[1][1] for s in persistence if s[0] == homology_group] y = [0.1 * x + 0.1 for x in range(len(xstart))] plt.hlines(y, xstart, xstop, color='b', lw=4) # Setup the plot ax = plt.gca() plt.ylim(0, max(y) + 0.1) ax.yaxis.set_major_formatter(plt.NullFormatter()) plt.xlabel('epsilon') plt.ylabel("Betti dim %s" % (homology_group,)) plt.show()
def plot(nb, pvec, avec, title): """Plotting function.""" pvecm = pvec.mean(1) avecm = avec.mean(1) plt.subplot(1, 4, nb) plt.vlines(pvecm, -10, 500, color='#ab4642') plt.hlines(avecm, -10, 500, color='slateblue') plt.xlabel('Frequency for phase (hz') plt.ylabel('Frequency for amplitude.mean(1)') plt.title(title) plt.xlim([0, 30]) plt.ylim([60, 200])
def plotFigure(self): print "plotFigure" xx = [] yy = [] for (x,y) in self.cp_data: xx.append(x) yy.append(y+1000) plt.scatter(xx , yy) xx = [] yy = [] for (x , y) in self.ori_data: xx.append(x) yy.append(y-1000) #print y plt.plot(xx , yy) #print self.KLinePointArr x1 = [x[0] for x in self.KLinePointArr if x[3] == HeiMoShui] y1 = [x[1] for x in self.KLinePointArr if x[3] == HeiMoShui] plt.scatter(x1, y1 , c = HeiMoShui , marker = 's') x1 = [x[0] for x in self.KLinePointArr if x[3] == HongMoShui] y1 = [x[1] for x in self.KLinePointArr if x[3] == HongMoShui] plt.scatter(x1, y1 , c = HongMoShui , marker = 's') x1 = [x[0] for x in self.KLinePointArr if x[3] == QianBi] y1 = [x[1] for x in self.KLinePointArr if x[3] == QianBi] plt.scatter(x1, y1 , c = QianBi , marker = 'x') #print self.keyPointArr for (xx,yy,ccolor) in self.keyPointArr: x = [xx - datetime.timedelta(days=20) , xx + datetime.timedelta(days=20)] y = [yy,yy] #plt.plot(x , y, color = ccolor) plt.hlines( y[0], x[0], x[1] , colors = ccolor , linestyles = "dashed") plt.show()
def plot_all_reliabilities(): """ Plot packet delivery ratio for all data sets :return: """ rel = [] avg = [] for filename in get_all_files(gl_dump_path): p = BasicProcessor(filename=filename) # compensate for the rest p.correct_timeline(clean_all=False) # plot the timeline # p.plot_timeline(writer=None) r, w = p.plot_motes_reliability(return_result=True) rel.append(r) avg.append(w) plt.figure(figsize=(7.5, 3.5)) bp = plt.boxplot(rel, flierprops={'linewidth':1.5}, showmeans=True) plt.hlines(0.95, xmin=0, xmax=9, linestyles='--', linewidth=1, label='0.95') x_axis = list(range(9)) labels = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII'] plt.xticks(x_axis, labels) plt.grid(True) plt.ylim((0.35, 1.1)) plt.legend(loc=4) plt.ylabel('PDR') set_box_plot(bp) plt.show()
def plot_delay_cdf(): plt.figure(figsize=(7.5, 3.5)) steps = [0.5, 1, 2, 5, 10] for idx, logfile in enumerate(get_all_files('../../data/raw/')): bp = BasicProcessor(filename=logfile) # delay_df.set_value(idx, 'set', logfile) delay_ser = pd.Series(bp.get_all_delays()) # print(delay_ser) counts = [] for step in steps: count = (delay_ser < step).sum() / len(delay_ser) counts.append(count) plt.plot([i+1 for i, _ in enumerate(steps)], counts, gl_line_color_map[idx], label=gl_legend_map[idx]) plt.hlines(0.95, xmin=0, xmax=len(steps)+1, linestyles='--', linewidth=2, label='0.95') x_axis = [0.5] + list(range(1, len(steps)+1)) + [len(steps)+0.5] labels = [''] + [str(step) for step in steps] + [''] plt.xticks(x_axis, labels) plt.ylim(0.4, 1.1) plt.legend(loc=0, ncol=3) plt.grid(True) plt.xlabel('Deadline, s') plt.ylabel('Packet ratio') plt.show()
def GDBT_regression(X=train_split,Y=y): est = GradientBoostingRegressor(n_estimators=75,max_depth=3,learning_rate=0.1) X_train,X_test,Y_train,Y_test = train_test_split(X,Y,test_size=0.3,random_state=0) est.fit(X_train,Y_train) y_train_pred = est.predict(X_test) plt.scatter(y_train_pred,y_train_pred - Y_test,c = 'blue',marker='s', label='error on training data') plt.title("Linear regression with GDBT") plt.xlabel("Predicted values") plt.ylabel("Residuals") plt.legend(loc="upper left") plt.hlines(y=0, xmin=10.5, xmax=13.5, color="red") plt.show() # Plot predictions plt.scatter(Y_test, y_train_pred, c="blue", marker="s", label="Training data") plt.title("Linear regression with GDBT") plt.xlabel("Predicted values") plt.ylabel("Real values") plt.legend(loc="upper left") plt.plot([10.5, 13.5], [10.5, 13.5], c="red") plt.show() print('rmse value:',rmsle(Y_test,y_train_pred)) return est # linear_regression() # ridge_regression() # Lasso_regression() #model = Elasticnet_regression() # ''' # predict final result # ''' # # # coefs,lasso = Lasso_regression() # selected_features = coefs[coefs['value'] != 0].index.values # train_new = train_split[selected_features]
def evaluate_sklearn_linear_regression(X_train, X_test, y_train, y_test): lr = LinearRegression() lr.fit(X_train, y_train) y_train_pred = lr.predict(X_train) y_test_pred = lr.predict(X_test) plt.scatter( y_train_pred, y_train_pred - y_train, c='blue', marker='o', label='Training data', ) plt.scatter( y_test_pred, y_test_pred - y_test, c='lightgreen', marker='s', label='Test data', ) plt.xlabel('Predicted values') plt.ylabel('Residuals') plt.legend(loc='upper left') plt.hlines(y=0, xmin=-10, xmax=50, lw=2, color='red') plt.xlim([-10, 50]) plt.show() print("MSE train: %.3f, test: %.3f" % ( mean_squared_error(y_train, y_train_pred), mean_squared_error(y_test, y_test_pred), )) print("R^2 train: %.3f, test: %.3f" % ( r2_score(y_train, y_train_pred), r2_score(y_test, y_test_pred), ))
def Hlines(ys, x1, x2, **options): """Plots a set of horizontal lines. Args: ys: sequence of y values x1: sequence of x values x2: sequence of x values options: keyword args passed to plt.vlines """ options = _UnderrideColor(options) options = _Underride(options, linewidth=1, alpha=0.5) plt.hlines(ys, x1, x2, **options)
def _print_junction(self): bw = self.game_params['board_width'] lw = self.game_params['lane_width'] # road marks plt.vlines(lw, -bw, -lw) plt.vlines(-lw, -bw, -lw) plt.vlines(-lw, lw, bw) plt.vlines(lw, lw, bw) plt.hlines(-lw, -bw, -lw) plt.hlines(-lw, lw, bw) plt.hlines(lw, -bw, -lw) plt.hlines(lw, lw, bw) # lane marks plt.vlines(0, -bw, -lw, linestyles='dashed') plt.vlines(0, lw, bw, linestyles='dashed') plt.hlines(0, -bw, -lw, linestyles='dashed') plt.hlines(0, lw, bw, linestyles='dashed') # stopping lines plt.hlines(-lw, 0, lw, linestyles='solid') plt.hlines(-lw-0.01, 0, lw, linestyles='solid') plt.hlines(lw, -lw, 0, linestyles='solid') plt.hlines(lw+0.01, -lw, 0, linestyles='solid') plt.vlines(-lw, -lw, 0, linestyles='solid') plt.vlines(-lw-0.01, -lw, 0, linestyles='solid') plt.vlines(lw, 0, lw, linestyles='solid') plt.vlines(lw+0.01, 0, lw, linestyles='solid') # print rails # self.ax.add_patch(Circle(xy=(lw,-lw), radius=lw/2, edgecolor='k', facecolor='none')) # self.ax.add_patch(Circle(xy=(-lw,-lw), radius=3*lw/2, edgecolor='k', facecolor='none'))
def plotFigure(self): print "plotFigure" xx = [] yy = [] for (x,y) in self.cp_data: xx.append(x) yy.append(y+150) plt.scatter(xx , yy) xx = [] yy = [] for (x , y , vol) in self.ori_data: xx.append(x) yy.append(y-150) #print y plt.plot(xx , yy) #print self.KLinePointArr x1 = [x[0] for x in self.KLinePointArr if x[3] == HeiMoShui] y1 = [x[1] for x in self.KLinePointArr if x[3] == HeiMoShui] plt.scatter(x1, y1 , c = HeiMoShui , marker = 's') x1 = [x[0] for x in self.KLinePointArr if x[3] == HongMoShui] y1 = [x[1] for x in self.KLinePointArr if x[3] == HongMoShui] plt.scatter(x1, y1 , c = HongMoShui , marker = 's') x1 = [x[0] for x in self.KLinePointArr if x[3] == QianBi] y1 = [x[1] for x in self.KLinePointArr if x[3] == QianBi] plt.scatter(x1, y1 , c = QianBi , marker = 'x') #print self.keyPointArr for (xx,yy,ccolor) in self.keyPointArr: x = [xx - datetime.timedelta(days=5) , xx + datetime.timedelta(days=5)] y = [yy,yy] #plt.plot(x , y, color = ccolor) plt.hlines( y[0], x[0], x[1] , colors = ccolor , linestyles = "dashed") #### ???? #xx = [x[0] for x in self.keyPointArr] xx = [x[0] for x in self.ori_data ] x_vol = [x[0] for x in self.ori_data if x[0] in xx] y_vol = [x[2] / 10000.0 for x in self.ori_data if x[0] in x_vol] plt.bar(x_vol , y_vol) plt.show()
def ridge_regression(): ridge = RidgeCV(alphas=[0.01, 0.03, 0.06, 0.1, 0.3, 0.6, 1, 3, 6, 10, 30, 60]) ridge.fit(X_train, y_train) alpha = ridge.alpha_ print("Best alpha :", alpha) print("Try again for more precision with alphas centered around " + str(alpha)) ridge = RidgeCV(alphas=[alpha * .6, alpha * .65, alpha * .7, alpha * .75, alpha * .8, alpha * .85, alpha * .9, alpha * .95, alpha, alpha * 1.05, alpha * 1.1, alpha * 1.15, alpha * 1.25, alpha * 1.3, alpha * 1.35, alpha * 1.4], cv=10) ridge.fit(X_train, y_train) alpha = ridge.alpha_ print("Best alpha :", alpha) print("Ridge RMSE on Training set :", rmse_cv(ridge, X_train, y_train).mean()) print("Ridge RMSE on Test set :", rmse_cv(ridge, X_test, y_test).mean()) y_train_rdg = ridge.predict(X_train) y_test_rdg = ridge.predict(X_test) # Plot residuals plt.scatter(y_train_rdg, y_train_rdg - y_train, c="blue", marker="s", label="Training data") plt.scatter(y_test_rdg, y_test_rdg - y_test, c="lightgreen", marker="s", label="Validation data") plt.title("Linear regression with Ridge regularization") plt.xlabel("Predicted values") plt.ylabel("Residuals") plt.legend(loc="upper left") plt.hlines(y=0, xmin=10.5, xmax=13.5, color="red") plt.show() # Plot predictions plt.scatter(y_train_rdg, y_train, c="blue", marker="s", label="Training data") plt.scatter(y_test_rdg, y_test, c="lightgreen", marker="s", label="Validation data") plt.title("Linear regression with Ridge regularization") plt.xlabel("Predicted values") plt.ylabel("Real values") plt.legend(loc="upper left") plt.plot([10.5, 13.5], [10.5, 13.5], c="red") plt.show() # Plot important coefficients coefs = pd.Series(ridge.coef_, index=X_train.columns) print("Ridge picked " + str(sum(coefs != 0)) + " features and eliminated the other " + \ str(sum(coefs == 0)) + " features") imp_coefs = pd.concat([coefs.sort_values().head(10), coefs.sort_values().tail(10)]) imp_coefs.plot(kind="barh") plt.title("Coefficients in the Ridge Model") plt.show() return ridge
def Lasso_regression(): lasso = LassoCV(alphas=[0.0001, 0.0003, 0.0006, 0.001, 0.003, 0.006, 0.01, 0.03, 0.06, 0.1, 0.3, 0.6, 1], max_iter=50000, cv=10) lasso.fit(train_split, y) alpha = lasso.alpha_ print("Best alpha :", alpha) print("Try again for more precision with alphas centered around " + str(alpha)) lasso = LassoCV(alphas=[alpha * .6, alpha * .65, alpha * .7, alpha * .75, alpha * .8, alpha * .85, alpha * .9, alpha * .95, alpha, alpha * 1.05, alpha * 1.1, alpha * 1.15, alpha * 1.25, alpha * 1.3, alpha * 1.35, alpha * 1.4], max_iter=50000, cv=10) lasso.fit(train_split, y) alpha = lasso.alpha_ print("Best alpha :", alpha) print("Lasso RMSE on Training set :", rmse_cv(lasso, train_split, y).mean()) y_train_las = lasso.predict(train_split) # Plot residuals plt.scatter(y_train_las, y_train_las - y, c="blue", marker="s", label="Training data") plt.title("Linear regression with Lasso regularization") plt.xlabel("Predicted values") plt.ylabel("Residuals") plt.legend(loc="upper left") plt.hlines(y=0, xmin=10.5, xmax=13.5, color="red") plt.show() # Plot predictions plt.scatter(y_train_las, y, c="blue", marker="s", label="Training data") plt.title("Linear regression with Lasso regularization") plt.xlabel("Predicted values") plt.ylabel("Real values") plt.legend(loc="upper left") plt.plot([10.5, 13.5], [10.5, 13.5], c="red") plt.show() # # Plot important coefficients coefs = pd.DataFrame(lasso.coef_, index=X_train.columns,columns=['value']) # print("Lasso picked " + str(sum(coefs != 0)) + " features and eliminated the other " + \ # str(sum(coefs == 0)) + " features") # imp_coefs = pd.concat([coefs.sort_values().head(10), # coefs.sort_values().tail(10)]) # imp_coefs.plot(kind="barh") # plt.title("Coefficients in the Lasso Model") # plt.show() return coefs,lasso
def plot_bstr(bands_node, kpoints_node=None, title=None, efermi=None, use_parent_calc=False, **kwargs): ''' py:function:: plot_bstr(bands_node[, kpoints_node=None]) Use matplotlib to plot the bands stored in a BandsData node. A KpointsData node can optionally be given as a fallback for kpoint labels. The caller is responsible for giving a node with matching labels (as in they are in/out nodes of the same calculation). :param BandsData bands_node: The BandsData node will be searched labels first :param KpointsData kpoints_node: The optional KpointsData node will be searched only if no labels are present on the BandsData node. No consistency checks are performed. :return: the matplotlib figure containing the plot ''' fig = plt.figure() title = title or 'Band Structure (pk=%s)' % bands_node.pk bands = bands_node.get_bands() _, nkp, _ = get_bs_dims(bands) plot_bands(bands_node, **kwargs) parent_calc = None if use_parent_calc: inputs = bands_node.get_inputs() parent_calc = inputs[0] if inputs else None efermi = get_efermi(parent_calc) kpoints_node = get_kp_node(parent_calc) if efermi: plt.hlines(efermi, plt.xlim()[0], nkp - 1, linestyles='dashed') plt.yticks( list(plt.yticks()[0]) + [efermi], [str(l) for l in plt.yticks()[0]] + [r'$E_{fermi}$']) try: kpx, kpl = get_kp_labels(bands_node, kpoints_node) plt.xticks(kpx, kpl) plt.vlines(kpx, plt.ylim()[0], plt.ylim()[1]) except Exception: # pylint: disable=broad-except pass plt.ylabel('Dispersion') plt.suptitle(title) return fig
def evaluate_random_forest_regression(X_train, X_test, y_train, y_test): forest = RandomForestRegressor( n_estimators=1000, criterion='mse', random_state=1, ) forest.fit(X_train, y_train) y_train_pred = forest.predict(X_train) y_test_pred = forest.predict(X_test) print("MSE train: %.3f, test: %.3f" % ( mean_squared_error(y_train, y_train_pred), mean_squared_error(y_test, y_test_pred), )) print("R^2 train: %.3f, test: %.3f" % ( r2_score(y_train, y_train_pred), r2_score(y_test, y_test_pred), )) plt.scatter( y_train_pred, y_train_pred - y_train, c='black', marker='o', s=35, alpha=0.5, label='Training data', ) plt.scatter( y_test_pred, y_test_pred - y_test, c='lightgreen', marker='s', s=35, alpha=0.7, label='Test data', ) plt.xlabel('Predicted values') plt.ylabel('Residuals') plt.legend(loc='upper left') plt.hlines(y=0, xmin=-10, xmax=50, lw=2, color='red') plt.xlim([-10, 50]) plt.show()
def ganttPlotter(self): """Plots gantt plot using dictionary of stations and associated start and end dates; uses output from markGaps function""" labs, tickloc, col = [], [], [] dateranges = self.dateranges stations = self.stations labels = self.labels # create color iterator for multi-color lines in gantt chart color = iter(plt.cm.Dark2(np.linspace(0, 1, len(stations)))) plt.figure(figsize=[8, 10]) fig, ax = plt.subplots() for i in range(len(stations)): c = next(color) for j in range(len(dateranges[stations[i]]) - 1): if (j + 1) % 2 != 0: if len(labels) == 0 or len(labels) != len(stations): plt.hlines(i + 1, dateranges[stations[i]][j], dateranges[stations[i]][j + 1], label=stations[i], color=c, linewidth=3) else: plt.hlines(i + 1, dateranges[stations[i]][j], dateranges[stations[i]][j + 1], label=labels[i], color=c, linewidth=3) labs.append(stations[i]) tickloc.append(i + 1) col.append(c) plt.ylim(0, len(stations) + 1) if len(labels) == 0 or len(labels) != len(stations): labels = stations plt.yticks(tickloc, labs) else: plt.yticks(tickloc, labels) plt.xlabel('Date') plt.ylabel('Station Name') plt.grid(linewidth=0.2) gytl = plt.gca().get_yticklabels() for i in range(len(gytl)): gytl[i].set_color(col[i]) plt.tight_layout() return fig