我有一个关于情节图例中的图例的问题。
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")))
我预计图例将只有一条黄线,带有标签总体平均值。可以改吗?
最佳答案
我们制作一些看起来像您的数据的东西:
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()
您将总体平均值
作为一列,因此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=' ')
)
关于python - 改变情节中的图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59735876/