0

Stackoverflow community,

I currently have a problem using plotnine in python. My code is the following:

from plotnine import *
from plotnine.data import mpg
%matplotlib inline


plot = (ggplot(result) +
aes(x = 'age', y = 'fa index', color='ID') + 

geom_point(size = 2) +
geom_path() + 

stat_smooth(method='lm') + 
aes(group = 'Trained_yung') + #line of best fit by group

ylab("FA index") +
xlab("Age (days)") +
theme_bw())
plot.save('filename.pdf', height=8, width=8)

"result" being the pandas dataframe with my results.

The resulting graph gave me almost the expected graph except that all points are linked together for a reason that I ignore. I didn't find a way to remove that.

If remove stat_smooth:

plot = (ggplot(result) +
aes(x = 'age', y = 'fa index', color='ID') + 

geom_point(size = 2) +
geom_path() + 

ylab("FA index") +
xlab("Age (days)") +
theme_bw())
plot.save('filename.pdf', height=8, width=8)

I have got:

so I deduced that stat_smooth added these weird links between my points

Clément
  • 1
  • 1

1 Answers1

0

https://github.com/has2k1/plotnine/issues/480

stat_smooth does not link any points together. The problem is that you are adding another aesthetic mapping!

stat_smooth(method='lm') + aes(group = 'Trained_yung') + #line of best fit by group

a good script is:

import pandas as pd
import numpy as np
from pandas.api.types import CategoricalDtype
from plotnine import *
from plotnine.data import mpg
%matplotlib inline


plot = (ggplot(result) +
aes(x = 'age', y = 'fa index', color='ID') + 

geom_point(size = 2) +
geom_path() + 

stat_smooth(method='lm', group = 'Trained_yung') + 

ylab("FA index") +
xlab("Age (days)") +
theme_bw())
plot.save('filename3.pdf', height=8, width=8)
Clément
  • 1
  • 1