python - 改变情节中的图例

标签 python ggplot2 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

最佳答案

我们制作一些看起来像您的数据的东西:

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=' ')
)

enter image description here

关于python - 改变情节中的图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59735876/

相关文章:

r - ggplot : overlay two plots

ggplot2 - ggplot aes : color vs group with time series data missing

python - Plotnine 的比例填充和轴位置

python - 如何在 python 中获取 32 位 IEEE 值的整数值

r - 在 R 中,如何在使用双花括号时选择列?为什么不能将 $ 运算符与大括号一起使用?

python - 什么类用于货币表示?

r - 无法在Ubuntu 11.10上安装ggp​​lot2

python-3.x - 将回归线方程和 R 方添加到 PLOTNINE

python - 无法在 Microsoft Azure 上运行 Dash 应用程序

python - 从 PIL 导入图像 - ImportError : No module named PIL