plot¶
The plot
module provides users with Python only implementations of common electoral plots.
A novel addition to social science analysis is the disproportionality bar plot, which graphically depicts the disproportionality between expected and realized results. Bar widths are the proportion of shares (ex: votes received), and heights are the difference or relative difference between shares and allocations (ex: parliament seats received).
Functions
- poli_sci_kit.plot.dispr_bar(shares, allocations, labels=None, colors=None, total_shares=None, total_alloc=None, percent=False, dsat=0.95, axis=None)[source]¶
Plots the difference in allocated seats to received shares.
- Parameters:
- shareslist
The shares amounts or those that allocations should be compared to.
- allocationslist
The allocated amounts.
- labelslist (default=None)
A list of group names as labels for the x-axis.
- colorslist or list of listsoptional (default=None)
The colors of the groups as hex keys.
- total_sharesint (default=None)
The total share amounts.
Note: allows for subsets of the total groups.
- total_allocint (default=None)
The total number of allocations amounts.
Note: allows for subsets of the total groups.
- percentbool (default=False)
Whether the y-axis should depict relative changes or not.
- dsatfloatoptional (default=default_sat)
The degree of desaturation to be applied to the colors.
- axisstroptional (default=None)
Adds an axis to plots so they can be combined.
- Returns:
- axmatplotlib.pyplot.subplot
A bar plot with aggregate or relative seat-share differences and bar widths representing share proportions.
poli_sci_kit also provides Python only implementations of both rectangular and semicircle parliament plots:
- poli_sci_kit.plot.parliament(allocations, labels=None, colors=None, style='semicircle', num_rows=2, marker_size=200, speaker=False, df_seat_lctns=None, dsat=0.95, axis=None, legend=False, **kwargs)[source]¶
Produces a parliament plot given seat allocations.
- Parameters:
- allocationslist
The share of seats given to the regions or parties.
- labelslist (contains strs)optional (default=None)
The names of the groups.
- colorslistoptional (default=None)
The colors of the groups as hex keys.
- stylestr (default=semicircle)
Whether to plot the parliament as a semicircle or a rectangle.
- num_rowsint (default=2)
The number of rows in the plot.
- marker_sizeint or float (default=200)
The size of the scatterplot markers that make up the plot.
- speakerbooloptional (default=False)
Whether to include a point for the speaker of the house colored by their group.
Note: ‘True’ colors the point based on the largest group, but passing a name from ‘labels’ is also possible.
- df_seat_lctnsdfoptional (default=None)
A df of coordinates to plot.
- dsatfloatoptional (default=default_sat)
The degree of desaturation to be applied to the colors.
- axisstroptional (default=None)
Adds an axis to plots so they can be combined.
- Returns:
- axmatplotlib.pyplot.subplot
Parliament seat distribution as either an arc or a rectangle, each having the option to be converted to seats.
The parliament plot function is built on top of scatter plots that are derived in the package’s utils module:
- poli_sci_kit.utils.gen_parl_points(allocations, labels=None, style='semicircle', num_rows=2, speaker=False)[source]¶
Produces a df with coordinates for a parliament plot.
- Parameters:
- allocationslist
The share of seats given to the regions or parties.
- labelslistoptional (default=None)
The names of the groups.
- stylestr (default=semicircle)
Whether to plot the parliament as a semicircle or a rectangle.
- num_rowsint (default=2)
The number of rows in the plot.
- speakerbooloptional (default=False)
Whether to include a point for the speaker of the house colored by their group.
Note: ‘True’ colors the point based on the largest group, but passing a name from ‘labels’ is also possible.
- Returns:
- df_seat_lctnspd.DataFrame
A dataframe with points to be converted to a parliament plot via seaborn’s scatterplot.
A final function to swap poorly allocated seats is also provided in utils:
- poli_sci_kit.utils.swap_parl_allocations(df, row_0, pos_0, row_1, pos_1)[source]¶
Replaces two allocations of the parliament plot df to clean up coloration.
- Parameters:
- dfpandas.DataFrame
DataFrame containing parliament data
- row_0int
The row of one seat to swap.
- pos_0int
The position in the row of one seat to swap.
- row_1int
The row of the other seat to swap.
- pos_1int
The position in the row of the other seat to swap.
- Returns:
- df_seat_lctnspd.DataFrame
A parliament plot allocations data frame with two allocations swapped