Line charts in PDF file

Interactive Google Colab Practice Notebook

Practice building line charts, line plots with markers, custom line colors, line widths, and axis formatting using ReportLab directly inside Google Colab.

View on GitHub Open In Colab
Adding line chart to PDF file with options to change the width, color and layout by Python ReportLab

Horizontal Line Chart 🔝

from reportlab.graphics.charts.linecharts import HorizontalLineChart

Here we have two sets of data used to generate line graphs:

data = [
    (13, 5, 20, 22, 37, 45, 19, 4),
    (5, 20, 46, 38, 23, 21, 6, 14)
]

Using this data, the graph is rendered below:

Horizontal Line 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 horizontal line 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.linecharts import HorizontalLineChart
from reportlab.graphics import renderPDF

output_dir = Path('output')
output_dir.mkdir(exist_ok=True)
my_path = output_dir / 'line_chart.pdf'

d = Drawing(1000, 500) # width, height
lc = HorizontalLineChart()

data = [
    (13, 5, 20, 22, 37, 45, 19, 4),
    (5, 20, 46, 38, 23, 21, 6, 14)
]
lc.x = 50  # horizontal position
lc.y = 50  # vertical position
lc.height = 400 # height of the chart area
lc.width = 930  # width of the chart area
lc.data = data
lc.lines[0].strokeWidth = 2  # width of line 1
lc.lines[1].strokeWidth = 4  # width of line 2
lc.lines[0].strokeColor = colors.blue # line color
lc.lines[1].strokeColor = colors.red  # line color
lc.valueAxis.valueMin = 0
lc.valueAxis.valueMax = 50
lc.valueAxis.valueStep = 10
lc.categoryAxis.labels.boxAnchor = 'ne'
lc.categoryAxis.labels.dx = 5
lc.categoryAxis.labels.dy = -2
lc.categoryAxis.labels.angle = 30
lc.categoryAxis.categoryNames = ['Jan-22', 'Feb-22', 'Mar-22',
 'Apr-22', 'May-22', 'Jun-22', 'Jul-22', 'Aug-22']
d.add(lc, '')

renderPDF.drawToFile(d, str(my_path), '')
print(f"PDF created: {my_path}")

Background Colour of the Chart 🔝

lc.fillColor = colors.yellow # background color

Colour of Lines 🔝

lc.lines[0].strokeColor = colors.blue # line color
lc.lines[1].strokeColor = colors.red  # line color

Line Plots 🔝

Line plots in PDF file
from pathlib import Path
from reportlab.lib import colors
from reportlab.graphics.shapes import Drawing
from reportlab.graphics.charts.lineplots import LinePlot
from reportlab.graphics.widgets.markers import makeMarker
from reportlab.graphics import renderPDF

output_dir = Path('output')
output_dir.mkdir(exist_ok=True)
my_path = output_dir / 'line_plot.pdf'

d = Drawing(1000, 500) # width, height

data = [
    ((1, 4), (2, 2), (2.5, 1), (3, 3), (4, 5)),
    ((1, 3), (2, 3), (2.5, 2), (3.5, 5), (4, 6))
]
lp = LinePlot()
lp.x = 40
lp.y = 50
lp.height = 445
lp.width = 945
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]
d.add(lp)

renderPDF.drawToFile(d, str(my_path), '')
print(f"PDF created: {my_path}")

Practical Uses of Line Charts & Line Plots in ReportLab

Line charts and line plots are essential for displaying continuous numerical data and trend analysis in PDF reports:

  • Time Series Tracking: Track monthly sales revenue, continuous traffic statistics, or temperature trends over time using HorizontalLineChart.
  • Scientific & Mathematical Plotting: Plot continuous numerical coordinates $(x, y)$ with custom data markers (e.g., FilledCircle, Cross, Square) using LinePlot.
  • Multi-Series Trend Comparison: Compare target metrics against actual performance with distinct line colors and stroke widths.

Common Errors & Troubleshooting

  • Difference Between `HorizontalLineChart` and `LinePlot`: Use HorizontalLineChart for categorical data along the X-axis (e.g., month names, departments). Use LinePlot for numerical continuous values on both X and Y axes.
  • Line Markers Not Displaying: Ensure you import makeMarker from reportlab.graphics.widgets.markers and assign symbols using lp.lines[index].symbol = makeMarker('MarkerName').
  • Axis Label Formatting Issues: Customize numerical label formatting using labelTextFormat='%2.1f' or '%d' to avoid floating-point display glitches on value axes.

Frequently Asked Questions (FAQ)

Q: How do I change the thickness of a specific line in a ReportLab line chart?
A: Set the strokeWidth property on the line index, e.g., lc.lines[0].strokeWidth = 3.

Q: Can I customize line styles (e.g., dashed or dotted lines)?
A: Yes, you can set the strokeDashArray property on individual line objects (e.g., lc.lines[0].strokeDashArray = [2, 2]).

Further Reading & Related Tutorials


Python PDF Shapes in PDF Bar 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