如何在3列中绘制数据,并在轴上绘制两列数据,第三列是openpyxl中的图例

时间:2019-12-06 10:17:32

标签: python-3.x openpyxl

在这里,我正在尝试绘制图表b / w“以美元计算的销售额”和“市场份额”,并以“产品数量”作为图例。但是,单击绘制的点时,在Excel工作表中未选择产品数列中的值。所有三列都必须通过Excel中的绘制点可识别。

import openpyxl 


from openpyxl.chart import ScatterChart, Reference, Series , LineChart


wb = openpyxl.Workbook() 

# Get workbook active sheet  
# from the active attribute. 
sheet = wb.active 

rows = [ 
    ("Number of Products", "Sales in USD", "Market share"), 
    (14, 12200, 15), 
    (20, 60000, 33), 
    (18, 24400, 10), 
    (22, 32000, 42), 
] 



# write content of each row in 1st, 2nd and 3rd 
# column of the active sheet respectively. 
for row in rows: 
    sheet.append(row) 


chart = ScatterChart() 


# create data for plotting 
xvalues = Reference(sheet, min_col = 3, 
                    min_row = 2, max_row = 2) 

yvalues = Reference(sheet, min_col = 2, 
                    min_row = 2, max_row = 2) 

size = Reference(sheet, min_col = 1, 
                min_row = 2, max_row = 2) 

# create a 1st series of data 
series = Series(values = yvalues, xvalues = xvalues, 
                      zvalues = size,title=sheet.cell(row=2,column=1).value) 
series.marker=openpyxl.chart.marker.Marker('circle')
series.graphicalProperties.line.noFill=True 
chart.series.append(series)

# add series data to the chart object 

chart.series.append(series) 

# set the title of the chart 
chart.title = " BUBBLE-CHART "

# set the title of the x-axis 
chart.x_axis.title = " X_AXIS "

# set the title of the y-axis 
chart.y_axis.title = " Y_AXIS "

# add chart to the sheet 
# the top-left corner of a chart 
# is anchored to cell E2 . 
sheet.add_chart(chart, "E2") 

# save the file 
wb.save("scatter`enter code here`Chart.xlsx") 

0 个答案:

没有答案