设为首页 加入收藏

TOP

Python+matplotlib实现折线图的美化(四)
2023-07-25 21:28:15 】 浏览:86
Tags:Python matplotlib
}}, inplace = True) df = pd.concat([home_df, away_df]).reset_index(drop = True) # ---- Filter the data Y_for = df[(df["team"] == team) & (df["variable"] == "xG_for")]["value"].reset_index(drop = True) Y_ag = df[(df["team"] == team) & (df["variable"] == "xG_ag")]["value"].reset_index(drop = True) X_ = pd.Series(range(len(Y_for))) if Y_for.shape[0] == 0: raise ValueError(f"Team {team} is not present in the DataFrame") # ---- Compute rolling average Y_for = Y_for.rolling(window = 5, min_periods = 0).mean() # min_periods is for partial avg. Y_ag = Y_ag.rolling(window = 5, min_periods = 0).mean() # ---- Create auxiliary series for filling between curves X_aux = X_.copy() X_aux.index = X_aux.index * 10 # 9 aux points in between each match last_idx = X_aux.index[-1] + 1 X_aux = X_aux.reindex(range(last_idx)) X_aux = X_aux.interpolate() # --- Aux series for the xG created (Y_for) Y_for_aux = Y_for.copy() Y_for_aux.index = Y_for_aux.index * 10 last_idx = Y_for_aux.index[-1] + 1 Y_for_aux = Y_for_aux.reindex(range(last_idx)) Y_for_aux = Y_for_aux.interpolate() # --- Aux series for the xG conceded (Y_ag) Y_ag_aux = Y_ag.copy() Y_ag_aux.index = Y_ag_aux.index * 10 last_idx = Y_ag_aux.index[-1] + 1 Y_ag_aux = Y_ag_aux.reindex(range(last_idx)) Y_ag_aux = Y_ag_aux.interpolate() # --- Plotting our data # --- Remove spines and add gridlines ax.spines["left"].set_visible(False) ax.spines["top"].set_visible(False) ax.spines["right"].set_visible(False) ax.grid(ls = "--", lw = 0.25, color = "#4E616C") # --- The data for_ = ax.plot(X_, Y_for, marker = "o", mfc = "white", ms = 4, color = color_for) ag_ = ax.plot(X_, Y_ag, marker = "o", mfc = "white", ms = 4, color = color_ag) # --- Fill between for index in range(len(X_aux) - 1): # Choose color based on which line's on top if Y_for_aux.iloc[index + 1] > Y_ag_aux.iloc[index + 1]: color = for_[0].get_color() else: color = ag_[0].get_color() # Fill between the current point and the next point in pur extended series. ax.fill_between([X_aux[index], X_aux[index+1]], [Y_for_aux.iloc[index], Y_for_aux.iloc[index+1]], [Y_ag_aux.iloc[index], Y_ag_aux.iloc[index+1]], color=color, zorder = 2, alpha = 0.2, ec = None) # --- Ensure minimum value of Y-axis is zero ax.set_ylim(0) # --- Adjust tickers and spine to match the style of our grid ax.xaxis.set_major_locator(ticker.MultipleLocator(2)) # ticker every 2 matchdays xticks_ = ax.xaxis.set_ticklabels([x - 1 for x in range(0, len(X_) + 3, 2)]) ax.xaxis.set_tick_params(length = 2, color = "#4E616C", labelcolor = "#4E616C", labelsize = 6) ax.yaxis.set_tick_params(length = 2, color = "#4E616C", labelcolor = "#4E616C", labelsize = 6) ax.spines["bottom"].set_edgecolor("#4E616C") # --- Legend and team name Y_for_last = Y_for.iloc[-1] Y_ag_last = Y_ag.iloc[-1] # -- Add the team's name team_ = ax.text( x = 0, y = ax.get_ylim()[1] + ax.get_ylim()[1]/20, s = f'{team}', color = "#4E616C", va = 'center', ha = 'left', size = 7 ) # -- Add the xG created label for_label_ = ax.text( x = X_.iloc[-1] + 0.75, y = Y_for_last, s = f'{Y_for_last:,.1f} xGF', color = color_
首页 上一页 1 2 3 4 5 下一页 尾页 4/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇在不同电脑间同步pycharm的配置 下一篇Python中 re.compile 函数的使用

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目