Bar Charts in PDF File

Interactive Google Colab Practice Notebook

Practice building vertical bar charts, horizontal bar charts, individual bar styling, stacked layouts, and axis formatting using ReportLab directly inside Google Colab.

View on GitHub Open In Colab
Vertical and horizontal bar charts with stacked style to add in PDF file by using Python ReportLab

Vertical Bar Chart 🔝

from 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:

vertical bar graph in PDF file

In this format, we can collect sample data from SQLite or MySQL databases and display the graph dynamically.

Category Axis and Value Axis 🔝

Category and value axis with spacing in PDF file

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}")

Background Colour of the Chart 🔝

bc.fillColor = colors.yellow # background color

Colour of Bars 🔝

bc.bars[0].fillColor = colors.yellow # first member of group bar
bc.bars[1].fillColor = colors.lightgreen # second member of group bar

Colour of Individual 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

Horizontal Bar Chart 🔝

Horizontal bar graph in PDF file
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}")

Stacked Chart 🔝

Stacked bar graph in PDF file

Default value is parallel.

bc.categoryAxis.style = 'stacked'

Practical Uses of Bar Charts in ReportLab

Bar charts provide clear visual comparisons across categorical datasets in professional PDF documents:

  • Financial & Sales Comparisons: Contrast multi-quarter revenue figures or product line earnings side-by-side.
  • Inventory & Stock Level Tracking: Display warehouse quantities against predefined threshold limits.
  • Performance Benchmarking: Showcase regional or departmental output metrics using stacked or parallel bar groupings.

Common Errors & Troubleshooting

  • Data Sequence Mismatch: Ensure the length of categoryAxis.categoryNames matches the number of inner data elements per series tuple.
  • Labels Clipping or Overflowing: If category labels overlap or get cut off, adjust categoryAxis.labels.angle, dx, and dy properties or increase canvas margins.
  • IndexError on Individual Bar Styling: When accessing specific bars via bc.bars[(series_index, category_index)], ensure both indices fall within valid bounds of the data structure.

Frequently Asked Questions (FAQ)

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.

Further Reading & Related Tutorials


Python PDF Shapes in PDF Line charts in PDF Pie Charts
PDF Mark sheet by using data from SQLite database



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer