Python 如何在数据框中循环并基于单个或多个列创建多个excel图表?
My dataframe包含多个类别和多个子类别。我根据子类别循环我的df,并为每个子类别创建了图表,但我需要按类别循环,并将子类别系列放在图表中。 我的df的ex:Python 如何在数据框中循环并基于单个或多个列创建多个excel图表?,python,loops,charts,Python,Loops,Charts,My dataframe包含多个类别和多个子类别。我根据子类别循环我的df,并为每个子类别创建了图表,但我需要按类别循环,并将子类别系列放在图表中。 我的df的ex: Date Purchased Fruit_orange Fruit_apple Vegetables Meats_chicken Meats_beef 5/11/2015 23 67 4 5/18/2
Date Purchased Fruit_orange Fruit_apple Vegetables Meats_chicken Meats_beef
5/11/2015 23 67 4
5/18/2015 87 53 2 9 10
5/25/2015 96
6/1/2015 45 34
6/8/2015 88 11 5 3
6/15/2015 12 2
我如何在df中循环并在图表中以系列的形式绘制包含橙子和苹果的水果类别,然后继续循环并让其将蔬菜识别为单个列以绘制图表,然后再次在图表中绘制包含鸡肉/牛肉的肉类
我的代码循环遍历每一列,分别为每一列创建一个图表
for c in range(3,maxcol+1):
ChartTitle = sheet.cell(row=1, column=c).value
LeftLegend = sheet.cell(row=3, column=2).value
RightLegend = sheet.cell(row=3, column=c).value
LeftAxis = sheet.cell(row=3, column=2).value
#RightAxis = sheet.cell(row=3, column=c).value
#date range
refObj1 = openpyxl.chart.Reference(sheet, min_col=1, min_row=4, max_col=1, max_row=maxrow)
#trend kpi column to repeat across charts
refObj2 = openpyxl.chart.Reference(sheet, min_col=2, min_row=4, max_col=2, max_row=maxrow)
seriesObj2 = openpyxl.chart.Series(refObj2, title=LeftLegend)
#loop thru 2dary data
refObj3 = openpyxl.chart.Reference(sheet, min_col=c, min_row=4, max_col=c, max_row=maxrow)
seriesObj3 = openpyxl.chart.Series(refObj3, title=RightLegend)
#create chart and add data, titles
chartObj = openpyxl.chart.LineChart()
chartObj.title = ChartTitle
chartObj.append(seriesObj2)
chartObj.append(seriesObj3)
chartObj.set_categories(refObj1)
chartObj.x_axis.title = "Date"
chartObj.y_axis.title = LeftAxis
#chartObj.y_axis.title = RightAxis
sheet.add_chart(chartObj, 'c5')
你是说excel图表吗?例如,excel文件中的图表?是的,我做了!我很抱歉没有指定您应该发布当前代码——有几种不同的方法可以从python编写excel文件,任何答案都需要知道您使用的是什么方法。