plotnine is an implementation of a grammar of graphics in Python based on ggplot2.

Code
import plotnine
print(plotnine.__version__)
Code
from plotnine import *

print(plotnine.__version__)
Code
from plotnine import *
import seaborn as sns
import pandas as pd


# Apply the default theme


# Load an example dataset
tips = sns.load_dataset("tips")
tips.head()

1 Scatter Plot

Code
p=(
    ggplot(data=tips)+aes(x="tip",y="total_bill")+ geom_point()
)

p

1.1 color by group

Code
p=(
    ggplot(data=tips)+aes(x="tip",y="total_bill")+ geom_point(aes(color="sex"))
)

p

1.2 size by group

Code
p=(
    ggplot(data=tips)+aes(x="tip",y="total_bill",size="size")+ geom_point()
)

p

2 line Plot

Code
dowjones= sns.load_dataset("dowjones")
dowjones.head()
Code
p=(
    ggplot(data=dowjones)+aes(x="Date",y="Price")+ geom_line()
)

p
Code
import random
from siuba import _, mutate, filter, group_by, summarize,show_query
from siuba import *

dowjones2=dowjones>>mutate(type='old')

dowjones3=dowjones>>mutate(Price=_.Price+random.random()*200,type='new')

dowjones4=pd.concat([dowjones2, dowjones3], ignore_index = True)>> arrange(_.Date)
Code
dowjones4.head()

2.1 color by group

Code
p=(
    ggplot(data=dowjones4)+aes(x="Date",y="Price")+ geom_line(aes(color="type"))
)

p

3 histogram

Code
p=(
    ggplot(data=tips)+aes(x="tip")+ geom_histogram()
)

p

3.1 color by group

Code
p=(
    ggplot(data=tips)+aes(x="tip",fill = 'sex')+ geom_histogram(position = 'dodge')
)

p

4 bar chart

Code
p=(
    ggplot(data=tips)+aes(x='sex',y='tip',fill="sex")+geom_col()
)

p

5 box plot

Code
p=(
    ggplot(data=tips)+aes(x='day',y='tip',fill="day")+geom_boxplot()
)

p

5.1 color by group

Code
p=(
    ggplot(data=tips)+aes(x='day',y='tip',fill="sex")+geom_boxplot()
)

p

6 strip plot

Code
p=(
    ggplot(data=tips)+aes(x='day',y='tip')+geom_jitter(width=0.1)
)

p

6.1 color by group

Code
p=(
    ggplot(data=tips)+aes(x='day',y='tip',fill="sex")+geom_jitter(position=position_jitterdodge())
)

p

7 Facet plot

Code
p=(
    ggplot(data=tips)+aes(x="tip",y="total_bill")+ geom_point(aes(color="sex"))
    + facet_wrap("day")
)

p

make 3 plot per row

Code
p=(
    ggplot(data=tips)+aes(x="tip",y="total_bill")+ geom_point(aes(color="sex"))
    + facet_wrap("day",ncol = 3)
)

p

8 title,size, x y names

8.1 add title

Code
p=(
    ggplot(data=tips)+aes(x="tip",fill = 'sex')+ geom_histogram(position = 'dodge')+ ggtitle("tip by sex")
)

p

8.2 adjust size

Code
p=(
    ggplot(data=tips)+aes(x="tip",fill = 'sex')+ geom_histogram(position = 'dodge')+ ggtitle("tip by sex")+ theme(figure_size=(4, 3)) 
)

p

8.3 change x y name

Code
p=(
    ggplot(data=tips)+aes(x="tip",y="total_bill")+ geom_point()+ scale_x_continuous(name="new x name")+ scale_y_continuous(name="new y name")
)

p

9 applying themes

find all build in themes in https://github.com/has2k1/plotnine/tree/main/plotnine/themes

10 Save plot

Code
p=(
    ggplot(data=tips)+aes(x="tip",fill = 'sex')+ geom_histogram(position = 'dodge')+ theme_dark()
)

p.save(filename = 'test3.png')

11 Animation plot

Code
from plotnine.animation import PlotnineAnimation
Code
new_data=dowjones4.sample(50, random_state=42)

new_data=new_data.sort_values(by=['Date'], ascending=True)

#write a function that creates all the plots
def plot(x):
    df2 = dowjones4.query('Date <= @x')

    p = (ggplot(df2,
               aes(x = 'Date', y = 'Price'))
         + geom_line(aes(color="type"))
         # Specify the limits for the x and y aesthetics
         #+ scale_x_continuous(limits=(dowjones4.Date.min(), dowjones4.Date.max()))
         #+ scale_y_continuous(limits=(dowjones4.Price.min(), dowjones4.Price.max()))
         + theme(subplots_adjust={'right': 0.85}) # Make space for the legend
        )
    return(p)


plots = (plot(i) for i in (new_data["Date"]))
Code
from matplotlib import rc

rc("animation", html="html5")

#create the animation
animation = PlotnineAnimation(plots, interval=300, repeat_delay=500)
animation

12 reference:

https://plotnine.org/

Back to top