我有一些每周时间序列数据,我想要绘制图表。我只希望时间序列中的日期显示在图表中,即我不希望Excel在时间序列中的日期之间插入空点。
我尝试将text_axis
明确设置为True
,date_axis
设置为False
,但我最终得到了自动轴和空点:
如果我然后手动将Axis类型设置为文本,那么我就会得到我想要的内容:
这是在第一个屏幕截图中创建图表的代码:
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)
知道我做错了吗?
答案 0 :(得分:1)
这是我的坏事。
我的VirtualEnv中是旧版本的XlsxWriter。该模块的作者约翰麦克纳马拉让我正确,它现在正在工作。我的小学生错误!
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...