Skip to main content

A Python package for political science data visualization.

Project description

PoliSciPy

License: MIT PyPI Version Conda Package tests codecov

PoliSciPy is an open-source Python library that makes it easy to generate electoral maps and explore political data with just a few lines of code. The library is designed for fast experimentation, clear visualizations, and flexible customization, whether you’re analyzing results, presenting findings, or exploring historical trends.

Key Features

  • Visualize Electoral Maps: Create U.S. electoral college maps with state labels, electoral votes, and party colors.
  • Customize Plots: Adjust figure size, title, edge colors, label colors, and other visual elements for customized plots.
  • Flexible Data Handling: Easily merge your own election results (current, historical, or hypothetical) with the map data for visualization.
Electoral College Map
Example: Figure with results from the 2024 U.S. election.

Installation

PoliSciPy requires Python 3.x and can be installed using pip:

pip install poliscipy

PoliSciPy is also available on conda via:

conda install -c conda-forge poliscipy

Dependencies: Note that PoliSciPy requires GeoPandas, matplotlib, and Pandas

Quickstart and Example

Creating electoral college maps using PoliSciPy can be done in only three simple steps:

  1. Load the GeoDataFrame that contains the electoral college geospatial data
  2. Load and merge the specific data you'd like to plot with the GeoDataFrame
  3. Call the plot_electoral_map() function, passing in your GeoDataFrame and the target column for plotting

Below is an example of how to use PoliSciPy to visualize the 2024 U.S. electoral college map shown above.

import poliscipy

from poliscipy.shapefile_utils import load_shapefile
from poliscipy.plot import plot_electoral_map

# Load in GeoDataFrame containing U.S. electoral college geospatial data
gdf = load_shapefile()

# Create a dictionary with the data to plot
winning_party = {
    'AL': 'Republican','AK': 'Republican','AZ': 'Republican','AR': 'Republican', ...
}

# Merge your data with the gdf and fill any missing data with 'No Data'
gdf['winning_party'] = gdf['STUSPS'].map(winning_party).fillna('No Data')

# Add the number of electors that voted for the other candidate
gdf.loc[38, 'defectors'] = 1 # maine
gdf.loc[10, 'defectors'] = 1 # nebraska

# Set the political party for each of the congressional district winners
gdf.loc[38, 'defector_party'] = 'Republican'
gdf.loc[10, 'defector_party'] = 'Democrat'

# Plot the electoral college map for the year 2024
plot_electoral_map(gdf, column='winning_party', title='2024 U.S. Electoral College Map')

Documentation

Complete documentation for PoliSciPy can be found here.

Contributing

PoliSciPy welcomes contributions! Please see the CONTRIBUTING.md for guidelines on how to get involved.

Citation

If you find PoliSciPy useful in your research, academic projects, or software, please cite it using the CITATION.cff file located in the root directory of this repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

poliscipy-0.1.0.tar.gz (3.3 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

poliscipy-0.1.0-py3-none-any.whl (3.3 MB view details)

Uploaded Python 3

File details

Details for the file poliscipy-0.1.0.tar.gz.

File metadata

  • Download URL: poliscipy-0.1.0.tar.gz
  • Upload date:
  • Size: 3.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for poliscipy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d2ff7f6b09dd1a7721e945633f362c28fbd2b42406ccb3ec6d9179b024c95c3b
MD5 0b9f48de100b9af0932bfba4db961ce1
BLAKE2b-256 ed150ab0dff9a1c950eb72b76fb7b45cb2fc64cdf2a2541079ee23bdb1d8ca50

See more details on using hashes here.

File details

Details for the file poliscipy-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: poliscipy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.3 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for poliscipy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9500ee81a0fd36eabb460bac207085bd554ae1bd90390eb88619f8294cf9febe
MD5 e184a32049ecbce6272186e9217789cf
BLAKE2b-256 35b125342cd581276b6e0492bb5263364ee789f5608b83c6554cc84ba2745627

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page