DATASET_URL = 'https://gist.githubusercontent.com/ZeccaLehn/4e06d2575eb9589dbe8c365d61cb056c/raw/64f1660f38ef523b2a1a13be77b002b98665cdfe/mtcars.csv'
mtcars = pd.read_csv(DATASET_URL).rename(columns={'Unnamed: 0': 'brand'})

mtcars.head()

"""
Explanation: Plotnine
plotnine is an implementation of a grammar of graphics in Python, it is based on ggplot2. The grammar allows users to compose plots by explicitly mapping data to the visual objects that make up the plot. - Plotnine Official Website

Plotting with a grammar is powerful, it makes custom (and otherwise complex) plots are easy to think about and then create, while the simple plots remain simple.

Let's take a create a quick example using the mtcars dataset.
End of explanation
"""


(gg.ggplot(mtcars, gg.aes('disp', 'mpg', color='factor(gear)'))
 + gg.geom_point()
 + gg.geom_smooth(method="lm")
 + gg.facet_wrap("~gear"))

"""
Explanation: Scatter Plot
End of explanation
"""


plt.scatter(mtcars['disp'], mtcars['mpg'])
plt.xlabel('Disp')
plt.ylabel('MPG')
plt.title('Disp vs MPG')
plt.show()

"""
Explanation: The equivalent in plain matplotlib would be
End of explanation
"""


(gg.ggplot(mtcars, gg.aes(y='hp', x='factor(gear)'))
 + gg.geom_boxplot())

"""
Explanation: Box Plot
Let's build a boxplot, and see the distribution of horspower on wether a car is automatic or manual
End of explanation
"""


auto_tmission = mtcars.loc[lambda df: df['am'] == 0].hp
man_tmission = mtcars.loc[lambda df: df['am'] == 1].hp

fig, axes = plt.subplots()
axes.set(xticklabels=['0', '1',], xlabel='am', ylabel='hp')
axes.boxplot([auto_tmission, man_tmission])

plt.show()

"""
Explanation: The matplotlib equivalent
End of explanation
"""


(gg.ggplot(mtcars, gg.aes(x='brand', y='wt'))
    + gg.geom_bar(stat='identity'))


"""
Explanation: Bar Chart
Let's plot the weight of each brand
End of explanation
"""


plt.bar(mtcars['brand'], mtcars['wt'])
plt.xlabel('Brand')
plt.ylabel('wt')
plt.title('Brand by weight')
plt.show()

"""
Explanation: The matplotlib equivalent
End of explanation
"""


(gg.ggplot(mtcars, gg.aes(x='brand', y='wt'))
    + gg.geom_bar(stat='identity')
    + gg.coord_flip())

(gg.ggplot(mtcars, gg.aes(x='gear', y='hp', fill="factor(brand)"))
    + gg.geom_bar(stat='identity', color="black")
    + gg.coord_flip()
    + gg.theme_xkcd())

plt.barh(mtcars['brand'], mtcars['wt'])
plt.xlabel('Brand')
plt.ylabel('wt')
plt.title('Brand by weight')
plt.show()

"""
Explanation: The brands are all quished together, let's try making the bar graph horizontal
End of explanation
"""


(gg.ggplot(mtcars, gg.aes(x='wt'))
    + gg.geom_histogram(bins=10, fill="blue"))

plt.hist(mtcars['wt'], 10)
plt.xlabel('history')
plt.ylabel('wt')
plt.title('Histogram')
plt.show()

"""
Explanation: Histogram
Let's look at the distribution of weight for all the cars
End of explanation
"""


