问题描述
我可以使用 Python 中的 xlsxwriter
通过线属性生成图形.但我需要把特定值上的 4 个点(菱形).谁能建议我如何使用 xlsxwriter
中的线属性将菱形放在特定值上.
I am able to generate graph using xlsxwriter
in Python by line properties. But I need to put4 dots(diamond) on the particular value. Can anyone suggest me How Can I put diamond on the particular value using line properties in xlsxwriter
.
如果需要,我也可以发布代码.
if require than I can post the code also.
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)
推荐答案
在 XlsxWriter 无法打开系列中的单个点标记,但可以关闭带有标记的系列的点标记.为此,您需要使用 点(请参阅文档) 系列选项并关闭您不想要的标记.
In XlsxWriter it isn't possible to turn on individual point markers in a series but it is possible to turn point markers off for a series with markers. To do this you need to use the points (see docs) series option and turn off markers that you don't want.
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()
这意味着您只能指示属于系列的标记,即 XlsxWriter 中没有用于向不属于系列的图表添加点的功能.
This implies that you can only indicate markers that are part of the series, i.e., there is no facility in XlsxWriter for adding a point to a chart that isn't part of a series.
这篇关于使用 xslwriter 在图中创建点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!