Python Seaborn-将x轴移动到顶部
我正在绘制以下数据集Python Seaborn-将x轴移动到顶部,python,matplotlib,seaborn,Python,Matplotlib,Seaborn,我正在绘制以下数据集 data = {'Surface':[0, -50, -100, -250, -600], 'Left':[0, 0, 0, 10, 50], 'Front':[0, 0, 5, 15, 90]} 这是一个负值数据集,因此我尝试将x轴移动到绘图的顶部,而不是正常的底部轴 现在的情节是这样的: 数据集和代码如下所示: import seaborn as sns import pandas as pd import matplotlib.pyplot as plt #
data = {'Surface':[0, -50, -100, -250, -600], 'Left':[0, 0, 0, 10, 50], 'Front':[0, 0, 5, 15, 90]}
这是一个负值数据集,因此我尝试将x轴移动到绘图的顶部,而不是正常的底部轴
现在的情节是这样的:
数据集和代码如下所示:
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")
我知道matplotlib中有一个选项,但在seaborn中尝试它会导致错误
AttributeError: 'PairGrid' object has no attribute 'xaxis'
我不确定这是最干净的方式,但它是有效的:
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)
如上所述,链接文章的可能重复并不能解决问题。它的语法完全相同。您只需要分别拾取每个轴。检查下面的答案=)非常感谢。我不知道您需要单独指定轴
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)