import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#from statsmodels.graphics.gofplots import qqplot
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from ipywidgets import *
from IPython.display import display, HTML
import plotly.io as pio
from ipywidgets.embed import embed_minimal_html, dependency_state
embed_minimal_html('interactive-eda-beetle-damage-export.html',views=[Tree_filter, cause_filter, state_filter,out] )#, state=dependency_state([Tree_filter, cause_filter, state_filter,out]
%precision 2
%config InlineBackend.figure_format = 'retina'
pd.set_option("mode.chained_assignment", None)
def Schadholzeinschlag_Bundesländer_Einschlagsursache():
States =['Baden-Württemberg','Bayern','Berlin','Brandenburg','Bremen','Hamburg','Hessen','Mecklenburg-Vorpommern','Niedersachsen',
'Nordrhein-Westfalen', 'Rheinland-Pfalz','Saarland','Sachsen','Sachsen-Anhalt','Schleswig-Holstein','Thüringen']
Causes =['Insects', "Wind/Storm", "Snow/Ice","Drought",'Total'] #"unknown",
Tree_types =['Oak and red oak', 'Beech and other deciduous wood','Pine and larch','Spruce, fir,Douglas fir and other coniferous trees','Total']
url= "http://vanoproy.be/python/41261-0012Schadholzeinschlag_Bundeslander_Einschlagsursache.csv" # www/
#s=requests.get(url).content
#c=pd.read_csv(io.StringIO(s.decode('utf-8')))
Timbercut_causeB = pd.read_csv(url, sep=";",skiprows=6, engine="python",encoding="ANSI",skipfooter=4,decimal=",")# , nrows=nrows
Timbercut_causeB=Timbercut_causeB[Timbercut_causeB.Owner=="Insgesamt"]
#Timbercut_causeB
#Timbercut_causeB["value"] =Timbercut_causeB["value"].astype( "float32")
Timbercut_causeB =Timbercut_causeB.replace("-","0") # fillna( 0) #
lossyears= [2011,2012,2013,2014,2015,2016,2017,2018,2019,2020]
Timbercut_causeB["Year"]= Timbercut_causeB.loc[:,"Year"].astype("int16")
Timbercut_causeB2018All= Timbercut_causeB[Timbercut_causeB.Year.isin(lossyears )] #.copy()
#Timbercut_causeB2018All.sample(10)
treetypedict= {"Fichte, Tanne, Douglasie und sonstiges Nadelholz":'Spruce, fir,Douglas fir and other coniferous trees', "Kiefer und Lärche":'Pine and larch',
"Buche und sonstiges Laubholz":"Beech and other deciduous wood", "Eiche und Roteiche":"Oak and red oak","Insgesamt":"Total", "Insekten":"Insects","Trockenheit":"Drought",
"Schnee/Duft":"Snow/Ice","Wind/Sturm":"Wind/Storm","Sonstiges":"Other"}
Timbercut_causeB2018All["Cause"]= Timbercut_causeB2018All["Cause"].map( treetypedict )
Timbercut_causeB2018All["Tree_type"] = Timbercut_causeB2018All["Tree_type"].map( treetypedict)
df =Timbercut_causeB2018All.melt(id_vars=["Year","Cause","Tree_type","Owner"], var_name="German State")
df['Year'] =pd.to_datetime( df.Year, format='%Y' ) # .astype('datetime64["Y"]') np.datetime64(1, 'Y')
df = df.rename( columns={"value": "Value"}, errors="raise" ) # avoid strange behavior
df.Value =df.Value.astype( "float32") #is_numeric()
df['Cause'] = df.Cause.replace( np.nan, "unknown")
print(df.head() )
return df
df = Schadholzeinschlag_Bundesländer_Einschlagsursache()
df.info()
States =['Baden-Württemberg','Bayern','Berlin','Brandenburg','Bremen','Hamburg','Hessen','Mecklenburg-Vorpommern','Niedersachsen',
'Nordrhein-Westfalen', 'Rheinland-Pfalz','Saarland','Sachsen','Sachsen-Anhalt','Schleswig-Holstein','Thüringen']
Causes =['Insects', "Wind/Storm", "Snow/Ice","Drought",'Total'] #"unknown",
Tree_types =['Oak and red oak', 'Beech and other deciduous wood','Pine and larch','Spruce, fir,Douglas fir and other coniferous trees','Total']
# Pick State / damage / Tree_type
state_filter = widgets.Select(description='Pick State', options=States, value="Nordrhein-Westfalen",style=dict(font_style='italic', font_weight='bold'),rows=10)
state_filter.description_width=500
cause_filter = widgets.Select(description='Pick cause of damage', options=Causes, value="Insects", description_tooltip='Pick a cause of tree damage', )
cause_filter.description_width=500
Tree_filter = widgets.Select(description='Pick Tree type', options= Tree_types,description_width=500, value="Spruce, fir,Douglas fir and other coniferous trees" ,style=dict(
font_style='italic', font_weight='bold'))
Tree_filter.description_width=500
#out2 = widgets.Output()
The cause of damage are the totals of cuttings in 1000 m³.
pd.set_option("mode.chained_assignment", None)
def f(a, b, c):
print('{} & {} & {}'.format(a, b, c)) #
data= df.copy().drop(columns="Owner", axis=1)
dfB = data[ (data.loc[:,"Tree_type"]== Tree_filter.value)& (data.loc[:,"Cause"]==cause_filter.value )& (data.loc[:,"German State"]== state_filter.value )]
dfB = dfB.loc[:, ["Year","Value"]]
dfB = dfB.set_index("Year")
#fig, ax = plt.subplots(1,1, )
dfB.plot(y="Value", figsize=(9,5 ),title=(str(Tree_filter.value)+" in "+str(state_filter.value)),ylabel="1000 m³", grid=True,fontsize=12) #
return dfB
out = widgets.interactive_output(f, {'a': Tree_filter, 'b': cause_filter, 'c': state_filter}) # 'd':out2
widgets.HBox([widgets.VBox([ Tree_filter, cause_filter, state_filter ]), out ], width=1600) # display( out2)
Tree_filter.value
#fig, ax = plt.subplots(1,1, )
data=df.copy()
df2 =data[ (data["Tree_type"]== "Total")& (data["Cause"]== "Total" )] # ["German State","Value"]
df2 =df2 [ (df2["German State"]!= "Berlin")& (df2["German State"]!= "Hamburg" )]
df2.Value =df2.Value.astype( "float32")
df2 =df2.loc[:, ["Year","German State","Value"]]
#df2 = df2.set_index(["Year","German State"]) #
#df2 = df2.sort_index()
fig= px.line( df2,x="Year",y="Value",color="German State",log_y=True,markers=True,symbol="German State",template="seaborn",) #linewidth=1 barmode="group",
fig.update_layout(title='Total timber cuttings per German state', #xaxis_title='Month',
yaxis_title='$1000 m³$)')
fig.show()