Skip to content

Avoiding interpolation for missing values for geom_line #540

Closed Answered by has2k1
v-- asked this question in Q&A
Discussion options

You must be logged in to vote

geom_line connects lines that belong to the same group. So you have to manipulate the data such that lines in the same streak are in the same group.

import plotnine as p9
import pandas as pd

df = pd.DataFrame(
    data=[(x, x ** 2) for x in range(10) if x < 3 or x > 6],
    columns=['x', 'y']
)

# By walking along the difference between two consecutive values, we can
# categorise the streaks i.e. whenever the difference is not 1 then we have
# a new group.
group = []
g = 0
for d in df['x'].diff():
    if d != 1:
        g += 1   
    group.append(g)
    
df['group'] = group

p9.ggplot(df) + p9.aes(x='x', y='y', group='group') + p9.geom_line()

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@v--
Comment options

@has2k1
Comment options

Answer selected by v--
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants