The Federal Chancellery maintains data on the Swiss political system. The curia dataset is publicly available it provides data on political parties, parliamentary comissions, members of parliament and their affiliations.
Parliament data is also available as Linked Data.
Swiss political data can be accessed with SPARQL queries.
You can send queries using HTTP requests. The API endpoint is https://lindas.admin.ch/query/.
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import warnings
from plotly.subplots import make_subplots
from graphly.api_client import SparqlClient
%matplotlib inline
pd.set_option('display.max_rows', 100)
warnings.filterwarnings('ignore')
# Uncomment to install dependencies in Colab environment
#!pip install git+https://github.com/zazuko/graphly.git
sparql = SparqlClient("https://int.lindas.admin.ch/query")
sparql.add_prefixes({
"schema": "<http://schema.org/>"
})
SPARQL queries can become very long. To improve the readibility, we will work wih prefixes.
Using the add_prefixes
method, we can define persistent prefixes.
Every time you send a query, graphly
will now automatically add the prefixes for you.
query = """
SELECT ?council ?faction (COUNT(?mep) AS ?members)
FROM <https://lindas.admin.ch/fch/curia>
FROM <https://lindas.admin.ch/fch/rvov>
WHERE {
VALUES ?_council { <https://politics.ld.admin.ch/council/S> <https://politics.ld.admin.ch/council/N> <https://politics.ld.admin.ch/council/UFA>}
?mep a schema:Person;
schema:memberOf/schema:memberOf ?_council;
schema:memberOf ?role.
FILTER NOT EXISTS { ?role schema:endDate ?end }
?role schema:memberOf ?_faction.
?_faction a <http://schema.org/ParliamentaryGroup>;
schema:name ?faction.
?_council schema:name ?council.
FILTER (lang(?council) = 'de')
FILTER (lang(?faction) = 'de')
}
GROUP BY ?council ?faction
ORDER BY DESC(?council) DESC (?members)
"""
df = sparql.send_query(query)
N_FACTIONS=df.faction.nunique()
cmap = plt.get_cmap('YlOrRd')
colors = [mcolors.rgb2hex(cmap((i+1)/N_FACTIONS)) for i in range(N_FACTIONS)]
factions = [
"Grünliberale Fraktion",
"Grüne Fraktion",
"Die Mitte-Fraktion. Die Mitte. EVP.",
"Sozialdemokratische Fraktion",
"FDP-Liberale Fraktion",
"Fraktion der Schweizerischen Volkspartei"
]
colormap = dict(zip(factions,colors))
fig = make_subplots(rows=1, cols=3, subplot_titles=df["council"].unique(), specs=[[{"type": "pie"}, {"type": "pie"}, {"type": "pie"}]])
for i, council in enumerate(df["council"].unique()):
members = dict(zip(df[df.council == council]["faction"],df[df.council == council]["members"]))
fig.add_trace(go.Pie(
values=[members[f] if f in members else 0 for f in factions],
labels=factions, sort=False
), row=1, col=i+1)
fig.update_traces(textinfo='none', marker={"colors": [colormap[f] for f in factions]})
fig.update_annotations(yshift=-280)
fig.update_layout(height=400, title={"text": "Members of Parliament (as of today)", "x": 0.5})
fig.show()