mirror of
https://github.com/fhswf/aki_prj23_transparenzregister.git
synced 2025-07-19 11:01:02 +02:00
Reverting black for the jupyter notebooks gets old. Can we just run black over all of them?
192 KiB
192 KiB
Erstellung verschiedener Diagrammarten mit Plotly¶
Installiere Plotly, beispielsweise mit pip:
In [417]:
# install plotly using pip
!pip3.10 install plotly
Zum statischen Speichern der Plotly Figuren, installiere Kaleido:
In [418]:
!pip3.10 install -U kaleido
Generierung von Beispieldaten und Anlegen eines Pandas-Dataframes:
In [419]:
# import libraries
import plotly.express as px
import plotly.graph_objs as go
import pandas as pd
import numpy as np
# create sample data for one company
data = {
"Jahr": ["2017", "2018", "2019", "2020", "2021", "2022"],
"Umsatz": [19, 23, 30, 42, 37, 45],
}
# save as pandas dataframe
df = pd.DataFrame.from_dict(data)
# create sample data for a second company
data2 = {
"Jahr": ["2017", "2018", "2019", "2020", "2021", "2022"],
"Umsatz": [15, 21, 33, 28, 27, 30],
}
# save as pandas dataframe
df2 = pd.DataFrame.from_dict(data2)
Säulendiagramme¶
Erstellung eines "einfachen" Säulendiagramms:
In [420]:
# create bar plot
fig_saeule = px.bar(df, y="Umsatz", labels={"index": "", "Umsatz": ""})
# set color
fig_saeule.update_traces(marker_color="#00509b")
# save as image
fig_saeule.write_image("Saeule.png")
# show in notebook
fig_saeule.show()
Erstellung eines "einfachen" Balkendiagramms:
In [421]:
# create horizontal bar plot
fig_balken = px.bar(df, x="Umsatz", labels={"index": "", "Umsatz": ""}, orientation="h")
# set color
fig_balken.update_traces(marker_color="#00509b")
# save as image
fig_balken.write_image("Balken.png")
# show in notebook
fig_balken.show()
Ergänzung von Titel und Achsenbeschriftungen:
In [422]:
# sreate bar plot with named labels and title
fig_saeule_titel = px.bar(
df,
x="Jahr",
y="Umsatz",
labels={"Umsatz": "Umsatz in Mio.€"},
title="Umsatzentwicklung von Unternehmen A",
)
# set color
fig_saeule_titel.update_traces(marker_color="#00509b")
# save as image
fig_saeule_titel.write_image("Saeule_Titel.png")
# show in notebook
fig_saeule_titel.show()
Vergleich zweier Datensätze in einem Säulendiagramm:
In [423]:
# create figure
fig_saeule_zwei = go.Figure()
# add trace for company 1
fig_saeule_zwei.add_trace(
go.Bar(x=df["Jahr"], y=df["Umsatz"], name="A", marker_color="#00509b")
)
# add trace for company 2
fig_saeule_zwei.add_trace(
go.Bar(x=df2["Jahr"], y=df2["Umsatz"], name="B", marker_color="#6f7072")
)
# update layout to grouped
fig_saeule_zwei.update_layout(barmode="group")
# set title and labels
fig_saeule_zwei.update_layout(
title="Vergleich der Umsatzentwicklung",
xaxis_title="Jahr",
yaxis_title="Umsatz in Mio.€",
legend_title="Unternehmen",
)
# save as image
fig_saeule_zwei.write_image("Saeule_Zwei.png")
# show in notebook
fig_saeule_zwei.show()
Liniendiagramme¶
Erstellen eines "einfachen" Liniendiagramms:
In [424]:
# create line plot
fig_line = px.line(df, y=df["Umsatz"], labels={"index": "", "Umsatz": ""})
# set color
fig_line.update_traces(line_color="#00509b")
# save as image
fig_line.write_image("Linie.png")
# show in network
fig_line.show()
In [425]:
# create figure
fig_line = go.Figure()
# add trace for company 1
fig_line.add_trace(
go.Scatter(
x=df["Jahr"],
y=df["Umsatz"],
name="A",
line_color="#00509b",
marker_color="#00509b",
)
)
# add trace for company 2
fig_line.add_trace(
go.Scatter(
x=df2["Jahr"],
y=df2["Umsatz"],
name="B",
line_color="#6f7072",
marker_color="#6f7072",
)
)
# set title and labels
fig_line.update_layout(
title="Vergleich der Umsatzentwicklung",
xaxis_title="Jahr",
yaxis_title="Umsatz in Mio.€",
legend_title="Unternehmen",
)
# save as image
fig_line.write_image("Linie_Vergleich.png")
# show in notebook
fig_line.show()
Streudiagramm¶
In [426]:
# create sample data
x = [1, 2, 2, 3, 5, 5, 6, 6, 6, 7, 8, 8, 8, 10, 10, 10, 7, 4, 3, 4, 9, 6]
y = [1, 2, 3, 3, 3, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 10, 2, 10, 8, 6, 6, 4]
# create scatter plot
scatter = go.Figure(
data=go.Scatter(x=x, y=y, mode="markers", marker={"color": "#00509b"})
)
# save as image
scatter.write_image("Streudiagramm.png")
# show in netbook
scatter.show()
Kreisdiagramm¶
Erstellung eines "einfachen" Kreisdiagramms:
In [427]:
# create sample data
sentiment = [0.1, 0.3, 0.6]
scores = ["negativ", "neutral", "positiv"]
# create pie chart
fig_kreis = px.pie(
values=sentiment,
color=scores,
color_discrete_map={"negativ": "black", "neutral": "#6f7072", "positiv": "#00509b"},
)
# save as image
fig_kreis.write_image("Kreis.png")
# show in notebook
fig_kreis.show()
Erweiterung um geeignete Farben, Beschriftungen und einen Titel:
In [428]:
# create figure
fig_sentiment = px.pie(
values=sentiment,
names=scores,
color=scores,
color_discrete_map={
"negativ": "lightcoral",
"neutral": "moccasin",
"positiv": "darkseagreen",
},
title="Stimmungsanalyse basierend auf Nachrichtenartikel X",
)
# change line color
fig_sentiment.update_traces(marker=dict(line=dict(color="#000000", width=2)))
# save as image
fig_sentiment.write_image("Kreis_Sentiment.png")
# show in notebook
fig_sentiment.show()
Pfeile¶
Achtung: Berechnungen stimmen noch nicht, Beispiel funktioniert bisher nur für 0.75 und 0.25 einigermaßen
In [429]:
import matplotlib.patches as patches
import matplotlib.pyplot as plt
import numpy as np
compound = 0.75
angle = (compound * 100 - 50) * 1.8
x_length = (np.sin(np.radians(angle)) * 1.2) / 2
y_length = (np.cos(np.radians(angle)) * 1.2) / 2
if compound < 0.5:
y_tail = y_length
y_head = -y_length
x_tail = x_length
x_head = -x_length
else:
y_tail = -y_length
y_head = y_length
x_tail = -x_length
x_head = x_length
dx = x_head - x_tail
dy = y_head - y_tail
fig, ax = plt.subplots()
arrow = patches.FancyArrowPatch(
(x_tail, y_tail),
(x_head, y_head),
mutation_scale=100,
ec="darkseagreen",
fc="darkseagreen",
)
ax.add_patch(arrow)
plt.axis("off")
ax.set_xlim([-1, 1])
ax.set_ylim([-1, 1])
ax.set_title("Aktueller Stimmungstrend", fontsize=20)
fig.suptitle(" ", fontsize=24)
fig.savefig("Pfeil.png")