python - 在 python 中使用 seaborn 绘图复制绘图

标签 python python-3.x matplotlib seaborn

我正在编写一个脚本来生成 python3 中学生 GPA 的多个绘图图。以下是数据集。

Roll No            GPA 1    GPA 2   GPA 3   GPA 4   GPA 5   GPA 6
BE/5746/07          7.82    8.29    8.53    8.76    9.31    8.88
BE/CIVIL/5777/07    7.18    7.29    7.82    8.24    8.94    8.69
BE/CIVIL/5577/07    7.29    7.41    8.18    8.18    8.75    7.63
BE/CIVIL/5753/07    7.29    7.24    7.47    7.65    8.13    8
BE/5782/07          6.88    7.41    7.41    7.59    8.25    7.75
BE/CIVIL/5524/07    6.53    6.82    7.33    7.22    8.06    7.75
BE/5763/07          6.65    6.94    7.35    6.76    8.47    6.94
BE/CIVIL/5780/07    6.82    7.33    7.06    7.06    8.31    6.78
BE/CIVIL/5760/07    6.47    7       6.88    6.39    7.59    6.56
BE/CIVIL/5772/07    6.17    7.06    7.23    6.31    6.76    6.56
BE/CIVIL/5787/07    6.24    6.53    6.35    6.35    7       6.87
BE/CIVIL/5758/07    6.11    6.71    6.47    6.35    7.06    6.25

由于一些次要原因,许多地 block 只是被复制,而我得到的是不同行的相同地 block 。我正在使用删除“/”的卷号名称保存图像。我使用的代码是 -

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style = "darkgrid")

raw_data = pd.read_csv("student alumni data.csv")

raw_data['img_name'] = raw_data['Roll No'].apply(lambda x: str(x).replace("/", "") + ".png")
i = 0
plt.ylim(4, 10)

for index, rows in raw_data.iterrows():
    x_list = ['GPA 1', 'GPA 2', 'GPA 3', 'GPA 4', 'GPA 5', 'GPA 6']
    y_list = [rows['GPA 1'], rows['GPA 2'], rows['GPA 3'], rows['GPA 4'], rows['GPA 5'], rows['GPA 6']]

    sns.set(style = "darkgrid")
    sns_plot = sns.barplot(x=x_list, y=y_list, palette="Blues_d").set_title("GPA's of all semesters")
    sns_plot = sns_plot.get_figure()
    sns_plot.savefig("img/" + str(rows['img_name']))
    print("Done fig : ", i)
    del sns_plot, x_list, y_list
    i += 1

请帮我找出错误

最佳答案

所以我认为问题在于情节每次都被覆盖,并且它们都在相互策划。

如果你改变行:

del sns_plot, x_list, y_list

收件人:

plt.clf()    

它应该会在您写入图像后清除内存中的绘图,并且应该会为您提供所需的结果。

del 行是不必要的,因为所有这些变量都在循环范围内,因此它们将在每次循环迭代后被删除。

关于python - 在 python 中使用 seaborn 绘图复制绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51571625/

相关文章:

python - 我们如何从Python中的glyph id获取unicode?

python - 在 python 中 list.extend 的替代方案/更快的方法?

处理范围检查和列表的 Python 3 float 错误

matplotlib - 如何在图例中的同一标签下添加条形和线条?

python - 大量数据的散点图

python - 从 pyspark 中的 Dataframe 方法内访问广播字典

python - 如何迭代和展平一个 groupby 值中的每一列

python - 使用python递归打印星号

python - 从 Python 2.7 转换到 3.4 时出现 jsonify 问题

python - 为什么 sns.lmplot 和 FacetGrid+plt.scatter 从相同的数据创建不同的散点?