使用 Report Lab 在 Python 中绘制面积线图
我正在使用 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
我得到的输出是:
我的预期输出是灰色应该代替红色,即线图下方的区域。另一个问题是如何将轴标题添加到该图表中。例如,我需要“月份”作为 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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要定义线条的颜色,您似乎需要访问...好吧,线条:)。因此,
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 oflp.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 forAreaLinePlot
. Of course, you could derive a class fromAreaLinePlot
copying that functionality, if you're going to use it often.更改
为
并尝试
,但填充颜色将与线条颜色相同。
功劳归@Ricardo Cárdenes
Change
to
and try that
but the fill color will be the same as the line color.
credit goes to @Ricardo Cárdenes