翼度科技»论坛 编程开发 python 查看内容

Python+matplotlib实现折线图的美化

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
1. 导入包
  1. <br>import pandas as pd
  2. import matplotlib.pyplot as plt
  3. import matplotlib.ticker as ticker
  4. import matplotlib.gridspec as gridspec
复制代码
  1. [/code][size=5]2. 获得数据[/size]
  2. [code]file_id = '1yM_F93NY4QkxjlKL3GzdcCQEnBiA2ltB'‘Python学习交流群:748989764 ’
  3. url = f'https://drive.google.com/uc?id={file_id}'
  4. df = pd.read_csv(url, index_col=0)
  5. df
复制代码
数据长得是这样的:
 
 
3. 对数据做一些预处理

 按照需要,对数据再做一些预处理,代码及效果如下:
  1. home_df = df.copy()
  2. home_df = home_df.melt(id_vars = ["date", "home_team_name", "away_team_name"])
  3. home_df["venue"] = "H"
  4. home_df.rename(columns = {"home_team_name":"team", "away_team_name":"opponent"}, inplace = True)
  5. home_df.replace({"variable":{"home_team_xG":"xG_for", "away_team_xG":"xG_ag"}}, inplace = True)
复制代码
  1. away_df = df.copy()
  2. away_df = away_df.melt(id_vars = ["date", "away_team_name", "home_team_name"])
  3. away_df["venue"] = "A"
  4. away_df.rename(columns = {"away_team_name":"team", "home_team_name":"opponent"}, inplace = True)
  5. away_df.replace({"variable":{"away_team_xG":"xG_for", "home_team_xG":"xG_ag"}}, inplace = True)
复制代码
  1. df = pd.concat([home_df, away_df]).reset_index(drop = True)
  2. df
复制代码

 
 
 
4. 画图
  1. # ---- Filter the data
  2. Y_for = df[(df["team"] == "Lazio") & (df["variable"] == "xG_for")]["value"].reset_index(drop = True)
  3. Y_ag = df[(df["team"] == "Lazio") & (df["variable"] == "xG_ag")]["value"].reset_index(drop = True)
  4. X_ = pd.Series(range(len(Y_for)))
  5. # ---- Compute rolling average
  6. Y_for = Y_for.rolling(window = 5, min_periods = 0).mean() # min_periods is for partial avg.
  7. Y_ag = Y_ag.rolling(window = 5, min_periods = 0).mean()
复制代码
  1. fig, ax = plt.subplots(figsize = (7,3), dpi = 200)
  2. ax.plot(X_, Y_for)
  3. ax.plot(X_, Y_ag)
复制代码

 
 
 
使用matplotlib倒是可以快速把图画好了,但是太丑了。接下来进行优化。
 4.1 优化:添加点

 这里为每一个数据添加点
  
  1. fig, ax = plt.subplots(figsize = (7,3), dpi = 200)
  2. # --- Remove spines and add gridlines
  3. ax.spines["left"].set_visible(False)
  4. ax.spines["top"].set_visible(False)
  5. ax.spines["right"].set_visible(False)
  6. ax.grid(ls = "--", lw = 0.5, color = "#4E616C")
  7. # --- The data
  8. ax.plot(X_, Y_for, marker = "o")
  9. ax.plot(X_, Y_ag, marker = "o")
复制代码
 

 
 
4.2 优化:设置刻度

 
  1. fig, ax = plt.subplots(figsize = (7,3), dpi = 200)
  2. # --- Remove spines and add gridlines
  3. ax.spines["left"].set_visible(False)
  4. ax.spines["top"].set_visible(False)
  5. ax.spines["right"].set_visible(False)
  6. ax.grid(ls = "--", lw = 0.25, color = "#4E616C")
  7. # --- The data
  8. ax.plot(X_, Y_for, marker = "o", mfc = "white", ms = 5)
  9. ax.plot(X_, Y_ag, marker = "o", mfc = "white", ms = 5)
  10. # --- Adjust tickers and spine to match the style of our grid
  11. ax.xaxis.set_major_locator(ticker.MultipleLocator(2)) # ticker every 2 matchdays
  12. xticks_ = ax.xaxis.set_ticklabels([x - 1 for x in range(0, len(X_) + 3, 2)])
  13. # This last line outputs
  14. # [-1, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35]
  15. # and we mark the tickers every two positions.
  16. ax.xaxis.set_tick_params(length = 2, color = "#4E616C", labelcolor = "#4E616C", labelsize = 6)
  17. ax.yaxis.set_tick_params(length = 2, color = "#4E616C", labelcolor = "#4E616C", labelsize = 6)
  18. ax.spines["bottom"].set_edgecolor("#4E616C")
复制代码

 
 
4.3 优化:设置填充

 
  1. fig, ax = plt.subplots(figsize = (7,3), dpi = 200)
复制代码
  1. Python学习交流群:748989764
复制代码
  1. <br>
  2. # --- Remove spines and add gridlines
  3. ax.spines["left"].set_visible(False)
  4. ax.spines["top"].set_visible(False)
  5. ax.spines["right"].set_visible(False)
  6. ax.grid(ls = "--", lw = 0.25, color = "#4E616C")
  7. # --- The data
  8. ax.plot(X_, Y_for, marker = "o", mfc = "white", ms = 5)
  9. ax.plot(X_, Y_ag, marker = "o", mfc = "white", ms = 5)
  10. # --- Fill between
  11. ax.fill_between(x = X_, y1 = Y_for, y2 = Y_ag, alpha = 0.5)
  12. # --- Adjust tickers and spine to match the style of our grid
  13. ax.xaxis.set_major_locator(ticker.MultipleLocator(2)) # ticker every 2 matchdays
  14. xticks_ = ax.xaxis.set_ticklabels([x - 1 for x in range(0, len(X_) + 3, 2)])
  15. ax.xaxis.set_tick_params(length = 2, color = "#4E616C", labelcolor = "#4E616C", labelsize = 6)
  16. ax.yaxis.set_tick_params(length = 2, color = "#4E616C", labelcolor = "#4E616C", labelsize = 6)
  17. ax.spines["bottom"].set_edgecolor("#4E616C")
复制代码

 
 
4.4 优化:设置填充颜色

1.当橙色线更高时,希望填充为橙色。但是上面的还无法满足,这里再优化一下.
  [code]fig, ax = plt.subplots(figsize = (7,3), dpi = 200)# --- Remove spines and add gridlinesax.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 dataax.plot(X_, Y_for, marker = "o", mfc = "white", ms = 5)ax.plot(X_, Y_ag, marker = "o", mfc = "white", ms = 5)# --- Fill between# Identify points where Y_for > Y_agpos_for = (Y_for > Y_ag)ax.fill_between(x = X_[pos_for], y1 = Y_for[pos_for], y2 = Y_ag[pos_for], alpha = 0.5)pos_ag = (Y_for

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具