3

I am plotting the following data set.

data = {'Surface':[0, -50, -100, -250, -600], 'Left':[0, 0, 0, 10, 50], 'Front':[0, 0, 5, 15, 90]} 

This is a negative value dataset and therfore I am trying to move the x-axis to the top of the plot instead of the normal bottom axis.

The plot looks like this now:

enter image description here

The dataset and code below:

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt

# initialise dataframe 
data = {'Surface':[0, -50, -100, -250, -600], 'Left':[0, 0, 0, 10, 50], 'Front':[0, 0, 5, 15, 90]} 

# Creates pandas DataFrame. 
df = pd.DataFrame(data)

#Plotting
g = sns.PairGrid(df, y_vars=["Surface"], x_vars=["Left", "Front"], height=4)
g.map(plt.scatter, color=".3")
g.map(sns.lineplot)

#Move X Axis to top
g.invert_yaxis()
g.xaxis.set_ticks_position("top")

I know that there is an option in matplotlib, but trying it in seaborn it gives an error with

AttributeError: 'PairGrid' object has no attribute 'xaxis'
Grisuu
  • 109
  • 2
  • 8
  • Possible duplicate of [Moving x-axis to the top of a plot in matplotlib](https://stackoverflow.com/questions/14406214/moving-x-axis-to-the-top-of-a-plot-in-matplotlib) – CAPSLOCK Nov 25 '19 at 13:04
  • The linked article does not solve the problem, as explained above. – Grisuu Nov 25 '19 at 13:13
  • Well, it is exactly the same syntax. You just need to pick each axis separately. Check the answer below =) – CAPSLOCK Nov 25 '19 at 13:15

2 Answers2

3
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt

# initialise dataframe 
data = {'Surface':[0, -50, -100, -250, -600], 'Left':[0, 0, 0, 10, 50], 'Front':[0, 0, 5, 15, 90]} 

# Creates pandas DataFrame. 
df = pd.DataFrame(data)

#Plotting
g = sns.PairGrid(df, y_vars=["Surface"], x_vars=["Left", "Front"], height=4)
g.map(plt.scatter, color=".3")
g.map(sns.lineplot)

#Move X Axis to top
#g.invert_yaxis()
g.axes[0,1].xaxis.set_ticks_position("top")

g.axes[0,0].xaxis.set_ticks_position("top")
CAPSLOCK
  • 6,243
  • 3
  • 33
  • 56
  • 2
    great thanks. I didn't know that you need to specify the axes separate – Grisuu Nov 25 '19 at 13:18
  • How about moving the x-axis zero point to the center of the chart? To make negative values more visible – lloydyu24 May 28 '22 at 06:28
  • 1
    @lloydyu24 OP has negative values only and only on the y-axis. If your comment is not a suggestion but a different question, then you should open a new one. – CAPSLOCK May 30 '22 at 07:08
  • I was asking for my problem. Anyways, I solved it by adding a .set_ylim() to my code. – lloydyu24 May 30 '22 at 14:59
0

I'm not sure this is the cleanest way, but it works:

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt

# initialise dataframe 
data = {'Surface':[0, -50, -100, -250, -600], 'Left':[0, 0, 0, 10, 50], 'Front':[0, 0, 5, 15, 90]} 

# Creates pandas DataFrame. 
df = pd.DataFrame(data)

#Plotting
g = sns.PairGrid(df, y_vars=["Surface"], x_vars=["Left", "Front"], height=4)
g.map(plt.scatter, color=".3")
g.map(sns.lineplot)

#Move X Axis to top
g.axes[0][0].invert_yaxis()
for ax in g.axes[0]:
    ax.xaxis.tick_top()
    ax.xaxis.set_label_position('top')
    ax.spines['bottom'].set_visible(False)
    ax.spines['top'].set_visible(True)

enter image description here

Andrea
  • 2,932
  • 11
  • 23