0Pricing
Pandas & NumPy Academy · درس

تصور الاتجاه الشهري

مثّل الإيرادات الشهرية بمخطط خطي، وأضف متوسطًا متحركًا لثلاثة أشهر، وعلّق على أشهر الذروة والقاع.

تصور الاتجاه الشهري درس مجاني في Pandas & NumPy Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Pandas & NumPy Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Pandas & NumPy Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Computing Monthly Revenue Totals

Before plotting, aggregate the sales DataFrame to monthly totals. Group by the year_month Period column and sum revenue. Convert the PeriodIndex to a DatetimeIndex with .to_timestamp() so Matplotlib's date formatter renders tick labels correctly. Sorting by index ensures the line chart flows left to right chronologically.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_parquet('sales_features.parquet')

monthly = df.groupby('year_month')['revenue'].sum().sort_index()
monthly.index = monthly.index.to_timestamp()
print(monthly.head())

Plotting the Monthly Revenue Line Chart

Create a Figure and Axes with plt.subplots(figsize=(12, 5)) and call ax.plot() to draw the monthly revenue trend. Use marker='o' to place a dot at each month so individual data points are visible even when the line is smooth. Set the colour and line width to make the primary trend stand out from any overlays you add later.

fig, ax = plt.subplots(figsize=(12, 5))

ax.plot(monthly.index, monthly.values,
        color='steelblue', linewidth=2, marker='o', markersize=5,
        label='Monthly Revenue')

ax.set_title('Monthly Revenue Trend')
ax.set_xlabel('Month')
ax.set_ylabel('Revenue (USD)')
plt.tight_layout()
plt.show()

Overlaying a 3-Month Moving Average

A raw monthly revenue line can appear jagged due to seasonal noise. Overlay a 3-month rolling average computed with monthly.rolling(3).mean() to show the underlying trend more clearly. Plot it as a dashed line in a contrasting colour so it stands out from the raw series without obscuring it.

rolling_avg = monthly.rolling(3).mean()

ax.plot(rolling_avg.index, rolling_avg.values,
        color='darkorange', linewidth=2, linestyle='--',
        label='3-Month Moving Avg')

ax.legend()
plt.show()

Annotating the Peak Month

Use ax.annotate() to mark the month with the highest revenue. Find the peak with monthly.idxmax() and its value with monthly.max(). The annotation arrow points from a text label to the peak data point, making the insight immediately visible to any reader of the chart, including those who receive it as a PNG or PDF.

peak_date = monthly.idxmax()
peak_val = monthly.max()

ax.annotate(f'Peak: ${peak_val:,.0f}',
            xy=(peak_date, peak_val),
            xytext=(peak_date, peak_val * 1.1),
            arrowprops=dict(arrowstyle='->', color='red'),
            color='red', fontsize=10)
plt.show()

Annotating the Trough Month

Similarly, mark the month with the lowest revenue using monthly.idxmin() to flag potential seasonality or disruptions. Annotations on both the peak and trough provide a complete story: they let stakeholders see not only the best month but also how far revenue can dip, which informs cash-flow planning.

trough_date = monthly.idxmin()
trough_val = monthly.min()

ax.annotate(f'Trough: ${trough_val:,.0f}',
            xy=(trough_date, trough_val),
            xytext=(trough_date, trough_val * 0.85),
            arrowprops=dict(arrowstyle='->', color='navy'),
            color='navy', fontsize=10)
plt.show()

Formatting Date Tick Labels

Matplotlib's default date tick labels often overlap or use an unreadable format. Import mdates from matplotlib and set a MonthLocator and DateFormatter to control tick frequency and label format. Rotating tick labels by 45 degrees prevents overlap on a 12-month axis.

import matplotlib.dates as mdates

