我编写了使用 xlsxwriter 模块生成图形的小脚本。它默认采用 x 和 y 轴的值。我想自定义这些值。谁能指出我来解决这个问题。截至目前,对于 X 轴,它需要 -300 到 300 和 Y 轴 0 到 300,但我正在寻找 Y 轴 -100,间隔为 2 到 100,X 到 0 到 -100,间隔。
谢谢您的回复。刚刚编辑了我的问题,例如 x 和 y 轴[-100,98...0,100]
编辑另一个问题:我们可以放大或缩小由 xlsx writer 创建的图形吗
一种选择是,更改 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)