MPLSoccer/PyPizza长参数文本换行重叠问题技术求助
问题:PyPizza模块长参数字符串无法自动换行导致文本重叠
我是编程新手,参考模板修改后成功生成了可视化效果,但使用mplsoccer的PyPizza模块时,从数据列名提取的较长参数字符串无法自动换行,出现文本重叠问题,附上相关代码:
from urllib.request import urlopen import io import pandas as pd import numpy as np from scipy import stats import math from mplsoccer import PyPizza, add_image, FontManager import matplotlib.pyplot as plt from PIL import Image from textwrap import wrap URL = "https://i.ibb.co/S6WmBqL/logo.png" image_data = urlopen(URL).read() concept_scouting_logo = Image.open(io.BytesIO(image_data)) URL = "https://upload.wikimedia.org/wikipedia/en/thumb/f/fc/Wolverhampton_Wanderers.svg/1200px-Wolverhampton_Wanderers.svg.png" image_data = urlopen(URL).read() club_logo = Image.open(io.BytesIO(image_data)) df = pd.read_csv(r'C:\\Users\\joewo\\OneDrive\\Documents\\Concept Scouting App\\FBCSV.csv') df.head() columns_to_keep = ['Player', 'Team', 'Position', 'Assists', 'xA', 'Successful defensive actions per 90', 'Defensive duels per 90', 'Aerial duels per 90', 'Shots blocked per 90', 'Interceptions per 90', 'Successful attacking actions per 90', 'Crosses per 90', 'Dribbles per 90', 'Offensive duels per 90', 'Progressive runs per 90', 'Forward passes per 90'] new_df = original_df[columns_to_keep] columns_list = new_df.columns.tolist() print(columns_list) params = list(new_df.columns) print(params) params = params[3:] player = new_df.loc[new_df['Player']== 'Nélson Semedo'].reset_index() player = list(player.loc[0]) player = player[4:] print(len(params),len(player)) values = [] for x in range(len(params)): values.append(math.floor(stats.percentileofscore(df[params[x]],player[x]))) for n,i in enumerate(values): if i == 100: values[n] = 99 baker = PyPizza( params=params, # list of parameters background_color="#222222", # background color straight_line_color="#000000", # color for straight lines straight_line_lw=1, # linewidth for straight lines last_circle_color="#000000", # color for last line last_circle_lw=1, # linewidth of last circle other_circle_lw=0, # linewidth for other circles inner_circle_size=20 # size of inner circle ) fig, ax = baker.make_pizza( values, # list of values figsize=(8, 8), # adjust figsize according to your need color_blank_space="same", # use the same color to fill blank space param_location=110, # where the parameters will be added kwargs_slices=dict( facecolor="#d70232", edgecolor="#000000", zorder=2, linewidth=1 ), # values to be used when plotting slices kwargs_params=dict( color="#F2F2F2", fontsize=12, va="center" ), # values to be used when adding parameter kwargs_values=dict( color="#000000", fontsize=12, zorder=3, bbox=dict( edgecolor="#000000", facecolor="#6CABDD", boxstyle="round,pad=0.2", lw=1 ) ) # values to be used when adding parameter-values ) # add title fig.text( 0.515, 0.97, "Nélson Semedo - Wolves", size=18, ha="center", color="#F2F2F2" ) # add subtitle fig.text( 0.515, 0.942, "Percentile Rank vs CB's in Top5 Leagues | 2023/24", size=15, ha="center", color="#F2F2F2" ) fig.text( 0.01, 0.01, f"{notes}\n{CREDIT_1}\n{CREDIT_2}", size=9, color="#F2F2F2", ha="left", va="bottom" ) # add image ax_image = add_image( concept_scouting_logo, fig, left=0.4478, bottom=0.4315, width=0.13, height=0.127 ) # add image ax_image = add_image( club_logo, fig, left=0.02, bottom=0.85, width=0.13, height=0.127 )
解决方案
核心原因
PyPizza默认不会自动处理长文本换行,需要手动对参数名进行换行处理,或者配置文本渲染参数。
具体修改步骤
用textwrap处理长参数名
你已经导入了textwrap,可以在生成params列表时,对每个长字符串进行自动换行拆分:# 替换原有的params = params[3:]这部分 params = params[3:] # 处理每个参数名,自动拆分成多行(width控制每行最大字符数,按需调整) wrapped_params = [] for param in params: wrapped = '\n'.join(wrap(param, width=15)) wrapped_params.append(wrapped) # 将处理后的参数列表传给PyPizza baker = PyPizza( params=wrapped_params, # 使用换行后的参数 background_color="#222222", straight_line_color="#000000", straight_line_lw=1, last_circle_color="#000000", last_circle_lw=1, other_circle_lw=0, inner_circle_size=20 )调整参数文本渲染设置
在make_pizza的kwargs_params里缩小字体,并启用换行支持(部分版本PyPizza支持wrap=True):kwargs_params=dict( color="#F2F2F2", fontsize=10, # 缩小字体避免拥挤 va="center", wrap=True # 启用自动换行 ),可选:手动简化参数名
对特别长的参数,比如"Successful defensive actions per 90",可以手动拆分成两行,比如"Successful defensive\nactions per 90",这样显示更可控。
额外修正
原代码里有一处错误:new_df = original_df[columns_to_keep]应该改成new_df = df[columns_to_keep],因为你读取的DataFrame变量是df,不是original_df,不修改会导致报错。
内容的提问来源于stack exchange,提问作者Joe Parker
相关产品推荐
相关产品推荐

