首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Plotnine :如何删除geom_line()中行周围的图例框

Plotnine :如何删除geom_line()中行周围的图例框
EN

Stack Overflow用户
提问于 2021-11-14 06:32:52
回答 1查看 108关注 0票数 1

考虑下面的plotnine图。如何删除图例中线条周围的伪框(在屏幕截图中圈出)。这些伪框不会出现在ggplot中。

我已经看了主题中的所有选项,但没有一个能做到这一点…https://plotnine.readthedocs.io/en/stable/generated/plotnine.themes.theme.html

代码语言:javascript
复制
import numpy as np
import pandas as pd
from plotnine import *


df = pd.DataFrame({
    'date':pd.date_range('1/1/2000', freq='A', periods=20),
    'a': np.random.uniform(0.01,0.03,20),
    'b': np.random.uniform(0.02,0.04,20),
})

df = pd.melt(df, id_vars=['date'])


p = (ggplot(df,aes(x='date',y='value',color='variable'))
  + theme_light()
  + geom_line(size=1.15)
  + labs(x=None, y=None)
  + scale_x_date(expand=(0,0), breaks=pd.date_range(start='2001-1-1', end='2019-1-1', periods=10), labels=lambda l: [v.strftime("%Y") for v in l])
  + scale_color_manual(('#50C878','#F75394'))
  + theme(
      legend_title=element_blank(),
      legend_direction='horizontal',
      legend_position='bottom',
      legend_box_spacing=0.25,
      legend_background=element_blank(),
      panel_grid_minor = element_blank(),
      panel_grid_major_x = element_blank(),
      panel_border = element_blank(),
  )
)
p

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-16 21:05:09

实现所需结果的一种方法是通过颜色(`legend_key=element_rect=“白色”)将图例键的颜色设置为“白色”,或者更一般地设置为背景颜色:

代码语言:javascript
复制
import numpy as np
import pandas as pd
from plotnine import *


df = pd.DataFrame({
    'date':pd.date_range('1/1/2000', freq='A', periods=20),
    'a': np.random.uniform(0.01,0.03,20),
    'b': np.random.uniform(0.02,0.04,20),
})
df = pd.melt(df, id_vars=['date'])

p = (ggplot(df,aes(x='date',y='value',color='variable'))
  + theme_light()
  + geom_line(size=1.15)
  + labs(x=None, y=None)
  + scale_x_date(expand=(0,0), breaks=pd.date_range(start='2001-1-1', end='2019-1-1', periods=10), labels=lambda l: [v.strftime("%Y") for v in l])
  + scale_color_manual(('#50C878','#F75394'))
  + 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(),
      panel_grid_minor = element_blank(),
      panel_grid_major_x = element_blank(),
      panel_border = element_blank()
  )
)
p

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69960796

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档