欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python+matplotlib實(shí)現(xiàn)折線圖的美化

 更新時(shí)間:2022年05月12日 11:11:49   作者:Python學(xué)習(xí)與數(shù)據(jù)挖掘  
這篇文章主要和大家分享一個(gè)非常有趣的Python教程—如何美化一個(gè)?matplotlib折線圖。文中的示例代碼講解詳細(xì),感興趣的可以了解一下

大家好,今天分享一個(gè)非常有趣的 Python 教程,如何美化一個(gè) matplotlib 折線圖,喜歡記得收藏、關(guān)注、點(diǎn)贊。

1. 導(dǎo)入包

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib.gridspec as gridspec

2. 獲得數(shù)據(jù)

file_id = '1yM_F93NY4QkxjlKL3GzdcCQEnBiA2ltB'
url = f'https://drive.google.com/uc?id={file_id}'
df = pd.read_csv(url, index_col=0)
df

數(shù)據(jù)長(zhǎng)得是這樣的:

3. 對(duì)數(shù)據(jù)做一些預(yù)處理

按照需要,對(duì)數(shù)據(jù)再做一些預(yù)處理,代碼及效果如下:

home_df = df.copy()
home_df = home_df.melt(id_vars = ["date", "home_team_name", "away_team_name"])
home_df["venue"] = "H"
home_df.rename(columns = {"home_team_name":"team", "away_team_name":"opponent"}, inplace = True)
home_df.replace({"variable":{"home_team_xG":"xG_for", "away_team_xG":"xG_ag"}}, inplace = True)
away_df = df.copy()
away_df = away_df.melt(id_vars = ["date", "away_team_name", "home_team_name"])
away_df["venue"] = "A"
away_df.rename(columns = {"away_team_name":"team", "home_team_name":"opponent"}, inplace = True)
away_df.replace({"variable":{"away_team_xG":"xG_for", "home_team_xG":"xG_ag"}}, inplace = True)
df = pd.concat([home_df, away_df]).reset_index(drop = True)
df

4. 畫圖

# ---- Filter the data

Y_for = df[(df["team"] == "Lazio") & (df["variable"] == "xG_for")]["value"].reset_index(drop = True)
Y_ag = df[(df["team"] == "Lazio") & (df["variable"] == "xG_ag")]["value"].reset_index(drop = True)
X_ = pd.Series(range(len(Y_for)))

# ---- 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()
fig, ax = plt.subplots(figsize = (7,3), dpi = 200)

ax.plot(X_, Y_for)
ax.plot(X_, Y_ag)

使用matplotlib倒是可以快速把圖畫好了,但是太丑了。接下來(lái)進(jìn)行優(yōu)化。

4.1 優(yōu)化:添加點(diǎn)

這里為每一個(gè)數(shù)據(jù)添加點(diǎn)

fig, ax = plt.subplots(figsize = (7,3), dpi = 200)

# --- 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.5, color = "#4E616C")

# --- The data

ax.plot(X_, Y_for, marker = "o")
ax.plot(X_, Y_ag, marker = "o")

4.2 優(yōu)化:設(shè)置刻度

fig, ax = plt.subplots(figsize = (7,3), dpi = 200)

# --- 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

ax.plot(X_, Y_for, marker = "o", mfc = "white", ms = 5)
ax.plot(X_, Y_ag, marker = "o", mfc = "white", ms = 5)

# --- 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)])
# This last line outputs
# [-1, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35]
# and we mark the tickers every two positions.

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")

4.3 優(yōu)化:設(shè)置填充

fig, ax = plt.subplots(figsize = (7,3), dpi = 200)

# --- 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

ax.plot(X_, Y_for, marker = "o", mfc = "white", ms = 5)
ax.plot(X_, Y_ag, marker = "o", mfc = "white", ms = 5)

# --- Fill between

ax.fill_between(x = X_, y1 = Y_for, y2 = Y_ag, alpha = 0.5)

# --- 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")

4.4 優(yōu)化:設(shè)置填充顏色

1.當(dāng)橙色線更高時(shí),希望填充為橙色。但是上面的還無(wú)法滿足,這里再優(yōu)化一下.

