gpt4 book ai didi

python - seaborn : plotting graph from dataframe in loop not working as expected

转载 作者:太空宇宙 更新时间:2023-11-04 05:04:14 25 4
gpt4 key购买 nike

我正在尝试从数据框中绘制 seaborn 的 tsplot。这是我的代码

df = run_fully_connected_network(20, 200)
# print(df)
for feature in features:
df_feature = df.loc[df['feature'] == feature]
# print(df_feature)
plt.figure()
plt.title(feature)
# plt.plot(figsize=(20,15))
sns.tsplot(time="iteration", unit="feature", condition = "traits", value="count", data=df_feature)

这是数据框如何查找每次迭代 (df_feature):

迭代 1:

      iteration    feature traits proportion  count
1 0 education 1 n/a 4
2 0 education 2 n/a 5
3 0 education 3 n/a 7
4 0 education 4 n/a 4
18 1 education 1 n/a 4
19 1 education 2 n/a 5
20 1 education 3 n/a 8
21 1 education 4 n/a 3
35 2 education 1 n/a 2
36 2 education 2 n/a 7
37 2 education 3 n/a 8
38 2 education 4 n/a 3
51 3 education 1 n/a 1
52 3 education 2 n/a 7
53 3 education 3 n/a 9
54 3 education 4 n/a 3
67 4 education 1 n/a 1
68 4 education 2 n/a 8
69 4 education 3 n/a 8
70 4 education 4 n/a 3
83 5 education 1 n/a 1
84 5 education 2 n/a 7
85 5 education 3 n/a 8
86 5 education 4 n/a 4
99 6 education 1 n/a 2
100 6 education 2 n/a 5
101 6 education 3 n/a 9
102 6 education 4 n/a 4
115 7 education 1 n/a 3
116 7 education 2 n/a 5
... ... ... ... ... ...
1049 170 education 3 n/a 20
1053 171 education 3 n/a 20
1057 172 education 3 n/a 20
1061 173 education 3 n/a 20
1065 174 education 3 n/a 20
1069 175 education 3 n/a 20
1073 176 education 3 n/a 20
1077 177 education 3 n/a 20
1081 178 education 3 n/a 20
1085 179 education 3 n/a 20
1089 180 education 3 n/a 20
1093 181 education 3 n/a 20
1097 182 education 3 n/a 20
1101 183 education 3 n/a 20
1105 184 education 3 n/a 20
1109 185 education 3 n/a 20
1113 186 education 3 n/a 20
1117 187 education 3 n/a 20
1121 188 education 3 n/a 20
1125 189 education 3 n/a 20
1129 190 education 3 n/a 20
1133 191 education 3 n/a 20
1137 192 education 3 n/a 20
1141 193 education 3 n/a 20
1145 194 education 3 n/a 20
1149 195 education 3 n/a 20
1153 196 education 3 n/a 20
1157 197 education 3 n/a 20
1161 198 education 3 n/a 20
1165 199 education 3 n/a 20

迭代 2:

      iteration   feature traits proportion  count
5 0 religion 1 n/a 5
6 0 religion 2 n/a 4
7 0 religion 3 n/a 6
8 0 religion 5 n/a 5
22 1 religion 1 n/a 5
23 1 religion 2 n/a 4
24 1 religion 3 n/a 7
25 1 religion 5 n/a 4
39 2 religion 1 n/a 5
40 2 religion 2 n/a 3
41 2 religion 3 n/a 8
42 2 religion 5 n/a 4
55 3 religion 1 n/a 7
56 3 religion 2 n/a 2
57 3 religion 3 n/a 8
58 3 religion 5 n/a 3
71 4 religion 1 n/a 7
72 4 religion 2 n/a 2
73 4 religion 3 n/a 8
74 4 religion 5 n/a 3
87 5 religion 1 n/a 5
88 5 religion 2 n/a 2
89 5 religion 3 n/a 8
90 5 religion 5 n/a 5
103 6 religion 1 n/a 5
104 6 religion 2 n/a 3
105 6 religion 3 n/a 7
106 6 religion 5 n/a 5
119 7 religion 1 n/a 4
120 7 religion 2 n/a 5
... ... ... ... ... ...
1050 170 religion 3 n/a 20
1054 171 religion 3 n/a 20
1058 172 religion 3 n/a 20
1062 173 religion 3 n/a 20
1066 174 religion 3 n/a 20
1070 175 religion 3 n/a 20
1074 176 religion 3 n/a 20
1078 177 religion 3 n/a 20
1082 178 religion 3 n/a 20
1086 179 religion 3 n/a 20
1090 180 religion 3 n/a 20
1094 181 religion 3 n/a 20
1098 182 religion 3 n/a 20
1102 183 religion 3 n/a 20
1106 184 religion 3 n/a 20
1110 185 religion 3 n/a 20
1114 186 religion 3 n/a 20
1118 187 religion 3 n/a 20
1122 188 religion 3 n/a 20
1126 189 religion 3 n/a 20
1130 190 religion 3 n/a 20
1134 191 religion 3 n/a 20
1138 192 religion 3 n/a 20
1142 193 religion 3 n/a 20
1146 194 religion 3 n/a 20
1150 195 religion 3 n/a 20
1154 196 religion 3 n/a 20
1158 197 religion 3 n/a 20
1162 198 religion 3 n/a 20
1166 199 religion 3 n/a 20

迭代 3:

类似的数据框

所以基本上有 200 次迭代。在 x 轴上,我希望范围从 0 到 200。这就是图表目前的样子

graph 1 religion enter image description here

为什么我在 x 轴上得到不同的范围。每个数据帧的值都在 0-199 之间。所以图表应该有 0-199 的 x 轴值。但是这里每个图都有不同的范围,即使每个数据帧的迭代值相同。

此外,每次运行我都会在 x 轴上得到不同的范围。例如,在宗教的第二次运行中,我得到的 x 轴范围为 0-200。这是一个示例

enter image description here

有什么方法可以控制图表中显示的范围吗?还是我做错了什么?

最佳答案

由于您已经知道“每个数据帧的值都在 0-199 之间”,因此请在迭代后尝试使用 plt.xlim(0, 200)。这是您可以控制 x 轴范围的一种方式。

细节是 seaborn.tsplot() 设置 xlim 使用 min()max()最后一组数据(基于data.groupby(condition, sort=False)),如你所见here .具体来说,您的 4 个图的 x 轴范围分别由“教育特征 4”、“宗教特征 5”、“社会地位特征 4”和“宗教特征 2”控制。我不确定为什么会这样。你可以提出 issue关于它。

关于python - seaborn : plotting graph from dataframe in loop not working as expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44929697/

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