TylerH
TylerH

Reputation: 132

xlsxwriter chart grid spacing

I'm currently trying to create charts in excel via the xlsxwriter python module. I've gotten it to graph just fine, however I'm trying to get it to look nice. I've been able to add major and minor gridlines, change their line width, and dash type, but I can't figure out how to modify their spacing. (e.g., minor lines on every unit with major lines on every 5 units)

As far as I can tell, it's not an option, but I thought I'd throw this out there to see if an expert had a trick (I've just started using this module) and/or put this out there for people looking for this option in the future.

Here's the code I'm working with right now:

chart = wb.add_chart({'type': 'line'})
chart.add_series({
    'name': 'Sheet1!{:}'.format(xlsxwriter.utility.xl_rowcol_to_cell(0, data_column)),
    'categories': ['Sheet1', 1, time_column, num_of_data_pts, time_column],
    'values': ['Sheet1', 1, i, num_of_data_pts, data_column],
    'line': {'color': 'blue'},
})
chart.set_x_axis({
    'num_font': {'italic': True},
    'major_gridlines': {
        'visible': True,
        'line': {'width': 1}
    },
    'minor_gridlines': {
        'visible': True,
        'line': {'width': 1, 'dash_type': 'dash'}
    },
})
chart.set_legend({'position': 'none'})
ws.insert_chart(5, 5, chart)

Also I've tried using the 'major_unit' attribute, but it appears that is something to do with if the x axis is a date only.

Upvotes: 1

Views: 1754

Answers (1)

jmcnamara
jmcnamara

Reputation: 41574

I can't figure out how to modify their spacing. (e.g., minor lines on every unit with major lines on every 5 units

I'm not sure if that is possible in Excel.

As explained in the XlsxWriter docs Excel treats Category and Values chart axes differently. The X axis for a line chart is a category axis and for text and numbers it is only possible to change the interval between the labels/ticks. You can verify that yourself in Excel.

That feature is also supported in XlsxWriter using the axis interval_unit.

Probably you need to format the chart as a scatter chart which has value axes for X and Y and which allows you to control the spacing

Upvotes: 1

Related Questions