Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Python - วาด Line Chart ใน Excel ด้วย XlsxWriter

Line chart (กราฟเส้น) แสดงการเคลื่อนไหวของราคา/ค่าในอดีตโดยเชื่อมจุดข้อมูลด้วยเส้นต่อเนื่อง เป็นกราฟพื้นฐานที่สุดในการเงิน แสดงราคาปิดตามเวลา

ตัวอย่างโค้ด


import xlsxwriter

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

bold = workbook.add_format({'bold': 1})

headings = ['Number', 'Batch 1', 'Batch 2']
data = [
    [2, 3, 4, 5, 6, 7],
    [80, 80, 100, 60, 50, 100],
    [60, 50, 60, 20, 10, 20],
]

worksheet.write_row('A1', headings, bold)
worksheet.write_column('A2', data[0])
worksheet.write_column('B2', data[1])
worksheet.write_column('C2', data[2])

# สร้าง Line Chart
chart1 = workbook.add_chart({'type': 'line'})

chart1.add_series({
    'name': '=Sheet1!$B$1',
    'categories': '=Sheet1!$A$2:$A$7',
    'values': '=Sheet1!$B$2:$B$7',
})

chart1.add_series({
    'name': ['Sheet1', 0, 2],
    'categories': ['Sheet1', 1, 0, 6, 0],
    'values': ['Sheet1', 1, 2, 6, 2],
})

chart1.set_title({'name': 'Results of data analysis'})
chart1.set_x_axis({'name': 'Test number'})
chart1.set_y_axis({'name': 'Data length (mm)'})
chart1.set_style(11)

worksheet.insert_chart('D2', chart1, {'x_offset': 25, 'y_offset': 10})
workbook.close()

Line Chart Subtypes


chart = workbook.add_chart({'type': 'line'})                          # Basic
chart = workbook.add_chart({'type': 'line', 'subtype': 'stacked'})    # Stacked
chart = workbook.add_chart({'type': 'line', 'subtype': 'percent_stacked'})  # 100% Stacked
chart = workbook.add_chart({'type': 'line', 'subtype': 'smooth'})     # Smoothed line

การปรับแต่งเส้น (Line Formatting)


chart1.add_series({
    'name': '=Sheet1!$B$1',
    'categories': '=Sheet1!$A$2:$A$7',
    'values': '=Sheet1!$B$2:$B$7',
    'line': {
        'color': 'red',
        'width': 2.25,
        'dash_type': 'solid',  # solid, dash, dot, dash_dot, dash_dot_dot
    },
    'marker': {
        'type': 'circle',
        'size': 6,
        'fill': {'color': 'red'},
        'border': {'color': 'black'},
    },
})

เมื่อไหร่ควรใช้ Line Chart

  • ข้อมูลต่อเนื่องตามเวลา (Time series)
  • Trend analysis
  • เปรียบเทียบหลาย series บนแกนเดียวกัน
  • แสดง pattern คาบเวลา

Line vs Scatter

Line ChartScatter Chart
Categories = text/date (ไม่ใช่ตัวเลข)ทั้ง X และ Y เป็นตัวเลข
จุดกระจายห่างเท่ากันตาม categoriesจุดวางตามค่า X จริง
เหมาะสำหรับ Time seriesเหมาะสำหรับ Correlation/Regression

ตัวอย่าง: Multiple Lines กับ Date Axis


import xlsxwriter
from datetime import date

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

date_format = workbook.add_format({'num_format': 'dd/mm/yyyy'})
bold = workbook.add_format({'bold': 1})

headings = ['Date', 'Product A', 'Product B']
data = [
    [date(2023, 1, 1), date(2023, 2, 1), date(2023, 3, 1), date(2023, 4, 1)],
    [100, 120, 110, 130],
    [90, 95, 105, 115],
]

worksheet.write_row('A1', headings, bold)
for i, row_data in enumerate(data):
    if i == 0:
        for j, val in enumerate(row_data):
            worksheet.write(j + 1, i, val, date_format)
    else:
        worksheet.write_column(chr(65 + i) + '2', row_data)

chart = workbook.add_chart({'type': 'line'})
chart.add_series({
    'name': '=Sheet1!$B$1',
    'categories': '=Sheet1!$A$2:$A$5',
    'values': '=Sheet1!$B$2:$B$5',
})
chart.add_series({
    'name': '=Sheet1!$C$1',
    'categories': '=Sheet1!$A$2:$A$5',
    'values': '=Sheet1!$C$2:$C$5',
})
chart.set_x_axis({'date_axis': True})  # ใช้ date axis
worksheet.insert_chart('E2', chart)
workbook.close()