Search code examples
pythonplotlinereportlab

Area Line Plot in Python using Report Lab


I am using Reportlab to create some graphs in my PDF reports. I was creating an Area Line Plot and got stuck at a point where I am not able to understand why am I not getting the output I would like to see.

Here is the code I had written for my output:

def standardLinePlot(data, width=200, height=200):

    d = Drawing(width, height)
    lp = AreaLinePlot()
    lp.data=data
    lp.width, lp.height = width, height
    lp.xValueAxis.valueMin = 0
    lp.xValueAxis.valueMax =36
    lp.xValueAxis.valueSteps = [0,6,12,18,24,30,36]

    lp.yValueAxis.valueMin = 0
    lp.yValueAxis.valueMax =100

    lp.strokeColor=colors.black
    lp.fillColor=colors.grey

    lp.reversePlotOrder = False

    lp.joinedLines=1

    d.add(lp)

    return d

The output I am getting is:

Area Line Plot

My intended output is that grey color should be in place of red color which is the area under the line plot. The other problem is how can I add the axis title to this chart. For example, I need “Months” to be my X axis and “% of NAV” to be my Y axis.


Solution

  • To define the color for the lines it seems you need to access... well, the lines :). So, lp.lines[0].strokeColor = colors.grey instead of lp.strokeColor = colors.grey, as that one goes for the plot background color!

    The question about the labels is a bit more tricky, though... ScatterPlot includes functionality to set labels for X and Y axis, but that's not the case for AreaLinePlot. Of course, you could derive a class from AreaLinePlot copying that functionality, if you're going to use it often.