fig, ax = plt.subplots(figsize = (7,3), dpi = 200)

# --- 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

ax.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_ag

pos_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 <= Y_ag)
ax.fill_between(x = X_[pos_ag], y1 = Y_for[pos_ag], y2 = Y_ag[pos_ag], alpha = 0.5)

# --- 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")

上面的圖出現(xiàn)異常,再修改一下:

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()



fig, ax = plt.subplots(figsize = (7,3), dpi = 200)

# --- 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 = 5)
ag_ = ax.plot(X_, Y_ag, marker = "o", mfc = "white", ms = 5)

# --- 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)

# --- 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")

5. 把功能打包成函數(shù)

上面的樣子都還不錯(cuò)啦,接下來(lái)把這些東西都打包成一個(gè)函數(shù)。方便后面直接出圖。

def plot_xG_rolling(team, ax, window = 5, color_for = "blue", color_ag = "orange", data = df):
  '''
  This function creates a rolling average xG plot for a given team and rolling
  window.

  team (str): The team's name
  ax (obj): a Matplotlib axes.
  window (int): The number of periods for our rolling average.
  color_for (str): A hex color code for xG created.
  color_af (str): A hex color code for xG conceded.
  data (DataFrame): our df with the xG data.
  '''

  # -- Prepping the data
  home_df = data.copy()
  home_df = home_df.melt(id_vars = ["date", "home_team_name", "away_team_name"])
  home_df["venue"] = "H"
  home_df.rename(columns = {"home_team_name":"team", "away_team_name":"opponent"}, inplace = True)
  home_df.replace({"variable":{"home_team_xG":"xG_for", "away_team_xG":"xG_ag"}}, inplace = True)

  away_df = data.copy()
  away_df = away_df.melt(id_vars = ["date", "away_team_name", "home_team_name"])
  away_df["venue"] = "A"
  away_df.rename(columns = {"away_team_name":"team", "home_team_name":"opponent"}, inplace = True)
  away_df.replace({"variable":{"away_team_xG":"xG_for", "home_team_xG":"xG_ag"}}, 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_for,
            va = 'center',
            ha = 'left',
            size = 6.5
          )

  # -- Add the xG conceded label
  ag_label_ = ax.text(
            x = X_.iloc[-1] + 0.75, y = Y_ag_last,
            s = f'{Y_ag_last:,.1f} xGA',
            color = color_ag,
            va = 'center',
            ha = 'left',
            size = 6.5
          )

6.測(cè)試函數(shù)

file_id = '1yM_F93NY4QkxjlKL3GzdcCQEnBiA2ltB'
url = f'https://drive.google.com/uc?id={file_id}'
df = pd.read_csv(url, index_col=0)
fig = plt.figure(figsize=(5, 2), dpi = 200)
ax = plt.subplot(111)

plot_xG_rolling("Sassuolo", ax, color_for = "#00A752", color_ag = "black", data = df)

plt.tight_layout()

再設(shè)置更加豐富的顏色:

fig = plt.figure(figsize=(5, 8), dpi = 200, facecolor = "#EFE9E6")

ax1 = plt.subplot(411, facecolor = "#EFE9E6")
ax2 = plt.subplot(412, facecolor = "#EFE9E6")
ax3 = plt.subplot(413, facecolor = "#EFE9E6")
ax4 = plt.subplot(414, facecolor = "#EFE9E6")

plot_xG_rolling("Sassuolo", ax1, color_for = "#00A752", color_ag = "black", data = df)
plot_xG_rolling("Lazio", ax2, color_for = "#87D8F7", color_ag = "#15366F", data = df)
plot_xG_rolling("Hellas Verona", ax3, color_for = "#153aab", color_ag = "#fdcf41", data = df)
plot_xG_rolling("Empoli", ax4, color_for = "#00579C", color_ag = "black", data = df)

plt.tight_layout()

最后

其實(shí)本文主要是對(duì)兩個(gè)折線圖做了一系列的優(yōu)化和改進(jìn)而已,主要是強(qiáng)調(diào)細(xì)節(jié)部分。

