Download notebook (.ipynb)

Line vs. Path#

import numpy as np
import pandas as pd

from lets_plot import *
LetsPlot.setup_html()
np.random.seed(42)

x = np.random.uniform(1, 50, size=40)

data = pd.DataFrame({'x': x, 'y': np.sin(x)})
data.head()
x y
0 19.352466 0.481977
1 47.585001 -0.444944
2 36.867703 -0.738881
3 30.334266 -0.882739
4 8.644913 0.703183

geom_line() connects points in order of the variable on the x-axis.

ggplot(data) + geom_point(aes(x='x', y='y', color='x'), alpha=0.7, size=4) + \
    scale_color_discrete() + \
    theme(legend_position='none') + \
    geom_line(aes(x='x', y='y'), linetype=3)

geom_path() connects observations in the order how they appear in data.

ggplot(data) + geom_point(aes(x='x', y='y', color='x'), alpha=0.7, size=4) + \
    scale_color_discrete() + \
    theme(legend_position='none') + \
    geom_path(aes(x='x', y='y'), size=0.7, linetype='dotted')

Another example to demonstrate the difference between geom_path() and geom_line().

a = [5, 1, 1, 5, 5, 2, 2, 4, 4, 3]
b = [5, 5, 1, 1, 4, 4, 2, 2, 3, 3]

snail = pd.DataFrame({'x': a, 'y': b})
ggplot() + geom_path(data=snail, mapping=aes(x='x', y='y'), size=2, alpha=0.7)
ggplot() + geom_line(data=snail, mapping=aes(x='x', y='y'), size=2, alpha=0.7)