在plotnine中更改图例

3

我有一个关于 plotnine 绘图中图例的问题。

import pandas as pd
import numpy as np
from pandas.api.types import CategoricalDtype
from plotnine import *
from plotnine.data import mpg
%matplotlib inline


c= pd.read_excel("cenpv.xlsx")
c.head()


dodge_text = position_dodge(width=0.9)

(ggplot(c, aes(x='exon', y='mean'))
 + geom_bar(stat='identity', position='dodge', show_legend=False)
 + geom_text(aes(label='percentage'),                                   
             position=dodge_text,
             size=8, va='bottom', format_string='{}%')
 + geom_hline(aes(yintercept = "Overall mean", color="Overall mean")))

我希望传说只有一个黄色的线和标签“总体均值”。是否可以更改它?

plot in plotnine

1个回答

2
我们制作出类似于您的数据的东西:
c = pd.DataFrame({'exon':['CENPV_'+str(i+1) for i in range(5)],
                 'mean':np.random.poisson(100,5),
                 'percentage':np.random.randint(low=10,high=100,size=5)})
c['Overall mean'] = c['mean'].mean()

你有一个名为overall mean的列,因此ggplot2(或plotnine)将其解释为连续值范围以绘制颜色。
你需要做的是提供一个均值数组和一个颜色列表:
dodge_text = position_dodge(width=0.9)

(ggplot(c, aes(x='exon', y='mean'))
 + geom_bar(stat='identity', position='dodge', show_legend=False)
 + geom_text(aes(label='percentage'),                                   
             position=dodge_text,
             size=8, va='bottom', format_string='{}%')
 + geom_hline(aes(yintercept = c['mean'].mean(), color=["Overall mean"]))
 + scale_color_manual(values="yellow",name=' ')
)

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接