Sea-level plots

SeaLevelPlot connects one plan’s values across an ordered set of categories. Repeating that operation for many plans creates the characteristic sea of lines.

from gerrytools.plotting import SeaLevelPlot

starting_plan = {
    "President 2016": 7 / 14,
    "Governor 2018": 6 / 14,
    "President 2020": 7 / 14,
    "U.S. Senate 2020": 8 / 14,
}

plot = SeaLevelPlot()
plot.add_dataset(starting_plan, linecolor="alizarin")
plot.show()
../../../../_images/9f503090a38544d95b5118ffbc91e73df4ed243e86bf49fa4317609a2b3c3de5.png

A mapping keeps category labels and order with the values. Lists, Series objects, and one DataFrame row are also accepted.

Frequently, it is useful to change the scale of the y-axis to use fractional values. There is also a convenience function for this.

plot = SeaLevelPlot()
plot.add_dataset(starting_plan, linecolor="alizarin")
plot.format_ylabels_as_fractions(denominator=14)
plot.set_ylim(3 / 14, 10 / 14)
plot.show()
../../../../_images/07c184ccb66c67e22fb9fae96c6cf5f91e9d6e54a19239be73bfa6ea01676f6d.png

It is also common to try and plot multiple plans on the same set of axis. Since seat counts are discrete, this can cause a collision leaving one or more of the lines difficult to see. Visibility can be improved by adding some jitter to the plot.

plan_2 = {
    "President 2016": 5 / 14,
    "Governor 2018": 6 / 14,
    "President 2020": 7 / 14,
    "U.S. Senate 2020": 5 / 14,
}

plot = SeaLevelPlot(jitter_rng_seed=42)
plot.add_dataset(starting_plan, linecolor="alizarin")
plot.add_dataset(plan_2, linecolor="default_grey")
plot.format_ylabels_as_fractions(denominator=14)
plot.set_vertical_jitter(0.01)
plot.set_horizontal_jitter(0.01)
plot.set_ylim(3 / 14, 10 / 14)
plot.show()
../../../../_images/0f31e08be3a244593a60ffaaa48a6e0420ee62d4955e8e5e318dacad974b9dde.png

Shared axes, labels, legends, and output controls are covered in shared statistical controls.