-
I have a daily time series and I want geom_line to only connect two data points if they belong to the same streak of days, i.e. if there is no day between them with missing data. This Stack Overflow question discusses something similar for ggplot2. Is there a way to achieve this using plotnine? For example, is there a way to adapt the following code to produce a plot that does not attempt to connect 2 and 7? 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']
)
p9.ggplot(df) + p9.aes(x='x', y='y') + p9.geom_line() |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
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() |
Beta Was this translation helpful? Give feedback.
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.