涉及到的matplotlib的知識(shí),也主要是在ticks、背景顏色、fill_between部分。

以上就是Python+matplotlib實(shí)現(xiàn)折線圖的美化的詳細(xì)內(nèi)容,更多關(guān)于Python matplotlib折線圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python輕松破解加密壓縮包教程詳解

    Python輕松破解加密壓縮包教程詳解

    相信大家都遇到過(guò)這種情況,下載文件的時(shí)候遇到壓縮包又沒(méi)有密碼,或者說(shuō)自己設(shè)置的加密密碼,但是忘記了,就很難受。下面就將為大家介紹如何解決這一問(wèn)題
    2021-12-12
  • python中字符串String及其常見(jiàn)操作指南(方法、函數(shù))

    python中字符串String及其常見(jiàn)操作指南(方法、函數(shù))

    String方法是用來(lái)處理代碼中的字符串的,它幾乎能搞定你所遇到的所有字符串格式,下面這篇文章主要給大家介紹了關(guān)于python中字符串String及其常見(jiàn)操作(方法、函數(shù))的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • python中斷time.sleep一種更優(yōu)雅的方式:event.wait

    python中斷time.sleep一種更優(yōu)雅的方式:event.wait

    這篇文章主要介紹了python中斷time.sleep一種更優(yōu)雅的方式:event.wait,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • PyQT實(shí)現(xiàn)多窗口切換

    PyQT實(shí)現(xiàn)多窗口切換

    這篇文章主要為大家詳細(xì)介紹了PyQT實(shí)現(xiàn)多窗口切換的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Python捕捉和模擬鼠標(biāo)事件的方法

    Python捕捉和模擬鼠標(biāo)事件的方法

    這篇文章主要介紹了Python捕捉和模擬鼠標(biāo)事件的方法,涉及PyHook和PyWin32模塊的使用技巧,需要的朋友可以參考下
    2015-06-06
  • 關(guān)于Python中幾個(gè)有趣的函數(shù)和推導(dǎo)式解析

    關(guān)于Python中幾個(gè)有趣的函數(shù)和推導(dǎo)式解析

    這篇文章主要介紹了關(guān)于Python中幾個(gè)有趣的函數(shù)和推導(dǎo)式解析,推導(dǎo)式comprehensions,又稱解析式,是Python的一種獨(dú)有特性,推導(dǎo)式是可以從一個(gè)數(shù)據(jù)序列構(gòu)建另一個(gè)新的數(shù)據(jù)序列的結(jié)構(gòu)體,需要的朋友可以參考下
    2023-08-08
  • Python數(shù)據(jù)類型相互轉(zhuǎn)換

    Python數(shù)據(jù)類型相互轉(zhuǎn)換

    當(dāng)涉及數(shù)據(jù)類型轉(zhuǎn)換時(shí),Python提供了多種內(nèi)置函數(shù)來(lái)執(zhí)行不同類型之間的轉(zhuǎn)換,本文主要介紹了Python數(shù)據(jù)類型相互轉(zhuǎn)換,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-09-09
  • Pandas中DataFrame對(duì)象轉(zhuǎn)置(交換行列)

    Pandas中DataFrame對(duì)象轉(zhuǎn)置(交換行列)

    本文主要介紹了Pandas中DataFrame對(duì)象轉(zhuǎn)置(交換行列),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • Python列表倒序輸出及其效率詳解

    Python列表倒序輸出及其效率詳解

    在本篇文章里小編給大家整理的是關(guān)于Python列表倒序輸出及其效率詳解內(nèi)容,需要的朋友們學(xué)習(xí)下。
    2020-03-03
  • 用Python手把手教你實(shí)現(xiàn)2048小游戲

    用Python手把手教你實(shí)現(xiàn)2048小游戲

    感覺(jué)好久沒(méi)和大家一起寫小游戲玩了,今天恰巧有空.這次我們來(lái)用Python做個(gè)2048小游戲吧.廢話不多說(shuō),文中有非常詳細(xì)的代碼示例,需要的朋友可以參考下
    2021-06-06

最新評(píng)論