2

I am writing a script to generate multiple plot figures of GPAs of student in python3. Following is the dataset.

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

For some minor reason many plots are simply getting copied and I am getting same plots for different rows. I am saving the images with the roll number name removing "/" .The code that I am using is -

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

Please help me out finding the bug

1 Answer 1

1

So I think that the problem is that the plot is being overwritten each time and they're all getting plotted over each other.

If you change the line:

del sns_plot, x_list, y_list

To:

plt.clf()    

It should clear out the plot in memory after you have written the image and should give you the desired result.

The del line is unnecessary as all of those variables are within scope of the loop so they will be deleted after each iteration of the loop.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.