diff --git a/src/T15_EnergyPlots.py b/src/T15_EnergyPlots.py index 3765ff5..ac3a753 100644 --- a/src/T15_EnergyPlots.py +++ b/src/T15_EnergyPlots.py @@ -2,6 +2,8 @@ # alle energieformen in einen plot import matplotlib.pyplot as plt import pandas as pd +from pandas.core.config_init import max_cols + energy_df = pd.read_csv("../data/germany_energy_mix_2019_2024.csv") # 1) Neue spalte energy_df["Year Quarter"] = energy_df["Year"].astype(str) + " " + energy_df['Quarter'] @@ -43,7 +45,51 @@ def energy_plots(): # 2. In 2 Plots übereinander # - greenenergies # - nongreenenergies +green_sources = ['Solar', 'Wind_Onshore', 'Wind_Offshore', 'Hydroelectric', 'Biomass'] +nongreen_sources = ['Natural_Gas', 'Coal_Hard', 'Coal_Lignite', 'Nuclear', 'Oil'] +fig, (ax1, ax2) = plt.subplots(2, 1, + figsize=(15, 12), + sharex=True) # als pdf, png speichern + +# top-plot die grünen Energien +for source in green_sources: + ax1.plot(quarterly_data.index, + quarterly_data[source], + color=colors.get(source, "green"), + label=source, + marker="o", linewidth=2, markersize=4) + +for source in nongreen_sources: + ax2.plot(quarterly_data.index, + quarterly_data[source], + color=colors.get(source, "brown"), + label=source, + marker="o", linewidth=2, markersize=4) + +ax1.set_title("Erneuerbaren Energien") +ax2.set_title("Nichterneuerbaren Energien") + +# jede spalte 1 max = series => max -> einen wert +max_value = quarterly_data.max().max() # * 1.05 +max_value = max_value + (10 - max_value % 10) +min_value = quarterly_data.min().min() +print(max_value) +for ax in [ax1, ax2]: + ax.set_ylim(min_value, max_value) + ax.grid(True, alpha=0.3) + ax.legend() + ax.set_ylabel("EnergieProduktion [TWh]") + +# nur jedes 2te quartal +labels = ax2.get_xticklabels() +for i, label in enumerate(labels): + if i % 2 == 1: + label.set_visible(False) + +fig.tight_layout() # enger zusammen +fig.savefig("energy_by_quarter.pdf") # pdf, svg, eps +plt.show() # 3. Alle energien nach type in einem barplot # - pie-plot diff --git a/src/T16_plotly_expls.py b/src/T16_plotly_expls.py new file mode 100644 index 0000000..35ea486 --- /dev/null +++ b/src/T16_plotly_expls.py @@ -0,0 +1,100 @@ +import pandas as pd +import plotly # pip/conda install plotly +# pip install kaleido +# javascript welches den plot enthält +import plotly.io as pio + +pio.renderers.default = "browser" +# T14_plotten.py +cities = {"Stadt": ["London", "Berlin", "Madrid", "Rom", "Paris", "Wien", "Bukarest", "Hamburg", "Budapest", "Warsaw", "Barcelona", "München", "Mailand"], + "Population": [8615246, 3562166, 3165235, 2874038, 2273305, 1805681, 1803425, 1760433, 1754000, 1740119, 1602386, 1493900, 1350680], + "Land": ["England", "Deutschland", "Spanien", "Italien", "Frankreich", "Österreich", "Romanien", "Deutschland", "Ungarn", "Polen", "Spanien", "Deutschland", "Italien"]} +cities_df = pd.DataFrame(cities) +areas = [1572, 892, 604, 1285, 105,415, 228, 755, 525, 517, 101, 310, 182] +cities_df = pd.DataFrame(cities) +cities_df["Flaeche"] = areas + +# **kwargs +fig = cities_df.plot(x="Stadt", y=["Population", "Flaeche"], backend="plotly") +fig.show() + + +energy_df = pd.read_csv("../data/germany_energy_mix_2019_2024.csv") +# 1) Neue spalte +energy_df["Year Quarter"] = energy_df["Year"].astype(str) + " " + energy_df['Quarter'] +energy_df.drop(["Year", "Quarter"], axis=1, inplace=True) +quarterly_data = energy_df.pivot_table( + index="Year Quarter", + columns="Energy_Source", + values="Generation_TWh", + aggfunc="sum", + fill_value=0, +) + +print(quarterly_data) +colors = { + 'Solar': '#FFD700', # Gold + 'Wind_Onshore': '#87CEEB', # Sky Blue + 'Wind_Offshore': '#4682B4', # Steel Blue + 'Hydroelectric': '#00CED1', # Dark Turquoise + 'Biomass': '#32CD32', # Lime Green + 'Natural_Gas': '#FF6347', # Tomato + 'Coal_Hard': '#2F4F4F', # Dark Slate Gray + 'Coal_Lignite': '#696969', # Dim Gray + 'Nuclear': '#FF4500', # Orange Red + 'Oil': '#8B4513' # Saddle Brown +} +pd.options.plotting.backend = 'plotly' # backend gesetzt +fig = quarterly_data.plot() +for i, (col, color) in enumerate(colors.items()): + print(quarterly_data.columns.get_loc(col)) + + fig.data[quarterly_data.columns.get_loc(col)].line.color = color + +fig.show() + +# klassische Ansatz +import plotly.graph_objs as go +fig = go.Figure() + +for column in quarterly_data.columns: + fig.add_trace(go.Scatter( + x=quarterly_data.index, + y=quarterly_data[column], + mode='lines', + name=column, + line={'color': colors.get(column, "#000000")} + )) + +fig.update_layout( + title="Quarterly energy production", + xaxis_title="Year Quarter", + yaxis_title="Generation [TWh]" +) + +fig.show() + +# '2019 Q1' Biomass 5.2 +# '2019 Q1' Solar 9.4 +print(quarterly_data) +#melted = quarterly_data.reset_index().melt(id_vars="Year Quarter", value_vars=[]) +melted = quarterly_data.melt(ignore_index=False) +fig = melted.plot(x=melted.index, + y=melted["value"], + colors='Energy_Source', + color_discrete_map=colors) +fig.show() + +fig = go.Figure() +fig.add_trace(go.Bar( + x=melted.index, + y=melted["value"], + ) +) +fig.update_layout( + title="Bars of energy", + xaxis_title="Year Quarter", + yaxis_title="Generation [TWh]" +) + +fig.show() \ No newline at end of file diff --git a/src/T17_merging_of_frames.py b/src/T17_merging_of_frames.py new file mode 100644 index 0000000..cd3de4f --- /dev/null +++ b/src/T17_merging_of_frames.py @@ -0,0 +1,22 @@ +import pandas as pd + + +kunden1 = pd.DataFrame({ + 'kunden_id': [1, 2], + 'name': ['Anna', 'Ben'], + 'stadt': ['Berlin', 'München'] +}) + +kunden2 = pd.DataFrame({ + 'kunden_id': [3, 4], + 'name': ['Clara', 'David'], + 'stadt': ['Hamburg', 'Köln'] +}) + + +bestellungen = pd.DataFrame({ + 'bestell_id': [101, 102, 103, 104], + 'kunden_id': [1, 2, 1, 5], + 'produkt': ['Laptop', 'Maus', 'Tastatur', 'Monitor'], + 'preis': [800, 25, 50, 300] +})