Please refer to the graph creation using xlwings. I would be happy if you pointed out that it is better to do this. If you use api, you can use vba code → If you don't understand, you can check it with "Record macro" in Excel. test.xlsx is a blank workbook.
python 3.7 xlwings 0.18.0
import xlwings as xw
from xlwings.constants import AxisType
DATA_NUM = 10
#Reading Excel workbook
wb = xw.Book('test.xlsx')
#Insert data
data_x = list(x for x in range(DATA_NUM))
data_y1 = list(x*2 for x in range(DATA_NUM))
data_y2 = list(x*10-50 for x in range(DATA_NUM))
xw.Range('A1').value = 'x'
xw.Range('B1').value = 'y1'
xw.Range('C1').value = 'y2'
cell_x = xw.Range('A2')
cell_y1 = xw.Range('B2')
cell_y2 = xw.Range('C2')
for cnt in range(DATA_NUM):
cell_x.value = data_x[cnt]
cell_y1.value = data_y1[cnt]
cell_y2.value = data_y2[cnt]
cell_x = cell_x.offset(1, 0)
cell_y1 = cell_y1.offset(1, 0)
cell_y2 = cell_y2.offset(1, 0)
#Insert graph
chart = xw.Chart()
#Adjustment of graph position and size
chart.left = 200
chart.top = 10
chart.width = 300
chart.height = 200
#Graph type setting
chart.chart_type = 'xy_scatter_lines_no_markers'
#Data range setting
chart.set_source_data(xw.Range('A1:C11'))
#Change x-axis position to bottom(xlminimum=4 ← Please tell me who knows how to read VBA constants)
# api[1]Please tell me who understands the meaning of 1. If you set it to 1 for the time being, it will work ...
chart.api[1].Axes(AxisType.xlValue).Crosses = 4
#Changed the axis scale to inward(xlInside=2)
chart.api[1].Axes(AxisType.xlCategory).MajorTickMark = 2
chart.api[1].Axes(AxisType.xlValue).MajorTickMark = 2
Recommended Posts