Performing Experiments in Python
Luke Hayden
Instructor
Data
How do we get answers?
Approach
Variable types
Mapping
fill
or color
argumentsCall ggplot()
function and give it a DataFrame
Assign mapping of variables with aes()
Specify a geometry
import plotnine as p9
(p9.ggplot([pandas DataFrame])+
p9.aes(
x='variable to put on X-axis',
y='variable to put on Y-axis',
color='variable ')+
p9.geom_point()
)
geom_point()
import plotnine as p9
import pandas as pd
df = pd.DataFrame(data= {'Sex': ["Male", "Male", "Female","Female"] ,
"Height (cm)": [183, 179, 160, 172],
"Weight (kg)": [82,75.1, 50, 58.7]})
print(p9.ggplot(df)+ p9.aes(x='Height (cm)',y='Weight (kg)', color='Sex')+ p9.geom_point())
geom_boxplot()
import plotnine as p9
import pandas as pd
df = pd.DataFrame(data= {'Sex': ["Male", "Male","Male", "Male","Male", "Male",
"Female","Female", "Female","Female", "Female","Female"] ,
"Height": [183, 179, 190, 181, 170, 175,
160, 165, 158, 154, 170, 160]})
(p9.ggplot(df)+ p9.aes(x='Sex',y='Height', fill='Sex')+ p9.geom_boxplot())
geom_density()
import plotnine as p9
import pandas as pd
df = pd.DataFrame(data= {'Sex': ["Male", "Male","Male", "Male","Male", "Male",
"Female","Female", "Female","Female", "Female","Female"] ,
"Height": [183, 179, 190, 181, 170, 175,
160, 165, 158, 154, 170, 160]})
(p9.ggplot(df)+ p9.aes(x='Height', fill='Sex') + p9.geom_density(alpha=0.5))
Performing Experiments in Python