简体   繁体   中英

customize X and Y axis value in xlsxwriter

I have written small script which generate graph using xlsxwriter module.it is taking by default value for x and y axis. I want to customize those value. Can anyone point me out to fix this issue. As of now for X axis it takes -300 to 300 and Y axis 0 to 300 but I am looking for Y axis -100 with 2 interval to 100 and X to 0 to -100 with interval.

Thanks for reply. Just Edited my question like for x and y axis[ -100,98...0,100]

Editing for One more question: Can we do zoom in or zoom out of graph which is created by xlsx writer

One option was, change the Excel formula but it will change the graph also and does not work for me. or another words, Can I do zoom in of existing excel chart

from openpyxl import load_workbook
from xlsxwriter.workbook import Workbook
import math
def graph(headline,table_percentage,table_threashold):
    """Create Graph Chart"""
    wb = load_workbook(filename = 'sample.xlsx')
    worksheet_final = wb.get_sheet_by_name(name='SUM_F_SCDLIB_DFF')
    workbook = Workbook('graphs.xlsx')
    worksheet = workbook.add_worksheet()
    worksheet.set_zoom(100)
    worksheet.set_print_scale(400)

    heading =['d2clksu0','clk2q0_S','clk2q1_S','clk2q0_H','clk2q1_H']
    worksheet.write_row('A1',heading)
    count =2
    base_list = []
    while(count < worksheet_final.get_highest_row()):
        data_x = worksheet_final.cell(row = count, column = 1).value
        data_s0 = worksheet_final.cell(row = count, column = 2).value
        data_s1 = worksheet_final.cell(row = count, column = 8).value
        data_d0 = worksheet_final.cell(row = count, column = 14).value
        data_d1 = worksheet_final.cell(row = count, column = 20).value
        worksheet.write(count,0,data_x)
        worksheet.write(count,1,data_s0)
        worksheet.write(count,2,data_s1)
        worksheet.write(count,3,data_d0)
        worksheet.write(count,4,data_d1)
        base_list.append(round(data_x,0))
        count = count + 1
    # Create a new chart with properties object.
    chart = workbook.add_chart({'type': 'scatter',
                                'subtype': 'smooth'})
    chart.show_hidden_data()
    chart.set_high_low_lines()
    cellname = headline
    chart.set_title({'name':cellname})
    chart.set_x_axis({'name':'CLK-D Time (ps)',
                  'name_font':{'size':14,'bold':True},
                  })
    chart.set_y_axis({'name':'CLK-Q Time (ps)',
                  'name_font':{'size':14,'bold':True},
                  })
    chart.set_size({'width': 720, 'height': 576})


    # Add a series to the chart.
    chart.add_series({
    'categories' : '=Sheet1!$A$2:$A$503',
    'values': '=Sheet1!$B$2:$B$503',
    'name':'clk2q0_S',
    'line':{'color':'blue'}})

    chart.add_series({
    'categories' : '=Sheet1!$A$2:$A$503',
    'values': '=Sheet1!$C$2:$C$503',
    'name':'clk2q1_S',
    'line':{'color':'red'}})

    chart.add_series({
    'categories' : '=Sheet1!$A$2:$A$503',
    'values': '=Sheet1!$D$2:$D$503',
    'name':'clk2q0_H',
    'line':{'color':'blue'}})

    chart.add_series({
    'categories' : '=Sheet1!$A$2:$A$503',
    'values': '=Sheet1!$E$2:$E$503',
    'name':'clk2q1_H',
    'line':{'color':'red'}})

    #Create Table
    table_heading_percentage = table_percentage
    table_heading = [ table_heading_percentage,'CK-D','CK-Q']
    table_column = ['D1_SU','D0_SU','D0_HD','D1_HD']
    format = workbook.add_format()
    format.set_font_color('blue')
    format.set_font_size(10)
    format.set_bold()
    worksheet.write_row(2,5,table_heading,format)
    worksheet.write_column(3,5,table_column,format)
    list_key = ['setup_mt1', 'setup_mt0', 'hold_mt0', 'hold_mt1']
    row = 3
    for key in list_key:
        column = 6
        worksheet.write(row,column,table_threashold[key][0])
        worksheet.write(row,column + 1,table_threashold[key][1])
        row = row + 1


    # Insert the chart into the worksheet.
    worksheet.insert_chart('K1', chart)
    workbook.close()

if __name__ == '__main__':
    table_percentage = "5%"
    table_threashold = {}
    table_threashold ['setup_mt1'] = [-127,97]
    table_threashold ['setup_mt0'] = [-105,140]
    table_threashold ['hold_mt0'] = [-39,143]
    table_threashold ['hold_mt1'] = [-41,96]
    headline = """graph"""

    graph(headline,table_percentage,table_threashold)

在此处输入图片说明

Use set_x_axis() and set_y_axis() methods:

chart.set_x_axis({'min': -100, 'max': 0})
chart.set_y_axis({'min': -100, 'max': 100})

To use intervals on an axis, look at 'major_unit'.

https://xlsxwriter.readthedocs.io/chart.html#major_unit

The below takes the basic example from above and changes the interval from 'auto' (which equates to 2 in this example) to 4 on the y axis:

import xlsxwriter

workbook = xlsxwriter.Workbook('chart.xlsx')
worksheet = workbook.add_worksheet()

# Create a new Chart object.
chart = workbook.add_chart({'type': 'column'})

# Write some data to add to plot on the chart.
data = [
    [1, 2, 3, 4, 5],
    [2, 4, 6, 8, 10],
    [3, 6, 9, 12, 15],
]

worksheet.write_column('A1', data[0])
worksheet.write_column('B1', data[1])
worksheet.write_column('C1', data[2])

# Configure the chart. In simplest case we add one or more data series.
chart.add_series({'values': '=Sheet1!$A$1:$A$5'})
chart.add_series({'values': '=Sheet1!$B$1:$B$5'})
chart.add_series({'values': '=Sheet1!$C$1:$C$5'})

# major_unit sets the interval for the axis, overriding the default of 'auto'
chart.set_y_axis({'major_unit': 4})

# Insert the chart into the worksheet.
worksheet.insert_chart('A7', chart)

workbook.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM