Plotting¶
survey_kit.plot builds interactive plotly figures directly from a StatCalculator's or MultipleImputation's own df_estimates/CI columns - no separate reshaping step. Every figure carries a hand-rolled dropdown (not plotly's native updatemenus) for switching which group of series is shown, with state preserved across switches and shared across figures on the same page - see Plotting for a walkthrough.
Core Plots¶
line ¶
line(
stat_item,
x_columns: list[str],
x_values: dict[str, float] | None = None,
series: str | None = None,
ci_level: float | None = None,
ci_area: bool = False,
filter_expr: Expr | None = None,
rename: dict[str, str] | None = None,
order: list[str] | None = None,
round_output: bool = True,
group_by: str | None = None,
group_first: bool = True,
group_label: str = "Show:",
x_axis_title: str = "",
y_axis_title: str = "",
x_axis_range: list[float] | None = None,
y_axis_range: list[float] | None = None,
x_tick_frequency: float | None = None,
colors: list[str] | None = None,
use_dashes: bool = False,
color_repeat_frequency: int = 0,
color_n_before_change: int = 0,
layout: dict | None = None,
fig: "plotly.graph_objects.Figure | None" = None,
) -> "plotly.graph_objects.Figure"
One line per series (a StatCalculator/MultipleImputation index value, e.g. "Variable" or a by-group) across a set of columns of stat_item.df_estimates treated as the x-axis - e.g. quantile columns (see quantiles(), a thin wrapper around this), or any other set of numeric-valued columns you want plotted against each other (e.g. one column per year).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stat_item
|
StatCalculator | MultipleImputation
|
|
required |
x_columns
|
list[str]
|
Columns of stat_item.df_estimates to plot, one point per column per series. |
required |
x_values
|
dict[str, float] | None
|
{column: x position}. If None, inferred from each column's own name (see numeric_x_values()) when every column in x_columns is itself numeric - either literally (e.g. "2016", "2017", "2018" - a wide-by-year table plots on a real year axis instead of 0,1,2) or as a quantile column ("q10"/"q0_1" -> 10, the percentile); else each column's 0-indexed position in x_columns. |
None
|
series
|
str | None
|
Which stat_index_columns(stat_item) column identifies one line. If None and there's only one index column, that one is used; if there's more than one, they're joined into a single label (series_label) and each unique combination is one line. |
None
|
ci_level
|
float | None
|
If given, also draw confidence intervals at this level (e.g. 0.95) via stat_item._df_ci(ci_level). |
None
|
ci_area
|
bool
|
Draw the CI as a shaded band around the line instead of per-point error bars. Only used when ci_level is given. |
False
|
filter_expr
|
Expr | None
|
Applied to stat_item.df_estimates (and _df_ci, if used) before plotting. |
None
|
rename
|
dict[str, str] | None
|
{old series label: new series label} - renames the legend/dropdown entries; unmatched labels pass through unchanged. |
None
|
order
|
list[str] | None
|
Explicit series order (also filters to only these series) - by
the renamed label if |
None
|
round_output
|
bool
|
Round values per Census DRB disclosure rules before plotting. Default True. |
True
|
group_by
|
str | None
|
Every figure's legend is replaced with a compact dropdown (see add_group_dropdown); this controls how series are grouped into dropdown entries. None (default) puts every series in one group, so the dropdown has a single entry and all series show at once - visually the same as a plain legend. Given a separator (e.g. ":"), each series label is split on it once - the prefix and suffix, one of which becomes the dropdown group (see group_first) and the other the series' name within that group - so related series (e.g. "allhh:Survey" and "allhh:NEWS") land in the same group and are shown together. A label without the separator falls back to its own group. |
None
|
group_first
|
bool
|
When group_by splits a label, use the part before the separator as the group name (True, default) or the part after it (False). |
True
|
group_label
|
str
|
Text shown next to the group dropdown (see add_group_dropdown). Default "Show:". |
'Show:'
|
x_axis_title
|
str
|
|
''
|
y_axis_title
|
str
|
|
''
|
x_axis_range
|
list[float] | None
|
|
None
|
y_axis_range
|
list[float] | None
|
|
None
|
x_tick_frequency
|
float | None
|
Spacing between x-axis ticks. |
None
|
colors
|
list[str] | None
|
One color per series, cycled if shorter. Defaults to plotly's qualitative palettes. |
None
|
use_dashes
|
bool
|
Cycle line dash styles across series in addition to color. |
False
|
color_repeat_frequency
|
int
|
Cycle colors (and dashes, if use_dashes) through only the first
|
0
|
color_n_before_change
|
int
|
Hold each color (and dash) for this many consecutive series before advancing to the next one, instead of advancing every series - e.g. so several consecutive related series share one color as a block. Takes priority over color_repeat_frequency if both are given; 0 (default) advances every series. |
0
|
layout
|
dict | None
|
Extra plotly layout properties, applied last via fig.update_layout(**layout) - so this always overrides any survey_kit default (e.g. {"title": "...", "font": {"size": 16}}). The returned figure is a plain plotly Figure regardless, so fig.update_layout(...)/fig.update_xaxes(...)/etc. also work fine called yourself afterward - this is only a same-call convenience. |
None
|
fig
|
Figure | None
|
Add these lines to an existing figure instead of a new one. |
None
|
Returns:
| Type | Description |
|---|---|
Figure
|
|
Source code in src/survey_kit/plot/core.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 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 | |
quantiles ¶
quantiles(
stat_item,
quantile_list: list[float] | None = None,
x_axis_title: str = "Percentile",
x_axis_range: list[float] | None = None,
**kwargs,
) -> "plotly.graph_objects.Figure"
line(), specialized to a StatCalculator/MultipleImputation's own quantile-stat columns (see quantile_columns()) - one line per series across percentile 0-100 on the x-axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stat_item
|
StatCalculator | MultipleImputation
|
|
required |
quantile_list
|
list[float] | None
|
Which quantiles to plot (as fractions 0-1 or percentiles 0-100 - either is accepted). None (default) plots every quantile column present. |
None
|
x_axis_title
|
str
|
Default "Percentile". |
'Percentile'
|
x_axis_range
|
list[float] | None
|
Default [0, 100]. |
None
|
**kwargs
|
Passed through to line() (series, ci_level, ci_area, filter_expr, rename, order, group_by, group_first, group_label, colors, ...). |
{}
|
Returns:
| Type | Description |
|---|---|
Figure
|
|
Source code in src/survey_kit/plot/core.py
coefplot ¶
coefplot(
stat_item,
column: str,
ci_level: float | None = None,
category: str | None = None,
series: str | None = None,
filter_expr: Expr | None = None,
rename: dict[str, str] | None = None,
order: list[str] | None = None,
headers: dict[str, str] | None = None,
round_output: bool = True,
item_spacing: float = 1.0,
offset_shift: float = 0.1,
x_axis_title: str = "",
y_axis_title: str = "",
x_axis_range: list[float] | None = None,
colors: list[str] | None = None,
height: int | None = None,
width: int = 1000,
layout: dict | None = None,
fig: "plotly.graph_objects.Figure | None" = None,
) -> "plotly.graph_objects.Figure"
Stata coefplot-style horizontal dot-and-whisker chart: one row per category (e.g. one per demographic subgroup), one point per row per series (e.g. one color per year, offset vertically so they don't overlap) - matches the classic disclosure-review chart of "estimate +/- CI, one row per subgroup, one color per year/source".
Has no group dropdown of its own (there's nothing to switch between - every series is always shown), but single/double-click toggle/isolate on the legend is still shared by trace name with any other line()/quantiles()/coefplot()/stacked_bar() figure on the same page (see add_group_dropdown) - isolating "2018" here and switching to a sibling figure inside combine() that also has a "2018" series shows it isolated there too.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stat_item
|
StatCalculator | MultipleImputation
|
|
required |
column
|
str
|
Which column of stat_item.df_estimates to plot (e.g. "poverty", or a compare()d "ratio"/"difference" column). |
required |
ci_level
|
float | None
|
If given, draw horizontal error bars at this level (e.g. 0.95) via stat_item._df_ci(ci_level). |
None
|
category
|
str | None
|
Which stat_index_columns(stat_item) column is the row/category
axis. Defaults to the one index column that isn't |
None
|
series
|
str | None
|
Which index column offsets multiple points per category row (e.g. "year") - each unique value becomes one color/trace. None (default) plots a single, unoffset point per category. |
None
|
filter_expr
|
Expr | None
|
Applied to stat_item.df_estimates (and _df_ci, if used) before plotting. |
None
|
rename
|
dict[str, str] | None
|
{old category label: new category label}. |
None
|
order
|
list[str] | None
|
Explicit category order, top-to-bottom - by the renamed
label if |
None
|
headers
|
dict[str, str] | None
|
{category label: header text} - inserts a bold divider row
with that text directly above the given category's row. Keyed
the same way as |
None
|
round_output
|
bool
|
Round values per Census DRB disclosure rules before plotting. Default True. |
True
|
item_spacing
|
float
|
Vertical spacing between category rows. Default 1.0. |
1.0
|
offset_shift
|
float
|
Vertical offset between each series' points within a category
row (only matters when |
0.1
|
x_axis_title
|
str
|
|
''
|
y_axis_title
|
str
|
|
''
|
x_axis_range
|
list[float] | None
|
|
None
|
colors
|
list[str] | None
|
One color per series (or a single color, if |
None
|
height
|
int | None
|
Defaults to scaling with the number of category rows. |
None
|
width
|
int
|
Default 1000. |
1000
|
layout
|
dict | None
|
Extra plotly layout properties, applied last via fig.update_layout(**layout) - overrides any survey_kit default. |
None
|
fig
|
Figure | None
|
Add these points to an existing figure instead of a new one. |
None
|
Returns:
| Type | Description |
|---|---|
Figure
|
|
Source code in src/survey_kit/plot/core.py
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 | |
stacked_bar ¶
stacked_bar(
items: dict[str, object],
column: str,
category: str | None = None,
total_key: str | None = None,
filter_expr: Expr | None = None,
rename: dict[str, str] | None = None,
order: list[str] | None = None,
round_output: bool = True,
horizontal: bool = True,
label_scale: float = 1.0,
label_round_digits: int | None = None,
value_axis_range: list[float] | None = None,
x_axis_title: str = "",
y_axis_title: str = "",
colors: list[str] | None = None,
height: int | None = None,
width: int = 1000,
layout: dict | None = None,
fig: "plotly.graph_objects.Figure | None" = None,
) -> "plotly.graph_objects.Figure"
Stacked bar decomposition: {name: StatCalculator|MultipleImputation} where every item shares the same category axis (e.g. "which safety-net program was removed") - each dict entry becomes one layer of the stack (e.g. one age group's contribution), stacked per category. A shared blue gradient (lightest first) colors the layers in dict order, since these are typically ordered meaningfully (e.g. smallest to largest group). Layers, and categories, may mix positive and negative values freely - each bar stacks its positive layers to the right of zero and its negative ones to the left, and a total_key label lands on whichever side its own net value falls on.
Has no group dropdown of its own, but single/double-click toggle/ isolate on the legend is shared by trace (layer) name with any other line()/quantiles()/coefplot()/stacked_bar() figure on the same page (see add_group_dropdown) - isolating "Under 18" here and switching to a sibling figure inside combine() that also has an "Under 18" layer shows it isolated there too.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
dict[str, StatCalculator | MultipleImputation]
|
One item per stack layer, in the order they should stack. |
required |
column
|
str
|
Which column of each item's df_estimates to plot. |
required |
category
|
str | None
|
Which stat_index_columns(item) column is the category axis (the bars/rows). Defaults to the one index column, if every item has exactly one. |
None
|
total_key
|
str | None
|
A key in |
None
|
filter_expr
|
Expr | None
|
Applied to every item's df_estimates before plotting. |
None
|
rename
|
dict[str, str] | None
|
{old category label: new category label} - applied uniformly to every item. |
None
|
order
|
list[str] | None
|
Explicit category order - by the renamed label if |
None
|
round_output
|
bool
|
Round values per Census DRB disclosure rules before plotting. Default True. |
True
|
horizontal
|
bool
|
Horizontal (default) or vertical bars. |
True
|
label_scale
|
float
|
Multiplier applied to |
1.0
|
label_round_digits
|
int | None
|
Round the displayed total label to this many digits. |
None
|
value_axis_range
|
list[float] | None
|
Range of the value axis (x if horizontal, else y). |
None
|
x_axis_title
|
str
|
Titles for the physical x/y axis, whichever one that ends up being (the value axis when horizontal, the category axis when not). |
''
|
y_axis_title
|
str
|
Titles for the physical x/y axis, whichever one that ends up being (the value axis when horizontal, the category axis when not). |
''
|
colors
|
list[str] | None
|
One color per stacked layer (excluding total_key), in dict order. Defaults to a light-to-dark blue gradient. |
None
|
height
|
int | None
|
height defaults to scaling with the number of categories; width defaults to 1000. |
None
|
width
|
int | None
|
height defaults to scaling with the number of categories; width defaults to 1000. |
None
|
layout
|
dict | None
|
Extra plotly layout properties, applied last via fig.update_layout(**layout) - overrides any survey_kit default. |
None
|
fig
|
Figure | None
|
Add these bars to an existing figure instead of a new one. |
None
|
Returns:
| Type | Description |
|---|---|
Figure
|
|
Source code in src/survey_kit/plot/core.py
594 595 596 597 598 599 600 601 602 603 604 605 606 607 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 818 819 820 821 822 823 824 825 826 827 828 829 | |
Combining Figures¶
combine ¶
combine(
tree: dict[str, object],
label: str | list[str | None] = "View:",
width: int | None = None,
height: int | None = 700,
layout: dict | None = None,
dropdowns_padding_left: int = DEFAULT_LEFT_MARGIN_PX,
) -> "CombinedFigure"
Combine several figures (from line()/quantiles()/coefplot()/ stacked_bar(), or any plotly Figure) into one HTML page, switched between by one cascading dropdown per nesting level - arbitrarily deep, e.g. {"Income": {"2016": fig_a, "2017": fig_b}, "Poverty": {"2016": fig_c, "2017": fig_d}} gets two dropdowns (top: Income/Poverty, second: year); picking a year under one top choice keeps that same year selected if you then switch the top dropdown to the other branch (falling back to that branch's first option if the previously-picked one doesn't exist there).
A leaf figure's own internal group dropdown (see add_group_dropdown - every line()/coefplot()/stacked_bar() figure already has one, even if it's a single inert group covering everything) keeps working independently once its figure is shown - this only adds the outer switching between whole figures, it doesn't touch what's inside any of them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tree
|
dict[str, ...]
|
Nested dict, any depth (branches may differ in depth), with plotly Figures at the leaves. |
required |
label
|
str | list[str | None]
|
Text shown next to each level's dropdown. A single string only labels the top level (deeper ones are unlabeled) - the default, "View:". Pass a list to label (or skip, with None or "") each level individually, e.g. ["Run:", "CI:"] for a 2-level tree, or ["Run:", None, "Year:"] to leave the middle level unlabeled in a 3-level one. A "\n" anywhere in a level's label (it's stripped from the displayed text) starts that level's whole control - label and dropdown together - on a new row of the control bar, e.g. ["Run:", "\nCI:", "\nYear:"] puts each level on its own row. |
'View:'
|
width
|
int | None
|
Applied to every leaf figure before rendering. height default 700; width default None (each figure's own autosize setting is left alone). |
None
|
height
|
int | None
|
Applied to every leaf figure before rendering. height default 700; width default None (each figure's own autosize setting is left alone). |
None
|
layout
|
dict | None
|
Extra plotly layout properties applied to every leaf figure (via fig.update_layout(**layout)), after width/height - useful for formatting that should be consistent across every figure in the tree (e.g. a shared font). Since each leaf is a plain figure you built yourself before handing it to combine(), you can always fig.update_layout(...) an individual one beforehand instead, if only that one needs something different. |
None
|
dropdowns_padding_left
|
int
|
Left padding (px) on the label/dropdown table, default 15 - also applied to any leaf's own internal group dropdown (see add_group_dropdown), so the two line up instead of each floating at a different offset. This is cosmetic spacing for the dropdowns (and text) themselves, not an attempt to line them up with a figure's plotted data area - that area's left edge floats with each figure's own axis-label width (via Plotly's automargin) and can differ leaf to leaf, so no fixed padding here would track it correctly anyway. |
DEFAULT_LEFT_MARGIN_PX
|
Returns:
| Type | Description |
|---|---|
CombinedFigure
|
Call .write_html(path) on it - there's no single Figure object to return (this holds several at once), so unlike the other plot functions there's nothing to .show() directly in a notebook; open the saved HTML instead. |
Source code in src/survey_kit/plot/_combine.py
Lower-Level¶
add_group_dropdown is what line()/quantiles()/coefplot()/stacked_bar() each call internally to attach their own group dropdown - use it directly only if you're building a custom figure (a raw plotly.graph_objects.Figure) that should get the same dropdown/state-preservation behavior.
add_group_dropdown ¶
add_group_dropdown(
fig,
groups: dict[str, list[int]],
companions: dict[int, int] | None = None,
default_group: str | None = None,
label: str = "Show:",
legend_space_per: float = 0.1,
width: int | None = None,
height: int | None = None,
) -> "plotly.graph_objects.Figure"
Replace a figure's legend with a compact dropdown that swaps which named subset of traces is shown - for a figure with many traces (e.g. one line per state), a full legend is unwieldy, but a dropdown listing named groups (which need not be single traces - each can be several, e.g. an estimate line plus its CI band) is not.
Within whichever group is currently selected, clicking a legend
entry still toggles that one trace on/off (single click) or
isolates it (double click, matching plotly's own legend
convention) - both preserved across switching which group is
selected, and both preserved across a fig.write_html() round trip
(the state-tracking JS this injects is self-contained, no
survey_kit code needed to view the saved HTML later). This click
handling is always attached, even when there's only one group (so
no dropdown widget is shown) - it's what makes double-click-isolate
and companion-trace linking (see companions) work consistently
whether or not there's anything to switch between.
Which traces are toggled on/off is tracked in a single object shared
by every figure on the page (not kept private to each figure), keyed
by each trace's own group + name (its legend label) rather than its
position - so if "2016" is isolated in one figure, a sibling figure
with a same-named "2016" series in a same-named group can pick up
that exact same isolation (via the exposed gd._applyVisible())
instead of resetting to "everything visible", and two unrelated
series that happen to occupy the same list position in different
groups never interfere with each other. combine() calls
gd._applyVisible() automatically whenever it switches which whole
figure is shown.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
Figure
|
Figure to modify in place (and also returned, for chaining). |
required |
groups
|
dict[str, list[int]]
|
{group label: [trace indices in fig.data belonging to that group]}. A trace may belong to more than one group's list if that's useful, though the common case is a partition of range(len(fig.data)). |
required |
companions
|
dict[int, int] | None
|
{trace index: companion trace index} - whenever the first
trace's visibility changes (by any means: single-click toggle,
double-click isolate, or a group switch), the companion is set
to match. For a CI band drawn as its own |
None
|
default_group
|
str | None
|
Which group to show initially. Defaults to the first key in
|
None
|
label
|
str
|
Text shown next to the dropdown. Default "Show:". |
'Show:'
|
legend_space_per
|
float
|
Extra vertical margin reserved below the plot per 10 items in the largest group (the native plotly legend still renders for whichever traces are visible). Default 0.1. |
0.1
|
width
|
int | None
|
If given, fixes the figure's size instead of leaving it autosized. |
None
|
height
|
int | None
|
If given, fixes the figure's size instead of leaving it autosized. |
None
|
Returns:
| Type | Description |
|---|---|
Figure
|
The same |
Source code in src/survey_kit/plot/_dropdown.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |