-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathklimabonus_bar_split.py
51 lines (40 loc) · 1.54 KB
/
klimabonus_bar_split.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import json
import matplotlib.pyplot as plt
import numpy as np
# Load JSON data
with open('nrwahl_plz.json', 'r') as file:
data = json.load(file)
# List of parties to analyze
parties = ['ÖVP', 'FPÖ', 'SPÖ', 'NEOS', 'GRÜNE']
categories = list(range(1, 5))
votes_by_party_and_category = {party: {category: 0 for category in range(1, 5)} for party in parties}
# Define custom colors for each party
party_colors = {
'ÖVP': 'black',
'FPÖ': 'blue',
'SPÖ': 'red',
'NEOS': 'pink',
'GRÜNE': 'green',
}
# Count votes for each party in each climate category
for area in data:
for party in parties:
party_votes = int(area[party])
category = int(area['klima_category'])
votes_by_party_and_category[party][category] += party_votes
# Find the maximum y-axis value across all subplots
max_y = 800000
# Create separate bar charts for each party in the same window with custom colors and equal y-axis scale
fig, axes = plt.subplots(1, len(parties), figsize=(15, 5), sharey=True)
for i, party in enumerate(parties):
vote_counts = [votes_by_party_and_category[party][category] for category in categories]
ax = axes[i]
ax.bar(categories, vote_counts, tick_label=categories, color=party_colors[party])
ax.set_xlabel('Klimabonus Kategory')
ax.set_ylabel('Abgegebne Stimmen (NR2019)')
ax.set_title(f'Stimmen pro Kategory {party}')
ax.set_ylim(0, max_y) # Set the same y-axis limits for all subplots
# Adjust spacing between subplots
plt.tight_layout()
plt.savefig('klimabonus_bar_split.png')
plt.show()