1

I have data in a simple pandas table with two columns: depth and a datum. There are multiple samples per depth. Using seaborn's relplot, I can produce a nice plot of the data using:

import seaborn as sns
sns.relplot(x='depth', y='datum', ci='sd', kind='line', data=myData)

This works as expected. However, it makes a lot more sense for depth to be on the y axis, because that represents the earth more faithfully. If I tell seaborn to swap the axes like this:

sns.relplot(y='depth', x='datum', ci='sd', kind='line', data=myData)

it does not work, of course, because the standard deviation is computed with respect to the x-axis. Is there a way to swap the axes, yet compute and plot the standard deviation with respect to what is now the y-axis?

Fadecomic
  • 1,220
  • 1
  • 10
  • 23
  • There is a method for swapping based on line graphs at [How to switch axes in matplotlib?](https://stackoverflow.com/a/50746552/5358968), perhaps that answer could be extended. – Steve Mar 26 '19 at 16:48

1 Answers1

0

I found a solution. I copied the relevant bits from seaborn's source and altered them.

grouped = myData['myColumn'].groupby(myData['depth'])
est = grouped.agg("mean")
sd = grouped.std()
cis = pd.DataFrame(np.c_[est - sd, est + sd], index=est.index, columns=["low", "high"]).stack()
if cis.notnull().any():
    cis = cis.unstack().reindex(est.index)
else:
    cis = None
x = est.index
y = est
y_ci = cis
x, y = np.asarray(x), np.asarray(y)
low, high = np.asarray(y_ci["low"]), np.asarray(y_ci["high"])
fig = plt.figure(figsize=(8, 8))
ax = fig.gca()
plt.plot(y, x)
plt.fill_betweenx(x, low, high, alpha=0.2)
plt.gca().invert_yaxis()
plt.xlabel('My Column [units]')
plt.ylabel('Depth [m]')

Some of the variables are a bit redundant, but that was to avoid altering seaborn's code and introducing inadvertent bugs.

Fadecomic
  • 1,220
  • 1
  • 10
  • 23