plotnine.geoms.geom_line

class plotnine.geoms.geom_line(mapping=None, data=None, **kwargs)[source]

Connected points

Usage

geom_line(mapping=None, data=None, stat='identity', position='identity',
          na_rm=False, inherit_aes=True, show_legend=None, arrow=None,
          lineend='butt', linejoin='round', **kwargs)

Only the mapping and data can be positional, the rest must be keyword arguments. **kwargs can be aesthetics (or parameters) used by the stat.

Parameters
mappingaes, optional

Aesthetic mappings created with aes(). If specified and inherit.aes=True, it is combined with the default mapping for the plot. You must supply mapping if there is no plot mapping.

Aesthetic

Default value

x

y

alpha

1

color

'black'

group

linetype

'solid'

size

0.5

The bold aesthetics are required.

datadataframe, optional

The data to be displayed in this layer. If None, the data from from the ggplot() call is used. If specified, it overrides the data from the ggplot() call.

statstr or stat, optional (default: stat_identity)

The statistical transformation to use on the data for this layer. If it is a string, it must be the registered and known to Plotnine.

positionstr or position, optional (default: position_identity)

Position adjustment. If it is a string, it must be registered and known to Plotnine.

na_rmbool, optional (default: False)

If False, removes missing values with a warning. If True silently removes missing values.

inherit_aesbool, optional (default: True)

If False, overrides the default aesthetics.

show_legendbool or dict, optional (default: None)

Whether this layer should be included in the legends. None the default, includes any aesthetics that are mapped. If a bool, False never includes and True always includes. A dict can be used to exclude specific aesthetis of the layer from showing in the legend. e.g show_legend={'color': False}, any other aesthetic are included by default.

See also

plotnine.geoms.geom_path

For documentation of other parameters.

Examples

[1]:
import pandas as pd
import numpy as np

from plotnine import *
from plotnine.data import *

%matplotlib inline

Line plots

geom_line() connects the dots, and is useful for time series data.

[2]:
economics.head()
[2]:
date pce pop psavert uempmed unemploy
0 1967-07-01 507.4 198712 12.5 4.5 2944
1 1967-08-01 510.5 198911 12.5 4.7 2945
2 1967-09-01 516.3 199113 11.7 4.6 2958
3 1967-10-01 512.9 199311 12.5 4.9 3143
4 1967-11-01 518.1 199498 12.5 4.7 3066
[3]:
(
    ggplot(economics, aes(x='date', y='uempmed'))
    + geom_line() # line plot
    + labs(x='date', y='median duration of unemployment')
)
../_images/geom_line_3_0.png
[3]:
<ggplot: (-9223372029847151335)>

You can put arrows at the end of a line:

[4]:
(
    ggplot(economics, aes(x='date', y='pop'))
    + geom_line(arrow=arrow()) # add an arrow to the end of the line
    + labs(x='date', y='total population (,000)')
)
../_images/geom_line_5_0.png
[4]:
<ggplot: (-9223372029844498516)>

The arrow can be modified for a different look:

[5]:
(
    ggplot(economics, aes(x='date', y='pop'))
    + geom_line(arrow=arrow(angle=35,      # defines the shape of the arrow head
                              ends="both",   # input what end to put the arrow on
                              type="closed", # defines arrow head type
                             ))
    + labs(x='date', y='total population (,000)')
)
../_images/geom_line_7_0.png
[5]:
<ggplot: (7007807983)>

You can change the look of the line:

[6]:
(
    ggplot(economics, aes(x='date', y='uempmed'))
    + geom_line(color='pink',        # set line colour
                  size=7,            # set line thickness
                  linetype='dashed'  # set line type
               )
    + labs(x='date', y='median duration of unemployment')
)

../_images/geom_line_9_0.png
[6]:
<ggplot: (-9223372029846967836)>