Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)
edited by

For the project, I have written the code where the student can input his name, admission number, math, physics, and chemistry score. Now I want the bar graph to compare his score to the highest scorer. I have defined the list (l_math, l_chem, and l_phy) which is the list of all scores of all the students. I even defined max scores as the max_math, max_chem, and max_phy...

I used pandas and tried changing the code:

import random

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

s = "Crime Type Summer|Crime Type Winter".split("|")

# Generate dummy data into a dataframe

j = {x: [random.choice(["ASB", "Violence", "Theft", "Public Order", "Drugs"]

                   ) for j in range(300)] for x in s}

df = pd.DataFrame(j)

index = np.arange(5)

bar_width = 0.35

fig, ax = plt.subplots()

summer = ax.bar(index, df["Crime Type Summer"].value_counts(), bar_width,

            label="Summer")

winter = ax.bar(index+bar_width, df["Crime Type Winter"].value_counts(),

             bar_width, label="Winter")

ax.set_xlabel('Category')

ax.set_ylabel('Incidence')

ax.set_title('Crime incidence by season, type')

ax.set_xticks(index + bar_width / 2)

ax.set_xticklabels(["ASB", "Violence", "Theft", "Public Order", "Drugs"])

ax.legend()

plt.show()

But I don't know how to modify the above code to use it for my needs.

1 Answer

0 votes
by (36.8k points)
edited by

You just need to modifying the code you posted, the below code will help you solve the error.

import random

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

# assuming math, chem, physics ()

student_scores = [9, 1.5, 4.6]

max_score = [9.1, 8.5, 7.8]

df = pd.DataFrame({

    'student_scores': student_scores,

    'max_score': max_score

})

index = np.arange(len(df))

bar_width = 0.35

fig, ax = plt.subplots()

summer = ax.bar(index, df["student_scores"], bar_width, label="Student scores")

winter = ax.bar(index+bar_width, df["max_score"], bar_width, label="Max Scores")

ax.set_xlabel('Course')

ax.set_ylabel('Score')

ax.set_title('Current student vs Max score')

ax.set_xticks(index + bar_width / 2)

ax.set_xticklabels(["Math", "Chem", "Physics"])

ax.legend()

plt.show()

Make sure the order in both score lists should be same.

Learn Python for Data Science Course know more about Python for Data Science.

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...