You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Python按时间分段绘制连续数据箱线图的实现指导

Got it, let's tackle this step by step. Handling 50k rows of time-series data and plotting boxplots by month/week with seaborn is totally doable, and should be faster than your R experience. Here's a complete walkthrough:

Step 1: Import Required Libraries

First, we'll load the core tools for data processing and visualization:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
Step 2: Load and Parse the CSV Data

The key here is to let pandas automatically parse your Timestamp column into datetime objects—this makes extracting month/week data trivial later.

# Replace 'your_data.csv' with your actual file path
df = pd.read_csv('your_data.csv', parse_dates=['Timestamp'])

# Quick check to confirm parsing worked
print(df.dtypes)
# You should see Timestamp listed as datetime64[ns]
Step 3: Create Time-Segment Columns

We'll add two new columns to group your data by month and year-week (to avoid cross-year week overlap):

# Add month column (uses full month names for readability)
df['Month'] = df['Timestamp'].dt.month_name()

# Optional: Use numeric months (1-12) instead if you prefer
# df['Month'] = df['Timestamp'].dt.month

# Add year-week column (formatted as YYYY-WXX, e.g., 2024-W03)
# This prevents mixing up week 52 from 2023 and week 52 from 2024
df['Year_Week'] = df['Timestamp'].apply(lambda x: f"{x.year}-W{x.isocalendar().week:02d}")
Step 4: Plot Boxplots with Seaborn

Boxplot by Month

We'll enforce a chronological order for months (instead of alphabetical) to make the plot intuitive:

# Set a clean seaborn style
sns.set_style("whitegrid")

# Define month order to avoid alphabetical sorting
month_order = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August']

plt.figure(figsize=(12, 6))
sns.boxplot(data=df, x='Month', y='Heat', order=month_order)
plt.title('Heat Distribution by Month')
plt.xlabel('Month')
plt.ylabel('Heat Value')
plt.xticks(rotation=45)  # Rotate labels to prevent overlap
plt.tight_layout()  # Adjust layout to fit all elements
plt.show()

Boxplot by Year-Week

For weekly segments, we'll sort the week labels chronologically to keep the plot in time order:

plt.figure(figsize=(16, 6))
# Sort week labels to ensure chronological order
sorted_weeks = sorted(df['Year_Week'].unique())

sns.boxplot(data=df, x='Year_Week', y='Heat', order=sorted_weeks)
plt.title('Heat Distribution by Year-Week')
plt.xlabel('Year-Week')
plt.ylabel('Heat Value')
plt.xticks(rotation=90)  # Rotate since there are many week labels
plt.tight_layout()
plt.show()
Step 5: Optimization Tips for Large Data

If you still notice lag with 50k rows, try these tweaks:

  • Hide outliers: Add showfliers=False to sns.boxplot() to skip calculating and plotting outliers (speeds up rendering).
  • Precompute stats: Calculate boxplot metrics (quartiles, median, etc.) upfront with pandas grouping, then plot directly with matplotlib:
    # Precompute monthly stats
    monthly_groups = [df[df['Month'] == m]['Heat'] for m in month_order]
    
    plt.figure(figsize=(12,6))
    plt.boxplot(monthly_groups)
    plt.xticks(range(1, len(month_order)+1), month_order)
    plt.title('Heat Distribution by Month')
    plt.xlabel('Month')
    plt.ylabel('Heat Value')
    plt.xticks(rotation=45)
    plt.tight_layout()
    plt.show()
    

内容的提问来源于stack exchange,提问作者Unknown

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:30:12