p3 = (p9.ggplot(data = df, mapping = p9.aes(x = 'gross_power', y = 'temp_drop_a',show_legend=False, alpha = 0.7))
+ p9.geom_jitter()
+ p9.geom_smooth(se= 'F',method="lm", colour = 'red'))
mygg_data = p9.ggplot_build(p3).data[[2]]
p3 = (p9.ggplot(data = df, mapping = p9.aes(x = 'gross_power', y = 'temp_drop_a',show_legend=False, alpha = 0.7))
+ p9.geom_jitter()
+ p9.geom_smooth(se= 'F',method="lm", colour = 'red'))
mygg_data = p9.ggplot_build(p3).data[[2]]
There is no ggplot_build
in plotnine
and it is not needed. I assume you are trying to plot a regression line. An example is below.
Also se= 'F'
will not work. If you do not want to display confidence intervals, it is se= False
.
import pandas as pd
from scipy import stats
from plotnine import *
from plotnine.data import mpg as df
df = df[df['cyl']!=5]
def regline(cyl):
data = df[df['cyl']==cyl]
params = stats.linregress(data['displ'],data['hwy'])
if params[0] < 0:
return 'y = {:.4f} - {:.4f} x'.format(params[1],abs(params[0]))
else:
return 'y = {:.4f} + {:.4f} x'.format(params[1],params[0])
regline4 = regline(4)
regline6 = regline(6)
regline8 = regline(8)
p = (ggplot(df, aes(x='displ', y='hwy',color='factor(cyl)'))
+ theme_light(8)
+ geom_jitter(size=0.8, alpha=0.4)
+ geom_smooth(aes(fill='factor(cyl)'),method='lm',alpha=0.3)
+ annotate('text', label=regline4, x=2.05, y=33.5, size=7, color='#005D17', ha='left')
+ annotate('text', label=regline6, x=3.95, y=28, size=7, color='#7E191B', ha='left')
+ annotate('text', label=regline8, x=6.5, y=21.5, size=7, color='#1C2E4A')
+ labs(x='Displacement', y='Miles per gallon (highway)')
+ scale_color_manual(('#005D17','#7E191B','#1C2E4A'))
+ scale_fill_manual(('#50C878','#E78587','#89CFF0'))
+ theme(
legend_title=element_blank(),
legend_key=element_rect(color='white'),
legend_direction='horizontal',
legend_position='bottom',
legend_box_spacing=0.25,
legend_background=element_blank(),
)
)
p