为什么图表的标题和 x-label 都一样,即使我已经将它们包含在 for 循环中?
Why the title and the x-label for the charts are all the same even I already included them in the for loop?
我希望条形图有自己的标题和 x-axis 标签,所以我在 for 循环中包含了 plt.title
和 plt.xlabel
。
但是,在我 运行 代码之后,两个图表的标题和 x-axis 标签是相同的。第一张图的标题应为 Histogram of gender
,第二张图的标题应为 Histogram of job
。我的代码有什么问题,或者我哪里做错了,尤其是循环?
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import pandas as pd
from scipy import stats
# first data is age
# 2nd data is gender
# third data is saving
# 4th data is job
data = np.array([[11, "male",1222,"teacher"],[23,"female",333,"student"],
[15,"male",542,"security"],[23,"male",4422,"farmer"],[25,"female",553,"farmer"],
[22, "male", 221, "teacher"],[27, "male", 333, "agent"],[11, "female", 33, "farmer"]])
data_feature_names = ["age","gender","saving","job"]
# type of the data above
types = ["num","cat","num","cat"]
idx2 = []
for index, _type in enumerate(types):
if _type == 'cat':
idx2.append(index)
# Order of x axis label
ss = [["female","male"],["farmer","agent","security","teacher","student"]]
for k in range(0,len(ss)):
for j in idx2:
pandasdf = pd.DataFrame(data)
sns.countplot(x=j, data=pandasdf, order = ss[k])
plt.title("Histogram of " + data_feature_names[j])
plt.xlabel(data_feature_names[j])
plt.show()
您在 ss
中的排序和 idx2
中的列名是成对的,因此您可以使用一个循环并最终得到性别和工作直方图的所需结果(但在这种情况下您最终不会得到年龄或储蓄的直方图)。您示例的最后几行将变为:
for k, j in zip(range(0, len(ss)), idx2):
pandasdf = pd.DataFrame(data)
sns.countplot(x=j, data=pandasdf, order=ss[k])
plt.title("Histogram of " + data_feature_names[j])
plt.xlabel(data_feature_names[j])
plt.show()
有几种方法可以简化此过程,从而更容易调试。例如,您可以使用列表理解来缩短 idx2
的循环:
idx2 = [ix for ix, f in enumerate(types) if f == "cat"]
我在下面提供了一个额外的示例,代码行更少,但对原始脚本进行了更多修改。
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
data = pd.DataFrame(
[
[11, "male", 1222, "teacher"],
[23, "female", 333, "student"],
[15, "male", 542, "security"],
[23, "male", 4422, "farmer"],
[25, "female", 553, "farmer"],
[22, "male", 221, "teacher"],
[27, "male", 333, "agent"],
[11, "female", 33, "farmer"],
],
columns=["age", "gender", "saving", "job"],
)
ordering = {
"gender": ["female", "male"],
"job": ["farmer", "agent", "security", "teacher", "student"],
}
for column in ['gender', 'job']:
ax = sns.countplot(x=column, data=data, order=ordering.get(column, None))
ax.set_title("Histogram of {}".format(column))
ax.set_xlabel(column)
plt.show()
我希望条形图有自己的标题和 x-axis 标签,所以我在 for 循环中包含了 plt.title
和 plt.xlabel
。
但是,在我 运行 代码之后,两个图表的标题和 x-axis 标签是相同的。第一张图的标题应为 Histogram of gender
,第二张图的标题应为 Histogram of job
。我的代码有什么问题,或者我哪里做错了,尤其是循环?
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import pandas as pd
from scipy import stats
# first data is age
# 2nd data is gender
# third data is saving
# 4th data is job
data = np.array([[11, "male",1222,"teacher"],[23,"female",333,"student"],
[15,"male",542,"security"],[23,"male",4422,"farmer"],[25,"female",553,"farmer"],
[22, "male", 221, "teacher"],[27, "male", 333, "agent"],[11, "female", 33, "farmer"]])
data_feature_names = ["age","gender","saving","job"]
# type of the data above
types = ["num","cat","num","cat"]
idx2 = []
for index, _type in enumerate(types):
if _type == 'cat':
idx2.append(index)
# Order of x axis label
ss = [["female","male"],["farmer","agent","security","teacher","student"]]
for k in range(0,len(ss)):
for j in idx2:
pandasdf = pd.DataFrame(data)
sns.countplot(x=j, data=pandasdf, order = ss[k])
plt.title("Histogram of " + data_feature_names[j])
plt.xlabel(data_feature_names[j])
plt.show()
您在 ss
中的排序和 idx2
中的列名是成对的,因此您可以使用一个循环并最终得到性别和工作直方图的所需结果(但在这种情况下您最终不会得到年龄或储蓄的直方图)。您示例的最后几行将变为:
for k, j in zip(range(0, len(ss)), idx2):
pandasdf = pd.DataFrame(data)
sns.countplot(x=j, data=pandasdf, order=ss[k])
plt.title("Histogram of " + data_feature_names[j])
plt.xlabel(data_feature_names[j])
plt.show()
有几种方法可以简化此过程,从而更容易调试。例如,您可以使用列表理解来缩短 idx2
的循环:
idx2 = [ix for ix, f in enumerate(types) if f == "cat"]
我在下面提供了一个额外的示例,代码行更少,但对原始脚本进行了更多修改。
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
data = pd.DataFrame(
[
[11, "male", 1222, "teacher"],
[23, "female", 333, "student"],
[15, "male", 542, "security"],
[23, "male", 4422, "farmer"],
[25, "female", 553, "farmer"],
[22, "male", 221, "teacher"],
[27, "male", 333, "agent"],
[11, "female", 33, "farmer"],
],
columns=["age", "gender", "saving", "job"],
)
ordering = {
"gender": ["female", "male"],
"job": ["farmer", "agent", "security", "teacher", "student"],
}
for column in ['gender', 'job']:
ax = sns.countplot(x=column, data=data, order=ordering.get(column, None))
ax.set_title("Histogram of {}".format(column))
ax.set_xlabel(column)
plt.show()