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)
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.
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 ().
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)
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:
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With