在xlsxwriter中自定义X和Y轴值

时间:2013-09-18 11:01:13

标签: python xlsxwriter

我编写了一个小脚本,它使用xlsxwriter模块生成图形。它采用x和y轴的默认值。我想定制这些价值。任何人都可以指出我解决这个问题。截至目前,对于X轴,它需要-300到300,Y轴需要0到300,但我正在寻找Y轴-100,其中2个间隔为100,X为0到-100,间隔为。

感谢您的回复。刚编辑了我的问题,比如x和y轴[-100,98 ... 0,100]

  

编辑还有一个问题:我们可以放大或缩小由其创建的图形   xlsx作家

一个选项是,更改Excel公式但它也会更改图形,但对我来说不起作用。 或者换句话说,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)

enter image description here

2 个答案:

答案 0 :(得分:3)

使用set_x_axis()set_y_axis()方法:

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

答案 1 :(得分:1)

要在轴上使用间隔,请查看&#39; major_unit&#39;。

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

以下是上面的基本示例,并更改了自动&#39;的间隔。 (在本例中等于2)到y轴上的4:

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()