matplotlib进阶教程:如何逐步美化一个折线图

大家好,今天分享一个非常有趣的 Python 教程,如何美化一个 matplotlib 折线图, 喜欢记得收藏、关注、点赞。

注:数据、完整代码、技术交流文末获取

1. 导入包

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

2. 获得数据

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

数据长得是这样的:

matplotlib进阶教程:如何逐步美化一个折线图

3. 对数据做一些预处理

按照需要,对数据再做一些预处理,代码及效果如下:

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

matplotlib进阶教程:如何逐步美化一个折线图

4. 画图


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

Y_for = Y_for.rolling(window = 5, min_periods = 0).mean()
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进阶教程:如何逐步美化一个折线图

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

4.1 优化:添加点

这里为每一个数据添加点

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

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

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

matplotlib进阶教程:如何逐步美化一个折线图

4.2 优化:设置刻度

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

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

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

ax.xaxis.set_major_locator(ticker.MultipleLocator(2))
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")

matplotlib进阶教程:如何逐步美化一个折线图

4.3 优化:设置填充

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

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

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

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

ax.xaxis.set_major_locator(ticker.MultipleLocator(2))
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")

matplotlib进阶教程:如何逐步美化一个折线图

4.4 优化:设置填充颜色

  1. 当橙色线更高时,希望填充为橙色。但是上面的还无法满足,这里再优化一下.
fig, ax = plt.subplots(figsize = (7,3), dpi = 200)

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

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

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)

ax.xaxis.set_major_locator(ticker.MultipleLocator(2))
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")

matplotlib进阶教程:如何逐步美化一个折线图

上面的图出现异常,再修改一下:

X_aux = X_.copy()
X_aux.index = X_aux.index * 10
last_idx = X_aux.index[-1] + 1
X_aux = X_aux.reindex(range(last_idx))
X_aux = X_aux.interpolate()

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

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)

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

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

for index in range(len(X_aux) - 1):

    if Y_for_aux.iloc[index + 1] > Y_ag_aux.iloc[index + 1]:
        color = for_[0].get_color()
    else:
        color = ag_[0].get_color()

    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)

ax.xaxis.set_major_locator(ticker.MultipleLocator(2))
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")

matplotlib进阶教程:如何逐步美化一个折线图

5. 把功能打包成函数

  1. 上面的样子都还不错啦,接下来把这些东西都打包成一个函数。方便后面直接出图。
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.

  '''

  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)

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

  Y_for = Y_for.rolling(window = 5, min_periods = 0).mean()
  Y_ag = Y_ag.rolling(window = 5, min_periods = 0).mean()

  X_aux = X_.copy()
  X_aux.index = X_aux.index * 10
  last_idx = X_aux.index[-1] + 1
  X_aux = X_aux.reindex(range(last_idx))
  X_aux = X_aux.interpolate()

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

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

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

  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)

  for index in range(len(X_aux) - 1):

      if Y_for_aux.iloc[index + 1] > Y_ag_aux.iloc[index + 1]:
          color = for_[0].get_color()
      else:
          color = ag_[0].get_color()

      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)

  ax.set_ylim(0)

  ax.xaxis.set_major_locator(ticker.MultipleLocator(2))
  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")

  Y_for_last = Y_for.iloc[-1]
  Y_ag_last = Y_ag.iloc[-1]

  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
          )

  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
          )

  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.1 测试函数

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

matplotlib进阶教程:如何逐步美化一个折线图

再设置更加丰富的颜色:

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

matplotlib进阶教程:如何逐步美化一个折线图

最后

其实本文主要是对两个折线图做了一系列的优化和改进而已,主要是强调细节部分。

涉及到的matplotlib的知识,也主要是在ticks、背景颜色、fill_between部分。

推荐文章

技术交流

欢迎转载、收藏、有所收获点赞支持一下! 数据、代码可以找我获取

matplotlib进阶教程:如何逐步美化一个折线图

目前开通了技术交流群,群友已超过 2000人,添加时最好的备注方式为:来源+兴趣方向,方便找到志同道合的朋友

  • 方式①、发送如下图片至微信,长按识别,后台回复:加群;
  • 方式②、添加微信号: dkl88191,备注:来自CSDN
  • 方式③、微信搜索公众号: Python学习与数据挖掘,后台回复:加群

matplotlib进阶教程:如何逐步美化一个折线图

Original: https://blog.csdn.net/weixin_38037405/article/details/124719333
Author: Love Python数据挖掘
Title: matplotlib进阶教程:如何逐步美化一个折线图

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/693211/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球