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

如何用Highcharts(Highstock)展示2015年1-50周的年-周格式数据?

How to Format X-Axis as "Year-Week" for 2015 Weekly Data

Hey there! Let's figure out how to get those clean "YYYY-WWW" labels (like 2015-W01) on your X-axis for your 2015 weekly data. I'll cover two practical approaches depending on your needs: using actual date objects (great for time series operations) or generating custom string labels (quick and straightforward).


Approach 1: Use Date Objects with Matplotlib Date Formatter

If you want to keep your data tied to actual dates (useful if you might extend the dataset later), we can generate the corresponding week dates for 2015 and format them to show the year-week string.

Here's the full code example:

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter

# Your 50 weeks of 2015 data
weekly_data = [1884,2936,2039,1948,1814,2071,2183,3234,3426,2188,
               1884,2936,2039,1948,1814,2071,2183,3234,3426,2188,
               1884,2936,2039,1948,1814,2071,2183,3234,3426,2188,
               1884,2936,2039,1948,1814,2071,2183,3234,3426,2188,
               1884,2936,2039,1948,1814,2071,2183,3234,3426,2188]

# Generate start dates for each week in 2015 (adjust freq if your week starts on a different day)
# 'W-MON' means weeks start on Monday; use 'W-SUN' if your week starts on Sunday
week_dates = pd.date_range(start='2015-01-01', periods=50, freq='W-MON')

# Pair dates with your data
df = pd.DataFrame({'week_date': week_dates, 'value': weekly_data})

# Create the plot
fig, ax = plt.subplots(figsize=(12, 6))
ax.plot(df['week_date'], df['value'])

# Format X-axis to show "YYYY-WWW"
# %U = week number (Sunday as first day of week), %W = week number (Monday as first day)
date_format = DateFormatter('%Y-W%U')
ax.xaxis.set_major_formatter(date_format)

# Rotate labels to avoid overlap
plt.xticks(rotation=45)

# Add labels and title
plt.title('2015 Weekly Data (Weeks 1-50)')
plt.xlabel('Week (Year-Week)')
plt.ylabel('Value')
plt.tight_layout()
plt.show()

Key Notes:

  • Adjust the freq parameter in pd.date_range to match your week's start day (e.g., W-SUN for Sunday-start weeks).
  • Use %W instead of %U in the date formatter if your week starts on Monday (this changes how week numbers are calculated).

Approach 2: Custom String Labels (No Date Logic Needed)

If you don't need to work with actual date objects, you can directly generate "2015-W01" to "2015-W50" as string labels for your X-axis. This is simpler if you just need the visual label format.

Here's how to do it:

import matplotlib.pyplot as plt

# Your 50 weeks of 2015 data
weekly_data = [1884,2936,2039,1948,1814,2071,2183,3234,3426,2188,
               1884,2936,2039,1948,1814,2071,2183,3234,3426,2188,
               1884,2936,2039,1948,1814,2071,2183,3234,3426,2188,
               1884,2936,2039,1948,1814,2071,2183,3234,3426,2188,
               1884,2936,2039,1948,1814,2071,2183,3234,3426,2188]

# Generate custom year-week labels (zfill(2) ensures 2-digit week numbers)
week_labels = [f'2015-W{str(week_num).zfill(2)}' for week_num in range(1, 51)]

# Create the plot
fig, ax = plt.subplots(figsize=(12, 6))
ax.plot(week_labels, weekly_data)

# Rotate labels for readability
plt.xticks(rotation=45)

# Add labels and title
plt.title('2015 Weekly Data (Weeks 1-50)')
plt.xlabel('Week (Year-Week)')
plt.ylabel('Value')
plt.tight_layout()
plt.show()

Key Note:

  • str(week_num).zfill(2) converts week numbers like 1 to 01, so all labels have a consistent "WXX" format.

Either approach will give you the clean "year-week" X-axis labels you're looking for. Pick the one that fits your workflow best!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:39:08