API Reference: ecg_sdk.visualization.plotter¶
ecg_sdk.visualization.plotter
¶
Clinical ECG Plotting Module¶
Renders single-lead, multi-lead, and standard 12-lead electrocardiograms with authentic clinical millimeter paper grid formatting, precise voltage/time calibration, fiducial wave annotations, adaptive anti-collision layout, configurable sparse tick labeling, and physical 1:1 "paper" mode vs responsive "digital" display modes.
Standard Clinical Specifications (IEC 60601-2-25 / AHA / ACC / ESC): - Paper Speed: 25 mm/s (1 small square [1 mm] = 0.04 s; 1 large block [5 mm] = 0.20 s) - Voltage Sensitivity / Gain: 10 mm/mV (1 small square [1 mm] = 0.10 mV; 1 large block [5 mm] = 0.50 mV) - Calibration Pulse: Standard 1.0 mV square pulse with 0.20 s duration (5 mm x 10 mm) - Physical Print Sizes: * 12-Lead Diagnostic Page: US Letter (11" x 8.5") or ISO A4 (297 mm x 210 mm) * Ambulatory / Holter Thermal Strip: 50 mm (approx 2.0") height per channel
GRID_STYLES = {'clinical_pink': {'bg_color': '#fff5f5', 'minor_color': '#fed7d7', 'major_color': '#feb2b2', 'minor_alpha': 0.65, 'major_alpha': 0.95, 'minor_lw': 0.45, 'major_lw': 0.85, 'signal_color': '#0a0a0a', 'signal_lw': 1.25, 'text_color': '#1a202c', 'border_color': '#e53e3e'}, 'clinical_red': {'bg_color': '#fef2f2', 'minor_color': '#fecaca', 'major_color': '#f87171', 'minor_alpha': 0.65, 'major_alpha': 0.95, 'minor_lw': 0.45, 'major_lw': 0.85, 'signal_color': '#000000', 'signal_lw': 1.25, 'text_color': '#111827', 'border_color': '#b91c1c'}, 'clinical_gray': {'bg_color': '#ffffff', 'minor_color': '#e2e8f0', 'major_color': '#94a3b8', 'minor_alpha': 0.6, 'major_alpha': 0.9, 'minor_lw': 0.45, 'major_lw': 0.85, 'signal_color': '#0f172a', 'signal_lw': 1.25, 'text_color': '#0f172a', 'border_color': '#64748b'}, 'dark_telemetry': {'bg_color': '#0b1120', 'minor_color': '#1e293b', 'major_color': '#334155', 'minor_alpha': 0.5, 'major_alpha': 0.8, 'minor_lw': 0.45, 'major_lw': 0.85, 'signal_color': '#10b981', 'signal_lw': 1.35, 'text_color': '#f8fafc', 'border_color': '#475569'}}
module-attribute
¶
plot_clinical_ecg(signal, lead=0, start_time=0.0, duration=10.0, display_mode='paper', speed_mm_s=25.0, gain_mm_mv=10.0, grid_style='clinical_pink', voltage_range=None, ylim=None, plot_height=None, height_per_lead=None, figsize=None, r_peaks=None, waves=None, annotations=None, time_tick_step=None, voltage_tick_step=None, sparse_ticks=True, show_calibration_pulse=True, show_header=True, title=None, save_path=None)
¶
Plot a single or multi-lead ECG strip on standard clinical ECG paper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signal
|
ECGSignal
|
ECGSignal instance. |
required |
lead
|
Optional[Union[str, int]]
|
Index or string name of lead to plot. If None, plots all leads stacked. |
0
|
start_time
|
float
|
Start time in seconds. |
0.0
|
duration
|
Optional[float]
|
Duration of window to plot in seconds (defaults to 10s or total duration). |
10.0
|
display_mode
|
str
|
'paper' (exact 1:1 physical millimeter scale matching thermal paper rolls) or 'digital' (expanded responsive layout optimized for monitors and slides). |
'paper'
|
speed_mm_s
|
float
|
Standard paper speed (default: 25.0 mm/s). |
25.0
|
gain_mm_mv
|
float
|
Standard voltage gain (default: 10.0 mm/mV). |
10.0
|
grid_style
|
str
|
Color palette: 'clinical_pink', 'clinical_red', 'clinical_gray', 'dark_telemetry'. |
'clinical_pink'
|
voltage_range
|
Optional[Tuple[float, float]]
|
Optional (min_mv, max_mv) tuple to fix vertical voltage scale. |
None
|
ylim
|
Optional[Tuple[float, float]]
|
Alias for voltage_range. |
None
|
plot_height
|
Optional[float]
|
Total figure height in inches (overrides display_mode defaults). |
None
|
height_per_lead
|
Optional[float]
|
Height in inches per stacked lead track. |
None
|
figsize
|
Optional[Tuple[float, float]]
|
Optional (width, height) in inches. |
None
|
r_peaks
|
Optional[ndarray]
|
1D array of sample indices for detected R-peaks within the window. |
None
|
waves
|
Optional[Dict[str, ndarray]]
|
Dictionary of fiducial wave sample indices, e.g. {'P': p_idx, 'QRS_onset': qon_idx, 'T': t_idx}. |
None
|
annotations
|
Optional[List[Dict[str, Any]]]
|
List of dicts specifying text annotations at given timestamps. |
None
|
time_tick_step
|
Optional[float]
|
Step interval in seconds for X-axis numeric labels (e.g. 1.0s). |
None
|
voltage_tick_step
|
Optional[float]
|
Step interval in mV for Y-axis numeric labels (e.g. 0.5mV or 1.0mV). |
None
|
sparse_ticks
|
bool
|
If True, renders uncluttered labels at clean intervals while keeping full grid. |
True
|
show_calibration_pulse
|
bool
|
Whether to prepend the standard 1 mV reference pulse. |
True
|
show_header
|
bool
|
Whether to render the clinical medical header banner on top. |
True
|
title
|
Optional[str]
|
Optional custom title for the strip. |
None
|
save_path
|
Optional[str]
|
Optional file path to export high-resolution image (PNG, PDF, SVG). |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[Figure, Any]
|
(fig, ax) or (fig, axes) Matplotlib objects. |
Source code in ecg_sdk/visualization/plotter.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 | |
plot_12lead_clinical(signal, duration=2.5, display_mode='paper', paper_size='letter', speed_mm_s=25.0, gain_mm_mv=10.0, grid_style='clinical_pink', voltage_range=(-1.5, 2.0), ylim=None, plot_height=None, figsize=None, time_tick_step=1.0, voltage_tick_step=1.0, sparse_ticks=True, rhythm_lead='II', show_header=True, save_path=None)
¶
Plot standard clinical 12-lead ECG format (3 rows x 4 columns + 1 continuous rhythm strip at bottom).
Layout: - Col 1: I, II, III - Col 2: aVR, aVL, aVF - Col 3: V1, V2, V3 - Col 4: V4, V5, V6 - Bottom row: Continuous rhythm strip (default Lead II).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signal
|
ECGSignal
|
ECGSignal instance containing 12 leads. |
required |
duration
|
float
|
Duration of 3x4 lead blocks in seconds (default: 2.5s). |
2.5
|
display_mode
|
str
|
'paper' (1:1 physical sheet: US Letter 11"x8.5" or A4 11.69"x8.27") or 'digital' (expanded 18"x10" landscape for monitors/slides). |
'paper'
|
paper_size
|
str
|
'letter' (11.0" x 8.5") or 'a4' (11.69" x 8.27"). |
'letter'
|
speed_mm_s
|
float
|
Standard paper speed (25.0 mm/s). |
25.0
|
gain_mm_mv
|
float
|
Voltage gain (10.0 mm/mV). |
10.0
|
grid_style
|
str
|
'clinical_pink', 'clinical_red', 'clinical_gray', 'dark_telemetry'. |
'clinical_pink'
|
voltage_range
|
Optional[Tuple[float, float]]
|
Vertical limits in mV (default (-1.5, 2.0)). |
(-1.5, 2.0)
|
ylim
|
Optional[Tuple[float, float]]
|
Alias for voltage_range. |
None
|
plot_height
|
Optional[float]
|
Total figure height in inches. |
None
|
figsize
|
Optional[Tuple[float, float]]
|
Optional (width, height) in inches. |
None
|
time_tick_step
|
Optional[float]
|
Step in seconds for sparse time labels. |
1.0
|
voltage_tick_step
|
Optional[float]
|
Step in mV for sparse voltage labels. |
1.0
|
sparse_ticks
|
bool
|
If True, renders uncluttered labels at clean intervals. |
True
|
rhythm_lead
|
str
|
Lead name to use for full-width bottom rhythm strip. |
'II'
|
show_header
|
bool
|
Whether to display medical header banner. |
True
|
save_path
|
Optional[str]
|
Optional output image path. |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[Figure, ndarray]
|
(fig, axes_array) |
Source code in ecg_sdk/visualization/plotter.py
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 | |
draw_clinical_grid(ax, t_min, t_max, v_min, v_max, style_dict, time_tick_step=1.0, voltage_tick_step=0.5, sparse_ticks=True)
¶
Draw authentic clinical ECG millimeter grid lines onto a Matplotlib Axes.
Grid Specification: - Time axis (X): Minor grid = 0.04 s (1 mm at 25 mm/s) Major grid = 0.20 s (5 mm at 25 mm/s = 5 small squares) - Voltage axis (Y): Minor grid = 0.10 mV (1 mm at 10 mm/mV) Major grid = 0.50 mV (5 mm at 10 mm/mV = 5 small squares)
Source code in ecg_sdk/visualization/plotter.py
generate_calibration_pulse(sampling_rate, start_time=0.0, pulse_width=0.2, pulse_height=1.0)
¶
Generate standard 1 mV calibration pulse (0.2s duration = 5mm width x 10mm height).