Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python- Reportlabs - save 2 different graphs in 2 different pages?

I've the following code where I'm drawing a vertical bar graph and a line graph as well inside a PDF.

How do I save these 2 graphs in 2 different pages of the PDF. I saw that it can be done using -

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

But, instead of using Canvas, I'm using Drawing object in which showPage() method does not exist.

How do I save the 2 graphs in 2 different pages of the PDF? Right the second graph(line chart) overlaps the first graph (vertical bar chart), thereby hindering the bar chart.

Here is my code.

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) 
like image 579
user2935897 Avatar asked Dec 03 '13 05:12

user2935897


People also ask

How to plot multiple separate graphs for same data from one Python?

Plot multiple separate graphs for same data from one Python script Last Updated : 07 Jul, 2021 It is very easy to plot different kinds of maps and plots of different data using Python. One such useful library in Python is Matplotlib which is very useful for creating different types of plots using the same data.

How do I plot multiple graphs in Seaborn?

In this post, I share 4 simple but practical tips for plotting multiple graphs. Let’s import packages and update default settings for charts to add a little bit of personal style to the charts. We will use Seaborn’s built-in dataset on tips: One easy way to plot multiple subplots is to use plt.subplots ().

How to plot multiple plots in Matplotlib?

Multiple Plots using subplot () Function. A subplot () function is a wrapper function which allows the programmer to plot more than one graph in a single figure by just calling it once. Syntax: matplotlib.pyplot.subplots (nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)

How to plot different kinds of maps and plots in Python?

It is very easy to plot different kinds of maps and plots of different data using Python. One such useful library in Python is Matplotlib which is very useful for creating different types of plots using the same data. The easiest way to install Matplotlib is by using the pip command in the command-line as shown below:


1 Answers

Make a canvas and render your drawings on it:

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

Hope it helps :)

like image 102
McAbra Avatar answered Sep 18 '22 11:09

McAbra