如何基于非常规标签数据为各联赛制作上下半场进球占比饼图?
Got it, let's walk through how to make those pie charts for each league's goal split. Here's a step-by-step solution using pandas and matplotlib—standard tools for this kind of data visualization task:
Create Individual Pie Charts for League Goal Splits
First, let's get your data into a properly structured pandas DataFrame. It looks like your first column is the league name, followed by the first and second half goal percentages. Here's how to set that up:
import pandas as pd import matplotlib.pyplot as plt # Define and load the data data = { 'Div': ['Bundesliga', 'EPL', 'La Liga'], 'First_Half': [0.438, 0.434, 0.441], 'Second_Half': [0.562, 0.566, 0.559] } df = pd.DataFrame(data)
Next, we'll loop through each league in the DataFrame to generate a dedicated pie chart for each. We'll add clear labels, precise percentage displays, and league-specific titles to make the charts easy to interpret:
# Iterate over each league to create its pie chart for _, row in df.iterrows(): league = row['Div'] # Extract the goal split values split_values = [row['First_Half'], row['Second_Half']] half_labels = ['First Half', 'Second Half'] # Create a new figure for each chart (avoids overlapping) plt.figure(figsize=(6, 6)) # Generate the pie chart with percentage labels plt.pie(split_values, labels=half_labels, autopct='%1.3f%%', startangle=90) # Add a descriptive title plt.title(f'Goal Split by Half - {league}') # Ensure the pie is a perfect circle (not stretched) plt.axis('equal') # Display the chart plt.show()
Quick Notes on Customization:
autopct='%1.3f%%': This shows the exact percentage with three decimal places, matching the precision of your original data.startangle=90: Rotates the chart so the "First Half" slice starts at the top, making it more intuitive to read.- If you want to save the charts instead of just displaying them, add
plt.savefig(f'{league}_goal_split.png')right beforeplt.show().
内容的提问来源于stack exchange,提问作者the man
相关产品推荐
相关产品推荐

