Seaborn: lineplot: rotate fig in 90° (plot x as a function of y)

Created on 30 Jan 2019  ·  4Comments  ·  Source: mwaskom/seaborn

Adding an optiion to rotate a seaborn.lineplot so that the result will be as a function of y and not a function of x.

For example, this code:

    import pandas as pd
    import seaborn as sns
    df = pd.DataFrame([[0,1],[0,2],[0,1.5],[1,1],[1,5]], columns=['group','val'])
    sns.lineplot(x='group',y='val',data=df)

Create this figure:

enter image description here

But as far as I understand there is no way to rotate the figure in 90° ? so that in the X we will have "val" and in Y we will have "group" and the std will go from left to right and not from bottom to up.

I assume something like

sns.lineplot(x='group',y='val',data=df, orientation='vertica') #orient : “v” | “h”, optional

will be great.

See also: https://stackoverflow.com/questions/54445044/how-to-rotate-a-seaborn-lineplot/54448926#54448926
Thanks

Most helpful comment

I understand that currently it is not possible? but can't you reopen the ticket and mark is a feature request? Thanks

All 4 comments

Nope, it's not possible, sorry.

I understand that currently it is not possible? but can't you reopen the ticket and mark is a feature request? Thanks

No, sorry.

@orena1 If you encounter this need again in the future...

from matplotlib import pyplot, transforms
from matplotlib.transforms import Affine2D
from matplotlib.collections import PathCollection
import pandas as pd
import seaborn as sns

def lineplot_plusplus(orientation = "horizontal", **kwargs):
    line = sns.lineplot(**kwargs)

    r = Affine2D().scale(sx=1, sy=-1).rotate_deg(90)
    for x in line.images + line.lines + line.collections:
        trans = x.get_transform()
        x.set_transform(r+trans)
        if isinstance(x, PathCollection):
            transoff = x.get_offset_transform()
            x._transOffset = r+transoff

    old = line.axis()
    line.axis(old[2:4] + old[0:2])
    xlabel = line.get_xlabel()
    line.set_xlabel(line.get_ylabel())
    line.set_ylabel(xlabel)

    return line

df = pd.DataFrame([[0,1],[0,2],[0,1.5],[1,1],[1,5]], columns=['group','val'])
lineplot_plusplus(x='group',y='val',data=df, orientation = "vertical")

Credit to: https://stackoverflow.com/a/43915452/1825043

Was this page helpful?
0 / 5 - 0 ratings

Related issues

songololo picture songololo  ·  4Comments

stonebig picture stonebig  ·  4Comments

chanshing picture chanshing  ·  3Comments

TDaltonC picture TDaltonC  ·  3Comments

btyukodi picture btyukodi  ·  3Comments