3
$\begingroup$

I am using the below code to get the bar plot. I want to get the bar plot with error bars displayed.

import seaborn as sns

def plot_coefficients(target, coeffs_df, n=10):
    if coeffs_df.shape[0]==0:
        return None # nothing to plot here!
    data = coeffs_df.copy()
    data = data.sort_values(('Coefficient','mean'), ascending=False).head(n)
    g = sns.barplot(
        data=data,
        y='Gene',
        x=('Coefficient','mean'),
        hue='Feature Type',
        hue_order=[
            h for h in [
                'Genomic Variant',
                'Gene Expression',
                'Proteomics',
                'Methylation',
            ] 
        ],
        dodge=False
    )
    g.set(
        xlim=(0,1),
        xlabel='Importance [0,1]',
        ylabel='',
        title=f"Top {target} predictor features"
    )
    return g

Coefficient here is a score generated from predictive modelling that ranges from 0 to 1. 2. data here is entire data and I created a mean column. Ideally I wanted to calculate it in seaborn plot rather than creating a mean column separately before.

$\endgroup$
2
  • 1
    $\begingroup$ I presume you import seaborn as sns, right? $\endgroup$ Aug 1, 2022 at 9:51
  • $\begingroup$ @KamilSJaron Yes $\endgroup$
    – Megha
    Aug 1, 2022 at 10:37

1 Answer 1

1
$\begingroup$

I did put forward a comment, it was removed I think the idea is its a full post or zero. The problem proving a negative is tricky.

I don't think its doable within the exact system specified, i.e. a Seaborn bar chart. Again I could be wrong.

If the OP went to a Seaborn dot line formation - that would work sns.barplot is changed to sns.pointplot but then all the nice colouring disappears. Standard deviation is one error range if all the values are present: but I think the OP will need to request it (.sd() ?? can't remember).

In matplotlib there is an error bar method where the coder specifies the error bars and that is plt.errorbar. An example from the official docs is here. Thus call a bar chart and then add the error bars onto it.

Summary The reasons I don't think its doable in Seaborn:

  1. I'm pretty certain Seaborn has no independent errorbar method unlike matplotlib
  2. Seaborn can do error bars given the full data set, but those are created automatically for a given method. Maybe there is a "sns.barplot-errorbar" method but I don't know it.

The Seaborn approach does carry the advantage of not having to work out the error bar independently.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.