gpt4 book ai didi

python - 如何反转 seaborn 图形级图的轴 (FacetGrid)

转载 作者:行者123 更新时间:2023-12-05 04:45:09 27 4
gpt4 key购买 nike

我想在 Facetgrid 的每个图中反转 y 轴。

下面是代码的简化示例:

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

df_test = pd.DataFrame({'a':['yes', 'no']*5, 'b':np.arange(10), 'c':np.arange(10)*2})
plt.figure()
g = sns.FacetGrid(data=df_test, row='a')
g.map_dataframe(sns.scatterplot, y='c', x='b')
plt.show()

enter image description here

据我所知,这通常是在使用 matplotlib 时使用 ax.invert_yaxis() 完成的,所以我尝试通过 g.axes 访问它但没有运气。

我知道我可以手动设置 ylim=(max_val, min_val),但是,这会导致不美观的刻度间距。

最佳答案

  • 通过遍历 g.axes
  • 提取并设置每个轴
  • python 3.8.11matplotlib 3.4.3seaborn 0.11.2 中测试
g = sns.FacetGrid(data=df_test, row='a')
g.map_dataframe(sns.scatterplot, y='c', x='b')

for ax in g.axes[0]:
ax.invert_yaxis()
g = sns.relplot(data=df_test, row='a', x='b', y='c', kind='scatter', height=3)
for ax in g.axes[0]:
ax.invert_yaxis()

enter image description here

  • 使用 .ravel() 来展平 n x n 数组或 axes,其中 n > 1<
df_test = pd.DataFrame({'a':['yes', 'no', 'maybe']*4, 'b':np.arange(12), 'c':np.arange(12)*2, 'd': np.arange(12)*3})

g = sns.relplot(data=df_test, col='a', col_wrap=2, x='b', y='c', kind='scatter', height=3)
for ax in g.axes.ravel():
ax.invert_yaxis()

enter image description here

关于python - 如何反转 seaborn 图形级图的轴 (FacetGrid),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69213504/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com