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

这篇具有很好参考价值的文章主要介绍了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. 画图

# ---- 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进阶教程:如何逐步美化一个折线图

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

4.1 优化:添加点

这里为每一个数据添加点

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

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

4.2 优化:设置刻度

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

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

4.3 优化:设置填充

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

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

4.4 优化:设置填充颜色

  1. 当橙色线更高时,希望填充为橙色。但是上面的还无法满足,这里再优化一下.
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")

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

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

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

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.
  '''

  # -- 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.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部分。

推荐文章

  • 李宏毅《机器学习》国语课程(2022)来了

  • 有人把吴恩达老师的机器学习和深度学习做成了中文版

  • 上瘾了,最近又给公司撸了一个可视化大屏(附源码)

  • 如此优雅,4款 Python 自动数据分析神器真香啊

  • 梳理半月有余,精心准备了17张知识思维导图,这次要讲清统计学

  • 年终汇总:20份可视化大屏模板,直接套用真香(文末附源码)

技术交流

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

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

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

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

matplotlib进阶教程:如何逐步美化一个折线图文章来源地址https://www.toymoban.com/news/detail-438179.html

到了这里,关于matplotlib进阶教程:如何逐步美化一个折线图的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 还不懂如何与chatGPT高效交流?保姆级且全面的chatGPT提示词工程教程来啦!(二)进阶篇

    基础篇中,教了如何通过遵循四个基本要义向chatGPT清楚的表达出你的需求。下面是四要义的提纲,忘记了可以复习一下还不懂如何与AI高效交流?保姆级且全面的chatGPT提示词工程教程来啦!(一)基础篇。 进阶技巧是,在基础之上,添加一些咒语。或者结合基础,通过拆分

    2024年02月07日
    浏览(52)
  • Lua 进阶 · 教程笔记

    笔记的内容出自 Bilibili 上的视频:Lua教程-进阶部分 - 4K超清【不定期更新】 笔记主要用于供笔者个人或读者回顾知识点,如有纰漏,烦请指出 : ) 国内的大佬 云风 翻译了 Lua 的 Api 参考手册:传送门【】 以后读者在练习或者开发途中可以在参考手册里查看 Lua 提供的 Api。

    2024年01月24日
    浏览(51)
  • kotlin教程4:函数进阶

    kotlin教程:编程基础💎数据结构💎面向对象 kotlin 的函数定义非常灵活,既可以按照顺序传参,也可以通过参数名传参,而且可以设置参数默认值,这些在基础教程中已经讲过了。 此外, kotlin 中用 vararg 修饰的参数,为长度可变的参数列表 递归是一种常用的编程技巧,就像

    2024年02月05日
    浏览(45)
  • aardio教程二) 进阶语法

    aardio中除了基础数据类型外,其他的复合对象都是table(例如类和名字空间等)。table可以用来存放aardio的任何对象,包括另一个table。 在其他语言中的字典、列表、数组、集合映射等,在aardio中都使用table来实现。 创建字典 创建数组 数组和字典可以混用 使用class定义类

    2024年03月18日
    浏览(54)
  • SQL Server进阶教程读书笔记

    最近把SQL Server进阶教程重新读了一遍,顺便整理了一下书本中的知识点 CASE WHEN         ❑ 高手使用select做分支,新手用where和having做分支         ❑ 要写ELSE,要写END,避免未匹配上得到NULL         ❑ check到底怎么用?                  在SQL Server中,s

    2024年02月09日
    浏览(49)
  • SpringBoot进阶教程(七十七)WebSocket

    WebSocket是一种在单个TCP连接上进行全双工通信的协议。WebSocket使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据。在WebSocket API中,浏览器和服务器只需要完成一次握手,两者之间就直接可以创建持久性的连接,并进行双向数据传输。 很多

    2024年02月08日
    浏览(74)
  • git 进阶系列教程--push

    功能介绍 将本地仓库中的内容同步到远程仓库 指令解析 这个命令就上将本地仓库中的文件同步到远程。是平时用到push最多的指令。它其实是一种简写方式。省略了远程仓库的地址,本地分支。 这个命令是比较全的一条指令。意思是推送本地master分支到远程(origin)的master分支

    2024年02月14日
    浏览(51)
  • git 进阶系列教程--add

    功能介绍 将工作区(working directory)中的内容放入暂存区(staging area) 追踪文件(解释:刚被添加到工作区的文件处于未跟踪状态(Untracked files),该命令会将新添加的文件放入暂存区,并且文件将处于已跟踪状态) 命令解析 添加工作区所有文件进入暂存区 最后的点可以理解为正则里的

    2024年02月06日
    浏览(50)
  • git 进阶系列教程--pull

    功能 从远程仓库拉取最新代码到本地 代码解析 将远程仓库代码拉取到本地。当然这个命令的前提是你本地代码与远程代码有链接,无论是你upstream也好git clone也罢。而且是默认拉取远程代码中与该分支有关联的分支与本地该分支合并。同时,git pull不止是拉取代码,同时会将

    2024年02月11日
    浏览(75)
  • SpringBoot进阶教程(七十八)邮件服务

    Sun公司提供了JavaMail用来实现邮件发送,但是配置烦琐,Spring中提供了JavaMailSender用来简化邮件配置,Spring Boot则提供了MailSenderAutoConfiguration对邮件的发送做了进一步简化。 开通POP3/SMTP服务或者IMAP/SMTP服务 使用邮箱发送邮件,首先要申请开通POP3/SMTP服务或者IMAP/SMTP服务。SMTP全

    2024年02月05日
    浏览(50)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包