如何用Highcharts(Highstock)展示2015年1-50周的年-周格式数据?
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
freqparameter inpd.date_rangeto match your week's start day (e.g.,W-SUNfor Sunday-start weeks). - Use
%Winstead of%Uin 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 like1to01, 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

