vrjr vrjr - 1 month ago 6
Python Question

Plot each column of Pandas dataframe pairwise against one column

I have a pandas dataframe where one of the columns is a set of labels that I would like to plot each of the other columns against in subplots. In other words, I want the y-axis of each subplot to use the same column, called 'labels', and I want a subplot for each of the remaining columns with the data from each column on the x-axis. I expected the following code snippet to achieve this, but I don't understand why this results in a single nonsensical plot:

examples.plot(subplots=True, layout=(-1, 3), figsize=(20, 20), y='labels', sharey=False)

Answer

The problem with that code is that you didn't specify an x value. It seems nonsensical because it's plotting the labels column against an index from 0 to the number of rows. As far as I know, you can't do what you want in pandas directly. You might want to check out seaborn though, it's another visualization library that has some nice grid plotting helpers.

Here's an example with your data:

import pandas as pd
import seaborn as sns
import numpy as np

examples = pd.DataFrame(np.random.rand(10,4), columns=['a', 'b', 'c', 'labels'])
g = sns.PairGrid(examples, x_vars=['a', 'b', 'c'], y_vars='labels')    
g = g.map(plt.plot)

This creates the following plot: enter image description here

Obviously it doesn't look great with random data, but hopefully with your data it will look better.

Comments