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

在单张图表中对比四年月度频次统计数据的实现需求

Monthly Frequency Comparison for 2015-2018: Step-by-Step Visualization

Got it, let's get your four years of monthly frequency data plotted together so you can easily spot trends and differences. The key first step is making sure your data is properly structured (since value_counts() returns results sorted by frequency, not month order) before visualizing. Here's how to do it with pandas, matplotlib, and optionally seaborn:

1. Clean & Combine Your Data

First, we'll take each year's value_counts() output, sort it by month (so we get Jan-Dec in order), and merge them into a single DataFrame with years as columns and months as rows.

import pandas as pd

# Sort each year's data by month (1-12) and rename columns to the year
df_2015 = newdf2015.Month.value_counts().sort_index().rename('2015')
df_2016 = newdf2016.Month.value_counts().sort_index().rename('2016')
df_2017 = newdf2017.Month.value_counts().sort_index().rename('2017')
df_2018 = newdf2018.Month.value_counts().sort_index().rename('2018')

# Combine all into one DataFrame
combined_data = pd.concat([df_2015, df_2016, df_2017, df_2018], axis=1)

Why sort_index()? Because value_counts() defaults to sorting by frequency (highest first), which would mess up your x-axis order. This ensures months are displayed 1 through 12 correctly.

2. Visualize with Matplotlib

Line plots are perfect for seeing how monthly frequencies change across years:

import matplotlib.pyplot as plt

# Set up the plot size
plt.figure(figsize=(12, 6))

# Plot each year's data as a line with markers
combined_data.plot(kind='line', marker='o', ax=plt.gca())

# Add labels and styling
plt.title('Monthly Frequency Comparison (2015-2018)', fontsize=14)
plt.xlabel('Month', fontsize=12)
plt.ylabel('Frequency', fontsize=12)
plt.xticks(range(1, 13))  # Force x-axis to show all 12 months
plt.legend(title='Year')
plt.grid(alpha=0.3)  # Light grid for readability

plt.show()

Option 2: Grouped Bar Plot (Great for Direct Month-to-Month Comparison)

If you want to compare exact counts for each month across years, a grouped bar plot works well:

plt.figure(figsize=(12, 6))

combined_data.plot(kind='bar', ax=plt.gca())

plt.title('Monthly Frequency Comparison (2015-2018)', fontsize=14)
plt.xlabel('Month', fontsize=12)
plt.ylabel('Frequency', fontsize=12)
plt.xticks(rotation=0)  # Keep month labels horizontal for readability
plt.legend(title='Year')

plt.show()

3. Alternative: Seaborn for a Polished Look

If you prefer seaborn's styling, you can reshape the data into long format and use lineplot:

import seaborn as sns

# Convert wide-format DataFrame to long-format
melted_data = combined_data.reset_index().melt(
    id_vars='index', 
    var_name='Year', 
    value_name='Frequency'
)
melted_data.rename(columns={'index': 'Month'}, inplace=True)

# Create the plot
plt.figure(figsize=(12, 6))
sns.lineplot(
    data=melted_data,
    x='Month',
    y='Frequency',
    hue='Year',
    marker='o',
    linewidth=2
)

plt.title('Monthly Frequency Comparison (2015-2018)', fontsize=14)
plt.xlabel('Month', fontsize=12)
plt.ylabel('Frequency', fontsize=12)
plt.xticks(range(1, 13))
plt.grid(alpha=0.3)

plt.show()

All these methods will give you a single chart where you can easily compare, for example, how December frequencies dropped in 2015 and 2017, or how June 2017 had the highest count across all years.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:22:54