模块' plotnine'没有属性' ggplot_build'

发布于 2025-01-22 22:17:39 字数 253 浏览 3 评论 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”,color ='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]]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔少女心 2025-01-29 22:17:39

plotnine中没有ggplot_build,并且不需要。我认为您正在尝试绘制回归线。一个示例如下。

另外,se ='f'将不起作用。如果您不想显示置信区间,则是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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文