使用 Report Lab 在 Python 中绘制面积线图

发布于 2024-12-23 04:30:24 字数 795 浏览 1 评论 0原文

我正在使用 Reportlab 在 PDF 报告中创建一些图表。我正在创建一个面积线图,但陷入了一个我无法理解为什么我没有得到我想要看到的输出的地方。

这是我为输出编写的代码:

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

我得到的输出是:

Area Line Plot

我的预期输出是灰色应该代替红色,即线图下方的区域。另一个问题是如何将轴标题添加到该图表中。例如,我需要“月份”作为 X 轴,“资产净值百分比”作为 Y 轴。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

第七度阳光i 2024-12-30 04:30:24

要定义线条的颜色,您似乎需要访问...好吧,线条:)。因此,lp.lines[0].StrokeColor = Colors.grey 而不是 lp.StrokeColor = Colors.grey,因为它适用于绘图背景颜色!

不过,关于标签的问题有点棘手……ScatterPlot 包含为 X 和 Y 轴设置标签的功能,但 AreaLinePlot 的情况并非如此。当然,如果您要经常使用它,您可以从复制该功能的 AreaLinePlot 派生一个类。

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.

橘虞初梦 2024-12-30 04:30:24

更改

lp = AreaLinePlot()

lp = LinePlot()

并尝试

lp.lines[0].strokeColor = colors.red
lp.lines[0].inFill = True

,但填充颜色将与线条颜色相同。

功劳归@Ricardo Cárdenes

Change

lp = AreaLinePlot()

to

lp = LinePlot()

and try that

lp.lines[0].strokeColor = colors.red
lp.lines[0].inFill = True

but the fill color will be the same as the line color.

credit goes to @Ricardo Cárdenes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文