如何避免在网格图中使用 seaborn 进行自动排序? (python)
how to avoid automatic sorting with seaborn in a grid plot? (python)
我正在尝试绘制我拥有的一些数据,但我正在为 seaborn 的自动排序而苦苦挣扎。正如您在下图中看到的,每个图表的 x 轴都有不同的排序,我希望它们相同,[Melhorou、Piorou、Indiferente]。有谁知道如何做到这一点?
这是我到目前为止所做的代码。
f = plt.figure()
ax = f.add_subplot(2, 2, 1)
sns.despine(f)
sns.histplot(data = data , x = "A pandemia afetou a sua performance acadêmica? [1o sem de 2020]", multiple="dodge", palette = "Blues", ax = ax);
plt.xlabel("1o sem 2020")
ax = f.add_subplot(2, 2, 2)
sns.despine(f)
sns.histplot(data = data , x = "A pandemia afetou a sua performance acadêmica? [2o sem de 2020]", multiple="dodge", palette = "Blues", ax = ax);
plt.xlabel("2o sem 2020")
ax = f.add_subplot(2, 2, 3)
sns.despine(f)
sns.histplot(data = data , x = "A pandemia afetou a sua performance acadêmica? [1o sem de 2021]", multiple="dodge", palette = "Blues", ax = ax);
plt.xlabel("1o sem 2021")
ax = f.add_subplot(2, 2, 4)
sns.despine(f)
sns.histplot(data = data , x = "A pandemia afetou a sua performance acadêmica? [2o sem de 2021]", multiple="dodge", palette = "Blues", ax = ax);
plt.xlabel("2o sem 2021")
默认情况下,值在列中出现的顺序决定 histplot
的 x 轴顺序。
您可以使数据框的列明确化,并以这种方式强制排序。
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
column_names_long = ["A pandemia afetou a sua performance acadêmica? [1o sem de 2020]",
"A pandemia afetou a sua performance acadêmica? [2o sem de 2020]",
"A pandemia afetou a sua performance acadêmica? [1o sem de 2021]",
"A pandemia afetou a sua performance acadêmica? [2o sem de 2021]"]
column_names_short = ["1o sem de 2020", "2o sem de 2020", "1o sem de 2021", "2o sem de 2021"]
affectings = ['Melhorou', 'Piorou', 'Indiferente']
data = pd.DataFrame({c: np.random.choice(affectings, 100) for c in column_names_long})
# rename the columns, so seaborn can show them directly
data = data.rename(columns={long: short for long, short in zip(column_names_long, column_names_short)})
for column_name in column_names_short:
data[column_name] = pd.Categorical(data[column_name], affectings) # fix an order on each of the columns
fig, axs = plt.subplots(ncols=2, nrows=2, figsize=(12, 7))
for ax, column_name in zip(axs.flatten(), column_names_short):
sns.histplot(data=data, x=column_name, color="dodgerblue", ax=ax)
sns.despine(fig)
plt.tight_layout() # fit the subplots and their labels nicely
plt.show()
或者,您可以将 histplot
替换为 countplot
,它接受 order=
参数并允许您为条形分配不同的颜色:
for ax, column_name in zip(axs.flatten(), column_names_short):
sns.countplot(data=data, x=column_name, order=affectings,
palette=["dodgerblue", "crimson", "orange"], ax=ax)
我正在尝试绘制我拥有的一些数据,但我正在为 seaborn 的自动排序而苦苦挣扎。正如您在下图中看到的,每个图表的 x 轴都有不同的排序,我希望它们相同,[Melhorou、Piorou、Indiferente]。有谁知道如何做到这一点?
这是我到目前为止所做的代码。
f = plt.figure()
ax = f.add_subplot(2, 2, 1)
sns.despine(f)
sns.histplot(data = data , x = "A pandemia afetou a sua performance acadêmica? [1o sem de 2020]", multiple="dodge", palette = "Blues", ax = ax);
plt.xlabel("1o sem 2020")
ax = f.add_subplot(2, 2, 2)
sns.despine(f)
sns.histplot(data = data , x = "A pandemia afetou a sua performance acadêmica? [2o sem de 2020]", multiple="dodge", palette = "Blues", ax = ax);
plt.xlabel("2o sem 2020")
ax = f.add_subplot(2, 2, 3)
sns.despine(f)
sns.histplot(data = data , x = "A pandemia afetou a sua performance acadêmica? [1o sem de 2021]", multiple="dodge", palette = "Blues", ax = ax);
plt.xlabel("1o sem 2021")
ax = f.add_subplot(2, 2, 4)
sns.despine(f)
sns.histplot(data = data , x = "A pandemia afetou a sua performance acadêmica? [2o sem de 2021]", multiple="dodge", palette = "Blues", ax = ax);
plt.xlabel("2o sem 2021")
默认情况下,值在列中出现的顺序决定 histplot
的 x 轴顺序。
您可以使数据框的列明确化,并以这种方式强制排序。
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
column_names_long = ["A pandemia afetou a sua performance acadêmica? [1o sem de 2020]",
"A pandemia afetou a sua performance acadêmica? [2o sem de 2020]",
"A pandemia afetou a sua performance acadêmica? [1o sem de 2021]",
"A pandemia afetou a sua performance acadêmica? [2o sem de 2021]"]
column_names_short = ["1o sem de 2020", "2o sem de 2020", "1o sem de 2021", "2o sem de 2021"]
affectings = ['Melhorou', 'Piorou', 'Indiferente']
data = pd.DataFrame({c: np.random.choice(affectings, 100) for c in column_names_long})
# rename the columns, so seaborn can show them directly
data = data.rename(columns={long: short for long, short in zip(column_names_long, column_names_short)})
for column_name in column_names_short:
data[column_name] = pd.Categorical(data[column_name], affectings) # fix an order on each of the columns
fig, axs = plt.subplots(ncols=2, nrows=2, figsize=(12, 7))
for ax, column_name in zip(axs.flatten(), column_names_short):
sns.histplot(data=data, x=column_name, color="dodgerblue", ax=ax)
sns.despine(fig)
plt.tight_layout() # fit the subplots and their labels nicely
plt.show()
或者,您可以将 histplot
替换为 countplot
,它接受 order=
参数并允许您为条形分配不同的颜色:
for ax, column_name in zip(axs.flatten(), column_names_short):
sns.countplot(data=data, x=column_name, order=affectings,
palette=["dodgerblue", "crimson", "orange"], ax=ax)