-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGraphs C and N
41 lines (36 loc) · 1.57 KB
/
Graphs C and N
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
import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline # needed in jupyter notebook to view plots
import seaborn as sns
#prepare data
# file created in "Calculations: Mineral soils data 2018"
soils=pd.read_csv("2018_treat_CNsums.csv",
header = 0)
# file created in "Forest floor calculations"
ff=pd.read_csv("2018FF_treat_CNsums.csv",
header = 0)
#combine csv files into one dataframe
df1 = [soils, ff]
df2 = pd.concat(df1)
# sum carbon and nitrogen amounts by treatment and soil type
df_sum =pd.DataFrame(df2
.groupby(['year', 'ws', 'treatm', 's_type'])
.agg(
C_sum=pd.NamedAgg(column="C_Mg_ha_sum", aggfunc="sum"),
N_sum=pd.NamedAgg(column="N_Mg_ha_sum", aggfunc="sum")
)
)
df_sum=df_sum.reset_index(drop=False) #reset index created by grouping
#set up two blank plots; one row and two columns
fig, axes=plt.subplots(1,2, figsize=(12,6))
# Plot graphs: axes position 0 = 1st row and 1st column; 1 2nd row and 1st column
g1=sns.barplot(ax=axes[0], data=df_sum, x="treatm", y="C_sum", palette="mako",hue="s_type",)
g1.set(ylabel="Total Carbon (Mg/ha)", xlabel="")
g1.get_legend().remove() #remove the legend from this plot so it doesn't appear in both
g2=sns.barplot(ax=axes[1], data=df_sum, x="treatm", y="N_sum", palette="mako",hue="s_type",)
g2.set(ylabel="Total Nitrogen (Mg/ha)", xlabel="")
g2.legend(loc="upper left",title="") #specify where legend is placed
#export graph as png
plt.savefig('CNsumsTreat.png')