pandasplotplotlyseabornpython-ggplot

Simple Method to Plot Columns in Pandas Dataframe on Different rows of plot?


Is there a simple method to plot all columns on different rows/ tiles, with a shared X-axis? Ido not want to go deep into matplotlib subplots for each new figure; I'm looking for something simple that allows me to view all data in the dataframe easily. I feel like there is a simple flag or option in Pandas or Seaborn I'm missing.

A simple dataframe.plot() in pandas gives all variables stacked: enter image description here

I want a simple approach (not many lines of matplotlib figure building) that creates a new facet for each variable (column) in the dataframe, with separate Y-axes, but shared X-axes.

Maybe something I'm overlooking in ggplot2?

Goal - something like this: enter image description here


Solution

  • Like this?

    df.plot(subplots=True, layout=(4,1))
    

    It seems to generate exactly what you wanted.

    If you want the labels to be outside of the plot, you can do some handling after the df.plot:

    fig = plt.figure(figsize=(14,8))
    ax = fig.add_subplot(111)
    df = pd.DataFrame(np.random.uniform(size=(20,4)))
    df.plot(ax=ax, subplots=True, layout=(4,1)) # ax=ax points df.plot to fig
    for each in fig.axes: # You can still modify these axes!
        each.legend(loc='center left', bbox_to_anchor=(1, 0.5))
    

    This will set the legend outside of the plot to the right, in the same process like you would set any other legend.