Advertisement
Guest User

Jupyter Notebook - Presidential Candidates that Passed X% of Votes

a guest
May 26th, 2023
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.26 KB | History | 0 0
  1. import matplotlib.pyplot as plt
  2. import ipywidgets as widgets
  3. from IPython.display import display
  4. import numpy as np
  5.  
  6. # Replace this with the actual data.
  7. data = [
  8.     (2020, "Joe Biden", 51.3, 'D'),
  9.     (2020, "Donald Trump", 46.9, 'R'),
  10.    
  11.     (2016, "Hillary Clinton", 48.2, 'D'),
  12.     (2016, "Donald Trump", 46.1, 'R'),
  13.    
  14.     (2012, "Barack Obama", 51.1, 'D'),
  15.     (2012, "Mitt Romney", 47.2, 'R'),
  16.    
  17.     (2008, "Barack Obama", 52.9, 'D'),
  18.     (2008, "John McCain", 45.7, 'R'),
  19.    
  20.     (2004, "John Kerry", 48.3, 'D'),
  21.     (2004, "George W. Bush", 50.7, 'R'),
  22.    
  23.     (2000, "Al Gore", 48.4, 'D'),
  24.     (2000, "George W. Bush", 47.9, 'R'),
  25.    
  26.     (1996, "Bill Clinton", 49.2, 'D'),
  27.     (1996, "Bob Dole", 40.7, 'R'),
  28.    
  29.     (1992, "Bill Clinton", 43.0, 'D'),
  30.     (1992, "George H. W. Bush", 37.4, 'R'),
  31.    
  32.    
  33.     (1988, "Michael Dukakis", 45.6, 'D'),
  34.     (1988, "George H. W. Bush", 53.4, 'R'),
  35.    
  36.     (1984, "Ronald Reagan", 58.8, 'D'),
  37.     (1984, "Walter Mondale", 40.6, 'R'),
  38.    
  39.     (1980, "Jimmy Carter", 41.0, 'D'),
  40.     (1980, "Ronald Reagan", 50.7, 'R'),
  41.    
  42.     (1976, "Jimmy Carter", 50.1, 'D'),
  43.     (1976, "Gerald Ford", 48.0, 'R'),
  44.    
  45.     (1972, "George McGovern", 37.5, 'D'),
  46.     (1972, "Richard Nixon", 60.7, 'R'),
  47.     # ...
  48. ]
  49.  
  50. def filter_data(threshold, year_range):
  51.     start_year, end_year = year_range
  52.     return [(year, name, party) for year, name, percentage, party in data if percentage > threshold and start_year <= year <= end_year]
  53.  
  54. def plot_data(threshold, year_range):
  55.     filtered_data = filter_data(threshold, year_range)
  56.     years, names, parties = zip(*filtered_data)
  57.  
  58.     # Counting the candidates who meet the threshold
  59.     D_count = parties.count('D')
  60.     R_count = parties.count('R')
  61.  
  62.     fig, ax = plt.subplots(figsize=(15, 5))
  63.     for i in range(len(years)):
  64.         if parties[i] == 'D':
  65.             ax.scatter(years[i], 0, color='blue', s=300, marker='2')  # increased marker size
  66.             ax.text(years[i], 0.05, names[i], ha='center')
  67.         else:
  68.             ax.scatter(years[i], 0, color='red', s=300, marker='1')  # increased marker size
  69.             ax.text(years[i], -0.05, names[i], ha='center')
  70.  
  71.     plt.yticks([])
  72.  
  73.     # Changed the xticks to be in the middle and only for the years in the selected range
  74.     start_year, end_year = year_range
  75.     plt.xticks(np.arange(start_year, end_year+1, 4))
  76.     ax.xaxis.set_ticks_position('none')
  77.     ax.set_xlabel('Years', labelpad=15)
  78.  
  79.     # Displaying the counts
  80.     plt.text(start_year - 8, 0.1, f'Democrats: {D_count}', ha='right', va='center', fontsize=12, color='blue')
  81.     plt.text(start_year - 8, -0.1, f'Republicans: {R_count}', ha='right', va='center', fontsize=12, color='red')
  82.  
  83.     plt.title(f'Presidential Candidates with More Than {threshold}% of Votes')
  84.     plt.grid(axis='x')
  85.     plt.show()
  86.  
  87. threshold_slider = widgets.FloatSlider(
  88.     value=40,
  89.     min=40,
  90.     max=55,
  91.     step=0.1,
  92.     description='Threshold:',
  93.     continuous_update=False
  94. )
  95.  
  96. year_range_slider = widgets.IntRangeSlider(
  97.     value=[1800, 2024],
  98.     min=1800,
  99.     max=2024,
  100.     step=4,
  101.     description='Year range:',
  102.     continuous_update=False
  103. )
  104.  
  105. widgets.interact(plot_data, threshold=threshold_slider, year_range=year_range_slider)
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement