mwaskom / seaborn

Statistical data visualization in Python
https://seaborn.pydata.org
BSD 3-Clause "New" or "Revised" License
12.55k stars 1.92k forks source link

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

Closed orena1 closed 5 years ago

orena1 commented 5 years ago

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

mwaskom commented 5 years ago

Nope, it's not possible, sorry.

orena1 commented 5 years ago

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

mwaskom commented 5 years ago

No, sorry.

christian-oreilly commented 4 years ago

@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

orena1 commented 3 years ago

Thanks @christian-oreilly !