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

使用Python XlsxWriter设置折线图X轴无效问题排查

Fix for Unapplied Category Axis with Date Labels in XlsxWriter Line Chart

The issue here is that when your categories range contains date values, XlsxWriter (and Excel by default) automatically switches the X-axis to a date axis instead of a discrete category axis. This means it’s treating your dates as continuous numerical values (based on Excel’s internal date numbering) rather than using the formatted date strings you’ve defined as discrete labels.

To resolve this, explicitly set the X-axis type to category after adding your series. Here’s how to modify your code:

Report_Chart = workbook.add_chartsheet('Report Numbers')
chart = workbook.add_chart({'type' : 'line'})

# Add your series as before
chart.add_series({'name': 'Report Source 1', 'categories' : '=Daily Report!$B$1:$W$1', 'values': '=Daily Report!$B$2:$W$2'})
chart.add_series({'name': 'Report Source 2', 'categories' : '=Daily Report!$B$1:$W$1', 'values': '=Daily Report!$B$3:$W$3'})

# Force the X-axis to use discrete category labels
chart.set_x_axis({'type': 'category'})

Report_Chart.set_chart(chart)

Why this works:

  • A category axis treats each entry in your categories range as a separate, unordered label (even if they’re dates), displaying exactly the formatted text from your worksheet cells.
  • A default date axis plots dates based on chronological spacing, which can skip or reorder labels if there are gaps, and won’t respect your custom "01 April 2020" format unless explicitly configured on the axis.

As an extra check, ensure the cells in Daily Report!$B$1:$W$1 are formatted with your desired date style in the worksheet (using worksheet.set_column() or cell-level formatting) to guarantee the labels appear correctly once the axis type is fixed.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 17:12:49