4
$\begingroup$

I am trying to plot a box plot with seaborn with the following code

plot = sns.boxplot(
    y='Min PPI distance',
    x='Synergy_percent',
    color='white',
    orient="h",
    data=synergy_df
)
plot.set(
    xlabel='Synergy (%)',
    ylabel='Min. distance',
);

The data synergy_df looks like

    Combination Min PPI distance    Synergy_percent
0   1003- Camptothecin & 1011- Navitoclax   2.0 22.2
1   1003- Camptothecin & 1017- Olaparib 2.0 6.4
2   1003- Camptothecin & 1022- AZD7762  2.0 40.4
3   1003- Camptothecin & 1025- SB216763 3.0 2.1
4   1003- Camptothecin & 1032- Afatinib 3.0 2.2

After running the above code I am getting the error TypeError: Horizontal orientation requires numeric `x` variable. I want to add that Synergy_percent column has many zero values and about 1500 rows; I haven't tried it with a smaller number of rows.

I think the reason for the error is the wide range in the synergy_percent values. The range is between 0 to 86.7. If I use xlim here what range should I use to divide the x-axis in 4 quarters (0-25-50-100)?

Background Seaborn is a Python library for more complex graph plotting than the commonly used matplotlib and is often used for boxplots and heatmaps usually using a pandas dataframe. The official documents are described here.

$\endgroup$
0

2 Answers 2

3
$\begingroup$

Very quickly because I'm not around.

I think I know what's going on, but don't have enough time to figure the df. The key thing is seaborn needs to know where columns are x and which are y ... within the data frame . This information appears missing and the OP used y='title', x='title without reference to the dataframe. I'm pretty certain thats the bug here.

import seaborn as sns
import matplotlib.pyplot as plt

plot = sns.boxplot(
    color='white',
    y=synergy_df["Synergy_percent"], 
    x=synergy_df["Min PPI distance"] 
);
plt.show()

Alternative, which seems more what the OP wants ...

import seaborn as sns
import matplotlib.pyplot as plt

plot = sns.boxplot(
    color='white',
    # use your orientation command here
    x=synergy_df["Synergy_percent"], 
    y=synergy_df["Min PPI distance"] 
);
plt.show()

Blurb Normally the x-axis is the discrete boundary whilst the y-axis is continuous. In you example the x-axis feels like the continuous variable and you are producing a rotated boxplot - thats perfectly fine and seabourn will do that however its good to get it working first before rotating the axes.

Final The orientation of the dataframe might possibly be an issue, not sure unless its tried.


If there's a formatting issue the only trick I know is

 plot = sis.boxplot(
 .... # same old
 dodge=True,
 );

I don't know beyond, e.g. imposing percentile ranges onto a boxplot.

$\endgroup$
2
  • 1
    $\begingroup$ Well its good its working. Could you try dodge=True. ?? What I think you want is to tweak the 'box and whiskers' of the boxplot - which I'd have to look up. The other possibility (which I don't think you want) is to make 4 boxplots within the range specified. This is last point is about pandas filtering and would be outside seaborn. I'm pretty certain you just need a simple tweak within boxplot method. However, beyond the dodge trick I dunno, but dodge does clean stuff up. $\endgroup$
    – M__
    Apr 18, 2022 at 14:27
  • 2
    $\begingroup$ Good to know; I guess that means data=X doesn't have the same effect in seaborn as it does in R. $\endgroup$
    – gringer
    Apr 19, 2022 at 3:19
1
$\begingroup$

I'm not familiar with seaborn, but I'd guess from the error that there's at least one text value inside the Synergy_percent field that is encouraging the rest of the values to text. Either that, or spaces in the input file are messing up the parsing of the columns.

$\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.