Practice building line charts, line plots with markers, custom line colors, line widths, and axis formatting using ReportLab directly inside Google Colab.
View on GitHubfrom 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:
In this format, we can collect sample data from SQLite or MySQL databases and display the graph dynamically.
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}")
lc.fillColor = colors.yellow # background color
lc.lines[0].strokeColor = colors.blue # line color
lc.lines[1].strokeColor = colors.red # line color
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}")
Line charts and line plots are essential for displaying continuous numerical data and trend analysis in PDF reports:
HorizontalLineChart.FilledCircle, Cross, Square) using LinePlot.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.makeMarker from reportlab.graphics.widgets.markers and assign symbols using lp.lines[index].symbol = makeMarker('MarkerName').labelTextFormat='%2.1f' or '%d' to avoid floating-point display glitches on value axes.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]).
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.