0
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]]
2
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Apr 21, 2022 at 20:57
  • The error is self-explanatory. There is no "ggplot_build". What are you trying to achieve? Commented May 6, 2022 at 10:46

1 Answer 1

0

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

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.