gpt4 book ai didi

Python- Reportlabs - 在 2 个不同的页面中保存 2 个不同的图表?

转载 作者:太空狗 更新时间:2023-10-30 00:11:17 26 4
gpt4 key购买 nike

我使用以下代码在 PDF 中绘制垂直条形图和折线图。

如何将这 2 个图表保存在 PDF 的 2 个不同页面中。我看到可以使用 -

c = canvas.Canvas("hello.pdf")
hello(c)
c.showPage()
c.save()

但是,我没有使用 Canvas,而是使用了 Drawing 对象,其中 showPage() 方法不存在。

如何将 2 个图表保存在 PDF 的 2 个不同页面中?右边的第二个图(折线图)与第一个图(垂直条形图)重叠,从而阻碍了条形图。

这是我的代码。

from reportlab.graphics.shapes import Drawing
from reportlab.graphics.charts.barcharts import VerticalBarChart

drawing = Drawing(400, 200)
data = [
(13, 5, 20, 22, 37, 45, 19, 4),
(14, 6, 21, 23, 38, 46, 20, 5)
]
bc = VerticalBarChart()
bc.x = 50
bc.y = 50
bc.height = 125
bc.width = 300
bc.data = data
#bc.strokeColor = colors.black
bc.valueAxis.valueMin = 0
bc.valueAxis.valueMax = 50
bc.valueAxis.valueStep = 10
bc.categoryAxis.labels.boxAnchor = 'ne'
bc.categoryAxis.labels.dx = 8
bc.categoryAxis.labels.dy = -2
bc.categoryAxis.labels.angle = 30
bc.categoryAxis.categoryNames = ['Jan-99','Feb-99','Mar-99',
'Apr-99','May-99','Jun-99','Jul-99','Aug-99']

drawing.add(bc)
drawing.save()

from reportlab.graphics.charts.lineplots import LinePlot
from reportlab.graphics.widgets.markers import makeMarker

drawing = Drawing(400, 200)
data = [
((1,1), (2,2), (2.5,1), (3,3), (4,5)),
((1,2), (2,3), (2.5,2), (3.5,5), (4,6))
]
lp = LinePlot()
lp.x = 50
lp.y = 50
lp.height = 125
lp.width = 300
lp.data = data
lp.joinedLines = 1
lp.lines[0].symbol = makeMarker('FilledCircle')
lp.lines[1].symbol = makeMarker('Circle')
lp.lineLabelFormat = '%2.0f'
#lp.strokeColor = colors.black
lp.xValueAxis.valueMin = 0
lp.xValueAxis.valueMax = 5
lp.xValueAxis.valueSteps = [1, 2, 2.5, 3, 4, 5]
lp.xValueAxis.labelTextFormat = '%2.1f'
lp.yValueAxis.valueMin = 0
lp.yValueAxis.valueMax = 7
lp.yValueAxis.valueSteps = [1, 2, 3, 5, 6]
drawing.add(lp)
drawing.save()
drawing.save(formats=['pdf'],outDir='.',fnRoot=None)

最佳答案

制作 Canvas 并在其上渲染您的绘图:

from reportlab.pdfgen import canvas
from reportlab.graphics import renderPDF

c = canvas.Canvas('hi.pdf')

# your drawing
# drawing = Drawing(400, 200)

x, y = 0, 0 # coordinates (from left bottom)
renderPDF.draw(drawing, c, x, y, showBoundary=False)

c.showPage() # to end a page and start a new one

# extra pages content

c.save() # to save :D the whole document

希望对您有所帮助:)

关于Python- Reportlabs - 在 2 个不同的页面中保存 2 个不同的图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20343382/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com