Adding and changing titles on plotnine charts¶
In [18]:
                Copied!
                
                
            import pandas as pd
from plotnine import *
df = pd.read_csv('countries.csv')
df.head()
import pandas as pd
from plotnine import *
df = pd.read_csv('countries.csv')
df.head()
    
        Out[18]:
| country | continent | gdp_per_capita | life_expectancy | population | |
|---|---|---|---|---|---|
| 0 | Afghanistan | Asia | 663 | 54.863 | 22856302 | 
| 1 | Albania | Europe | 4195 | 74.200 | 3071856 | 
| 2 | Algeria | Africa | 5098 | 68.963 | 30533827 | 
| 3 | Angola | Africa | 2446 | 45.234 | 13926373 | 
| 4 | Antigua and Barbuda | N. America | 12738 | 73.544 | 77656 | 
Adding a title¶
In [19]:
                Copied!
                
                
            (
    ggplot(df)
    + aes(x='gdp_per_capita', y='life_expectancy')
    + geom_point()
    + labs(title="Life Expectancy vs GDP (per capita)")
)
(
    ggplot(df)
    + aes(x='gdp_per_capita', y='life_expectancy')
    + geom_point()
    + labs(title="Life Expectancy vs GDP (per capita)")
)
    
        Out[19]:
<ggplot: (318906405)>
Left-aligning the title¶
In [20]:
                Copied!
                
                
            (
    ggplot(df)
    + aes(x='gdp_per_capita', y='life_expectancy')
    + geom_point()
    + labs(title="Life Expectancy vs GDP (per capita)")
    + theme(plot_title=element_text(ha='left', x=0.125))
)
(
    ggplot(df)
    + aes(x='gdp_per_capita', y='life_expectancy')
    + geom_point()
    + labs(title="Life Expectancy vs GDP (per capita)")
    + theme(plot_title=element_text(ha='left', x=0.125))
)
    
        Out[20]:
<ggplot: (319249936)>
Changing the title size¶
In [21]:
                Copied!
                
                
            (
    ggplot(df)
    + aes(x='gdp_per_capita', y='life_expectancy')
    + geom_point()
    + labs(title="Life Expectancy vs GDP (per capita)")
    + theme(plot_title=element_text(size=20))
)
(
    ggplot(df)
    + aes(x='gdp_per_capita', y='life_expectancy')
    + geom_point()
    + labs(title="Life Expectancy vs GDP (per capita)")
    + theme(plot_title=element_text(size=20))
)
    
        Out[21]:
<ggplot: (319084075)>
In [ ]:
                Copied!