2013-06-03 8 views
1

enter image description here使用xslwriter

我可以使用Python中xlsxwriter由行属性生成图表创建图表点。但是我需要在特定的值上加上4个点(菱形)。任何人都可以向我建议如何使用xlsxwriter中的线条属性将钻石放在特定的值上。

如果需要比我还可以发布代码。

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='test') 
    workbook = Workbook('graphs.xlsx') 
    worksheet = workbook.add_worksheet() 

    heading =['test1','test2','test3','test4','test5'] 
    worksheet.write_row('A1',heading) 
    count =2 
    while(count < worksheet_final.get_highest_row()): 
     data_x = worksheet_final.cell(row = count, column = 1).value 
     data_y = 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,round(data_x,0)) 
     worksheet.write(count,1,data_y) 
     worksheet.write(count,2,data_s1) 
     worksheet.write(count,3,data_d0) 
     worksheet.write(count,4,data_d1) 
     count = count + 1 
    # Create a new chart with properties object. 
    chart = workbook.add_chart({'type': 'line'}) 
    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$1:$B$503', 
    'name':'clk2q0_S', 
    'line':{'color':'blue'}, 
    }) 

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

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

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

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

if __name__ == '__main__': 
    table_percentage = "5%" 
    table_threashold = {} 
    #Need to put dot on the below 4 value 
    table_threashold ['setup_mt1'] = [5,210] 
    table_threashold ['setup_mt0'] = [-105,140] 
    table_threashold ['hold_mt0'] = [-39,143] 
    table_threashold ['hold_mt1'] = [-41,96] 
    headline = "sample_data" 

    graph(headline,table_percentage,table_threashold) 
+0

您能否显示您迄今为止编写的代码?而且,通过在特定值上放4个点是什么意思? – alecxe

+0

抱歉,回滚 - 稍微修改了编辑:( –

+0

@alecxe,我的意思是,我们需要代表某个特定值的符号,换句话说,它代表阈值级别。例如:(10.200),我们可以将一些符号在线上,因此用户可以轻松识别阈值级别 – user765443

回答

2

XlsxWriter这是不可能的系列开启个别点标记,但也可以把点标记功能,用于标记的一个系列。为此,您需要使用points (see docs)系列选项并关闭不需要的标记。

from xlsxwriter.workbook import Workbook 

workbook = Workbook('chart_point.xlsx') 

worksheet = workbook.add_worksheet() 
chart = workbook.add_chart({'type': 'line'}) 

data = [ 
    [1, 2, 3, 4, 5], 
    [2, 4, 6, 8, 10], 
    [3, 6, 9, 12, 15], 

] 

# Write the data for the chart. 
worksheet.write_column('A1', data[0]) 
worksheet.write_column('B1', data[1]) 
worksheet.write_column('C1', data[2]) 

# Add a chart series with markers but turn some off. 
chart.add_series({ 
    'categories': '=Sheet1!$A$1:$A$5', 
    'values': '=Sheet1!$B$1:$B$5', 
    'marker': {'type': 'automatic'}, 
    'points': [ 
      {'fill': {'none': True}, 'line': {'none': True}}, 
      {'fill': {'none': True}, 'line': {'none': True}}, 
      {'fill': {'none': True}, 'line': {'none': True}}, 
      {'fill': {'none': True}, 'line': {'none': True}}, 
    ], 
}) 

# Add a second chart series. 
chart.add_series({ 
    'categories': '=Sheet1!$A$1:$A$5', 
    'values': '=Sheet1!$C$1:$C$5', 
    'marker': {'type': 'automatic'}, 
    'points': [ 
      {'fill': {'none': True}, 'line': {'none': True}}, 
      {}, 
      {'fill': {'none': True}, 'line': {'none': True}}, 
      {'fill': {'none': True}, 'line': {'none': True}}, 
      {'fill': {'none': True}, 'line': {'none': True}}, 
    ], 
}) 

worksheet.insert_chart('E9', chart) 

workbook.close() 

enter image description here

这意味着你只能说明是系列的一部分标记,即存在于XlsxWriter没有工厂增加一个点的图表,是不是一个系列的一部分。