Practice building vertical bar charts, horizontal bar charts, individual bar styling, stacked layouts, and axis formatting using ReportLab directly inside Google Colab.
View on GitHubfrom reportlab.graphics.charts.barcharts import VerticalBarChart
Here we have two sets of data used to generate bar graphs:
data = [
(13, 15, 20, 22, 37, 42, 19, 4),
(14, 16, 21, 23, 38, 41, 20, 5)
]
Using this data, the graph is rendered below:
In this format, we can collect sample data from SQLite or MySQL databases and display the graph dynamically.
Full code for vertical bar charts is shown below. Different options are explained using comments:
from pathlib import Path
from reportlab.lib import colors
from reportlab.graphics.shapes import Drawing
from reportlab.graphics.charts.barcharts import VerticalBarChart
from reportlab.graphics import renderPDF
output_dir = Path('output')
output_dir.mkdir(exist_ok=True)
my_path = output_dir / 'vertical_bar_chart.pdf'
d = Drawing(600, 400) # width, height
bc = VerticalBarChart()
data = [
(13, 15, 20, 22, 37, 42, 19, 4),
(14, 16, 21, 23, 38, 41, 20, 5)
]
bc.x = 50 # horizontal position
bc.y = 50 # vertical position
bc.height = 340 # height of the chart area
bc.width = 530 # width of the chart area
bc.data = data
bc.strokeColor = colors.black # bar edges color
bc.bars[0].fillColor = colors.yellow # first member of group bar
bc.bars[1].fillColor = colors.lightgreen # second member of group
bc.groupSpacing = 10 # gap between groups
bc.barSpacing = 2.5 # gap within groups (among members)
bc.valueAxis.valueMin = 0 # value Axis minimum
bc.valueAxis.valueMax = 50
bc.valueAxis.valueStep = 10
bc.categoryAxis.labels.boxAnchor = 'ne'
bc.categoryAxis.labels.dx = 5
bc.categoryAxis.labels.dy = -2
bc.categoryAxis.labels.angle = 30
# bc.categoryAxis.style = 'stacked' # stacked bar graph, parallel
bc.categoryAxis.categoryNames = ['Jan-22', 'Feb-22', 'Mar-22',
'Apr-22', 'May-22', 'Jun-22', 'Jul-22', 'Aug-22']
d.add(bc, '')
renderPDF.drawToFile(d, str(my_path), '')
print(f"PDF created: {my_path}")
bc.fillColor = colors.yellow # background color
bc.bars[0].fillColor = colors.yellow # first member of group bar
bc.bars[1].fillColor = colors.lightgreen # second member of group bar
bc.bars[(0, 0)].fillColor = colors.blue # left bar of first group
bc.bars[(0, 1)].fillColor = colors.yellow # left bar of second group
bc.bars[(1, 2)].fillColor = colors.red # right bar of 3rd group
from pathlib import Path
from reportlab.lib import colors
from reportlab.graphics.shapes import Drawing
from reportlab.graphics.charts.barcharts import HorizontalBarChart
from reportlab.graphics import renderPDF
output_dir = Path('output')
output_dir.mkdir(exist_ok=True)
my_path = output_dir / 'horizontal_bar_chart.pdf'
d = Drawing(620, 400) # width, height
bc = HorizontalBarChart()
data = [
(13, 15, 20, 22, 37, 42, 19, 4),
(14, 16, 21, 23, 38, 41, 20, 5)
]
bc.x = 50 # horizontal position
bc.y = 50 # vertical position
bc.height = 345 # height of the chart area
bc.width = 545 # width of the chart area
bc.data = data
bc.strokeColor = colors.black
bc.bars[0].fillColor = colors.yellow
bc.bars[1].fillColor = colors.lightgreen
bc.bars[(0, 5)].fillColor = colors.red
bc.groupSpacing = 8
bc.barSpacing = 2
bc.valueAxis.valueMin = 0
bc.valueAxis.valueMax = 50
bc.valueAxis.valueStep = 5
bc.categoryAxis.labels.boxAnchor = 'ne'
bc.categoryAxis.labels.dx = -10
bc.categoryAxis.labels.dy = 5
bc.categoryAxis.labels.angle = 30
bc.categoryAxis.categoryNames = ['Jan-22', 'Feb-22', 'Mar-22',
'Apr-22', 'May-22', 'Jun-22', 'Jul-22', 'Aug-22']
d.add(bc, '')
renderPDF.drawToFile(d, str(my_path), '')
print(f"PDF created: {my_path}")
Default value is parallel.
bc.categoryAxis.style = 'stacked'
Bar charts provide clear visual comparisons across categorical datasets in professional PDF documents:
categoryAxis.categoryNames matches the number of inner data elements per series tuple.categoryAxis.labels.angle, dx, and dy properties or increase canvas margins.bc.bars[(series_index, category_index)], ensure both indices fall within valid bounds of the data structure.Q: How do I switch from vertical to horizontal bars?
A: Import HorizontalBarChart from reportlab.graphics.charts.barcharts instead of VerticalBarChart.
Q: Can I display data labels directly on top of the bars?
A: Yes, ReportLab bar charts support custom label formatters via the `barLabels` property on the chart object.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.