Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- import ipywidgets as widgets
- from IPython.display import display
- import numpy as np
- # Replace this with the actual data.
- data = [
- (2020, "Joe Biden", 51.3, 'D'),
- (2020, "Donald Trump", 46.9, 'R'),
- (2016, "Hillary Clinton", 48.2, 'D'),
- (2016, "Donald Trump", 46.1, 'R'),
- (2012, "Barack Obama", 51.1, 'D'),
- (2012, "Mitt Romney", 47.2, 'R'),
- (2008, "Barack Obama", 52.9, 'D'),
- (2008, "John McCain", 45.7, 'R'),
- (2004, "John Kerry", 48.3, 'D'),
- (2004, "George W. Bush", 50.7, 'R'),
- (2000, "Al Gore", 48.4, 'D'),
- (2000, "George W. Bush", 47.9, 'R'),
- (1996, "Bill Clinton", 49.2, 'D'),
- (1996, "Bob Dole", 40.7, 'R'),
- (1992, "Bill Clinton", 43.0, 'D'),
- (1992, "George H. W. Bush", 37.4, 'R'),
- (1988, "Michael Dukakis", 45.6, 'D'),
- (1988, "George H. W. Bush", 53.4, 'R'),
- (1984, "Ronald Reagan", 58.8, 'D'),
- (1984, "Walter Mondale", 40.6, 'R'),
- (1980, "Jimmy Carter", 41.0, 'D'),
- (1980, "Ronald Reagan", 50.7, 'R'),
- (1976, "Jimmy Carter", 50.1, 'D'),
- (1976, "Gerald Ford", 48.0, 'R'),
- (1972, "George McGovern", 37.5, 'D'),
- (1972, "Richard Nixon", 60.7, 'R'),
- # ...
- ]
- def filter_data(threshold, year_range):
- start_year, end_year = year_range
- return [(year, name, party) for year, name, percentage, party in data if percentage > threshold and start_year <= year <= end_year]
- def plot_data(threshold, year_range):
- filtered_data = filter_data(threshold, year_range)
- years, names, parties = zip(*filtered_data)
- # Counting the candidates who meet the threshold
- D_count = parties.count('D')
- R_count = parties.count('R')
- fig, ax = plt.subplots(figsize=(15, 5))
- for i in range(len(years)):
- if parties[i] == 'D':
- ax.scatter(years[i], 0, color='blue', s=300, marker='2') # increased marker size
- ax.text(years[i], 0.05, names[i], ha='center')
- else:
- ax.scatter(years[i], 0, color='red', s=300, marker='1') # increased marker size
- ax.text(years[i], -0.05, names[i], ha='center')
- plt.yticks([])
- # Changed the xticks to be in the middle and only for the years in the selected range
- start_year, end_year = year_range
- plt.xticks(np.arange(start_year, end_year+1, 4))
- ax.xaxis.set_ticks_position('none')
- ax.set_xlabel('Years', labelpad=15)
- # Displaying the counts
- plt.text(start_year - 8, 0.1, f'Democrats: {D_count}', ha='right', va='center', fontsize=12, color='blue')
- plt.text(start_year - 8, -0.1, f'Republicans: {R_count}', ha='right', va='center', fontsize=12, color='red')
- plt.title(f'Presidential Candidates with More Than {threshold}% of Votes')
- plt.grid(axis='x')
- plt.show()
- threshold_slider = widgets.FloatSlider(
- value=40,
- min=40,
- max=55,
- step=0.1,
- description='Threshold:',
- continuous_update=False
- )
- year_range_slider = widgets.IntRangeSlider(
- value=[1800, 2024],
- min=1800,
- max=2024,
- step=4,
- description='Year range:',
- continuous_update=False
- )
- widgets.interact(plot_data, threshold=threshold_slider, year_range=year_range_slider)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement