Openpyxl Bar graph Error bars

989 views Asked by At

I was wondering if it is possible to set an error bar series to a reference. What I would like to do is reference cells with the error values already calculated. and then have it displayed on top of the bar graphs.

I found a similar question where they have seemed to do a version of what I am trying, but when I edit their example I get an error. Any advice would be much appreciated. Thank you for your time.

Example of what I want the graph and error bars to look like enter image description here Error:

TypeError: expected class 'openpyxl.chart.error_bar.ErrorBars'

similar Question openpyxl chart error bar styles

My Current Code

            chart1 = BarChart()
            chart1.type = "col"
            chart1.height = 10
            chart1.width = col + 7
            chart1.title = name

            data = Reference(ws, min_col=4, min_row=23, max_col=17)
            cats = Reference(ws, min_col=4, min_row=29, max_col = 17)
            eBars = Reference(ws, min_col=4, min_row=26, max_col=17)

            s= Series(data)

            series = SeriesFactory(data, title="y direction error")
            series.errBars = eBars

            chart1.append(s)
            chart1.append(series)
            chart1.legend = None
            chart1.set_categories(cats)

            chart1.x_axis.tickLblPos = "low"
            #chart1.x_axis.tickLblSkip = 0

            chart1.shape = 10
            ws.add_chart(chart1, "C3")
1

There are 1 answers

2
stovfl On BEST ANSWER

Comment: ... setting a reference to the plus and minus

I see your point, replace numLitwith numRef:

NumDataSource / NumRef

class openpyxl.chart.data_source.NumDataSource(numRef=None, numLit=None)  
    `numLit`  Values must be of type <class ‘openpyxl.chart.data_source.NumData’>
    `numRef` Values must be of type <class ‘openpyxl.chart.data_source.NumRef’>
eBarsNumDataSource = NumDataSource(NumRef(eBars))
series.errBars = ErrorBars(errDir='y', errValType='cust', plus=eBarsNumDataSource, minus=eBarsNumDataSource)

Question: TypeError: expected class 'openpyxl.chart.error_bar.ErrorBars'

Your eBars is of Type Reference but you need Type openpyxl.chart.error_bar.ErrorBars.

class openpyxl.chart.error_bar.ErrorBars

class openpyxl.chart.error_bar.ErrorBars(
    errDir=None, 
    errBarType='both', 
    errValType='fixedVal', 
    noEndCap=None, plus=None, minus=None, val=None, spPr=None, extLst=None)

You need at least the following Parameters:

 ErrorBars(errDir=Value must be one of {‘x’, ‘y’}, 
           plus=Values must be of type <class ‘openpyxl.chart.data_source.NumDataSource’>, 
           minus=Values must be of type <class ‘openpyxl.chart.data_source.NumDataSource’>, 
          )

Follow def list2errorbars(... in the linked similar Question.