ax.xaxis.set_major_locator(mdates.MonthLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %Y'))

plt.xticks(rotation=45, ha='right')
plt.tight_layout()
plt.show()

Adding a Shaded Region for Q4

Highlight the Q4 period (October–December) with a shaded background using ax.axvspan(). This makes seasonal patterns immediately visible: if the peak consistently falls within the shaded zone, Q4 is a key selling period worth targeting with promotions. The alpha parameter controls the shading transparency.

import datetime

q4_start = datetime.datetime(2023, 10, 1)
q4_end = datetime.datetime(2023, 12, 31)

ax.axvspan(q4_start, q4_end, alpha=0.15, color='gold', label='Q4')
ax.legend()
plt.show()

Plotting Revenue by Category Over Time

Create a multi-line chart where each line represents a product category. Pivot the monthly-by-category DataFrame so columns are categories and the index is time, then iterate over columns and call ax.plot() for each. Matplotlib automatically assigns a different colour to each line when called repeatedly on the same Axes.

monthly_cat = df.groupby(['year_month', 'category'])['revenue'].sum().unstack('category')
monthly_cat.index = monthly_cat.index.to_timestamp()

fig, ax = plt.subplots(figsize=(12, 6))
for col in monthly_cat.columns:
    ax.plot(monthly_cat.index, monthly_cat[col], label=col, linewidth=1.5)

ax.legend(loc='upper left', fontsize=8)
ax.set_title('Monthly Revenue by Category')
plt.tight_layout()
plt.show()

Stacked Bar Chart for Monthly Revenue

A stacked bar chart shows the total monthly revenue while also revealing each category's contribution to the stack. Use monthly_cat.plot(kind='bar', stacked=True, ax=ax) directly on the pivoted DataFrame. Stacked bars are best when you have fewer than six categories, otherwise the colours become hard to distinguish.

fig, ax = plt.subplots(figsize=(12, 5))

monthly_cat.plot(kind='bar', stacked=True, ax=ax, colormap='tab10')

ax.set_title('Monthly Revenue by Category (Stacked)')
ax.set_xlabel('Month')
ax.set_ylabel('Revenue')
ax.legend(loc='upper left', fontsize=8)
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

Month-over-Month Growth Rate

Plot the month-over-month percentage growth rate alongside the revenue to give context: a visually small bar in January might represent a 20 % growth jump from the prior month. Compute growth with monthly.pct_change() * 100 and plot it on a secondary y-axis using ax.twinx() to share the same x-axis without conflating units.

growth = monthly.pct_change() * 100

fig, ax1 = plt.subplots(figsize=(12, 5))
ax2 = ax1.twinx()

ax1.bar(monthly.index, monthly.values, width=20, color='steelblue', alpha=0.6, label='Revenue')
ax2.plot(growth.index, growth.values, color='red', marker='o', label='MoM Growth %')

ax1.set_ylabel('Revenue')
ax2.set_ylabel('MoM Growth %')
plt.title('Revenue and Month-over-Month Growth')
plt.tight_layout()
plt.show()

Saving the Figure for Reports

Use fig.savefig('monthly_trend.png', dpi=150, bbox_inches='tight') to produce a high-resolution PNG suitable for slide decks or web dashboards. The bbox_inches='tight' argument trims excess white space, and dpi=150 gives a sharp image without an excessively large file. Save to PDF as well for print-quality reports.

fig.savefig('monthly_trend.png', dpi=150, bbox_inches='tight')
fig.savefig('monthly_trend.pdf', bbox_inches='tight')
print('Figure saved as PNG and PDF')

Quick Check

Test your understanding of Data Analysis concepts from this lesson.

Lesson Recap

In this lesson you learned: computing monthly revenue totals and plotting them as a line chart, overlaying a rolling average and annotating peak and trough months, and saving publication-quality figures to PNG and PDF. Next up we explore sessionisation and event sequencing in user behaviour data.

الأسئلة الشائعة

هل درس «تصور الاتجاه الشهري» مجاني؟

نعم — نص درس «تصور الاتجاه الشهري» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Pandas & NumPy Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Pandas & NumPy Academy 4 دروس في المجموع.

ماذا ستتعلم في «تصور الاتجاه الشهري»؟

مثّل الإيرادات الشهرية بمخطط خطي، وأضف متوسطًا متحركًا لثلاثة أشهر، وعلّق على أشهر الذروة والقاع. تتمرن على Pandas & NumPy Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Pandas & NumPy Academy؟

لا تُشترط خبرة سابقة. Pandas & NumPy Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «تصور الاتجاه الشهري»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Pandas & NumPy Academy هذا؟

نعم. كل درس في Pandas & NumPy Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تحميل مجموعة بيانات المبيعات وتدقيقها
  2. حساب الإيرادات وهندسة الميزات
  3. تحليل GroupBy حسب المنطقة والفئة
  4. تصور الاتجاه الشهري
← العودة إلى Pandas & NumPy Academy