Plotnine:如何删除 geom_line() 中线条周围的图例框
Plotnine : How to remove legend boxes around lines in geom_line()
考虑以下 plotnine
情节。如何删除图例中线条周围的伪框(在屏幕截图中圈出)。这些伪框不会出现在 ggplot
.
中
我已经查看了主题中的所有选项,但是 none 成功了...
https://plotnine.readthedocs.io/en/stable/generated/plotnine.themes.theme.html
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
实现您想要的结果的一个选项是将图例键的颜色设置为“白色”或更普遍的背景颜色,通过`legend_key=element_rect(color = "white "):
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
考虑以下 plotnine
情节。如何删除图例中线条周围的伪框(在屏幕截图中圈出)。这些伪框不会出现在 ggplot
.
我已经查看了主题中的所有选项,但是 none 成功了... https://plotnine.readthedocs.io/en/stable/generated/plotnine.themes.theme.html
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
实现您想要的结果的一个选项是将图例键的颜色设置为“白色”或更普遍的背景颜色,通过`legend_key=element_rect(color = "white "):
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