我有一些我想要绘制的每周时间序列数据。我只希望时间序列中的日期出现在图表中,即我不希望 Excel 在时间序列中的日期之间插入空点。
我已经尝试将 text_axis 显式设置为 True 并将 date_axis 显式设置为 False 但我最终得到了一个自动轴和空点:
如果我随后手动将 Axis 类型设置为 Text,那么我会得到我想要的:
这是在第一个屏幕截图中创建图表的代码:
import pandas as pd
# Create a Pandas series with a date index
data = pd.Series([1, 5, 7, 2, 6, 6],
index=pd.to_datetime(['2012-10-08',
'2012-10-15',
'2012-10-22',
'2012-10-29',
'2012-11-05',
'2012-11-12']))
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
df = pd.DataFrame(data)
# Stick it in Excel
worksheet_name = 'test'
df.to_excel(writer, worksheet_name)
# Create a graph
workbook = writer.book
chart = workbook.add_chart({'type': 'column'})
chart.add_series({'values': '={worksheet_name}!$B$2:$B$7'.format(worksheet_name=worksheet_name),
'categories': '={worksheet_name}!$A$2:$A$7'.format(worksheet_name=worksheet_name)})
# Set the X axis to be a text axis
chart.set_x_axis({'name': 'Week', 'text_axis': True, 'date_axis': False})
# Stick the graph in Excel
sheets = [sheet for sheet in workbook.worksheets() if sheet.name == worksheet_name]
sheets[0].insert_chart('D2', chart)
# Make the date column wide enough for the date to be visible
sheets[0].set_column(0, 0, 20)
知道我做错了什么吗?
请您参考如下方法:
这是我的错。
在我的 VirtualEnv 中使用旧版本的 XlsxWriter。该模块的作者约翰·麦克纳马拉 (John McNamara) 说对了,它现在可以正常工作了。我的小学生错误!
pip install --upgrade xlsxwriter
Downloading/unpacking xlsxwriter from https://pypi.python.org/packages/2.7/X/XlsxWriter/XlsxWriter-0.7.2-py2.py3-none-any.whl#md5=8dca87f0bd21708a0587017c14e5f453
Downloading XlsxWriter-0.7.2-py2.py3-none-any.whl (133kB): 133kB downloaded
Installing collected packages: xlsxwriter
Found existing installation: XlsxWriter 0.5.7
Uninstalling XlsxWriter:
Successfully uninstalled XlsxWriter
Successfully installed xlsxwriter
Cleaning up...
