Create a graph comparing grade changes with Python visualizations: color-code score increases/decreases
It can be difficult to compare changes in students' midterm and final grades based on numbers alone. However Visualizationyou can intuitively understand the rise and fall of grades, as shown in the following figure.

In this post, we'll use the PythonThe matplotlibusing the Graphs comparing midterm and final grade changeswith the following characteristics
- If the score is Green when rising, If it goes down, the redto display lines and text.
- Add a vertical dotted line to Grade changes over timein a clean way.
What is the Grade Change Graph?
The Grade Change graph is a visualization that shows the change in scores between specific points in time. Each student's Midterm and final exam scoreswith lines, and display different colors as the score changes to make the data clearer.
Graph Features
- Red: If the score has dropped
- Green: If the score has increased
- On the left and right Name and scoreto make it intuitive.
Python code examples
Below is the matplotlibto implement a grade change graph.
Full code
import matplotlib.pyplot as plt
Set up # student names and grade data
students = ['Tom', 'Richard', 'Anna', 'Emily', 'Grace']
midterm_scores = [75, 70, 65, 60, 45] # midterm grades
final_scores = [55, 40, 75, 70, 65] # final exam grades
Create and size a # graph
fig, ax = plt.subplots(figsize=(10, 6)) Set the # graph size
ax.set_title("Student Exam Scores", fontsize=15) # Set the graph title
Show # midterm exam scores and names (left - set spacing to the left of the vertical dotted line)
ax.scatter([1] * len(midterm_scores), midterm_scores, color='red', s=50, zorder=3)
for i, txt in enumerate(midterm_scores):
color = 'green' if final_scores[i] > midterm_scores[i] else 'red' # green for rising grade, red for falling grade
ax.text(0.75, midterm_scores[i], f"{students[i]} ({txt})", fontsize=10, color=color, va='center', ha='right') # Display names and scores on left side
# Show final exam score and name on the right (right - set spacing to the right of the vertical dotted line)
ax.scatter([2] * len(final_scores), final_scores, color='green', s=50, zorder=3)
for i, txt in enumerate(final_scores):
color = 'green' if final_scores[i] > midterm_scores[i] else 'red' # green for rising grade, red for falling grade
ax.text(2.25, final_scores[i], f"{students[i]} ({txt})", fontsize=10, color=color, va='center', ha='left') # Display names and scores on right side
# Draw a line connecting midterm and final exam scores
for i in range(len(students)):
color = 'green' if final_scores[i] > midterm_scores[i] else 'red' # Green for rising grades, red for falling grades
ax.plot([1, 2], [midterm_scores[i], final_scores[i]], color=color, linewidth=2, zorder=2) # Connect two points with a line
# Add a vertical dotted line (highlighting the midterm and final exam locations)
ax.axvline(x=1, color='gray', linestyle='--', linewidth=1) # Midterm vertical dotted line
ax.axvline(x=2, color='gray', linestyle='--', linewidth=1) # Final Vertical dotted line
Set the # X-axis
ax.set_xticks([1, 2])
ax.set_xticklabels(["Midterm", "Final"], fontsize=12, fontweight='bold') Set # x-axis labels
Remove the # y-axis and scale
ax.yaxis.set_visible(False) Remove the # Y-axis
ax.set_yticks([]) # Remove Y-axis tick marks
Remove # Grade Change Comparison graph borders
ax.spines['left'].set_visible(False) # Remove left border
ax.spines['right'].set_visible(False) # Remove right border
ax.spines['top'].set_visible(False) # remove top border
ax.spines['bottom'].set_visible(False) # remove bottom border
Output the # grade change comparison graph
plt.show()Code brief

1. set up your data
- Save student names and midterm and final exam scores to a list.
2. Show midterm/final exam scores
- Midterm: Dots
x=1and displays the name and score to the left of the vertical dotted line. - Final Exam: Dots
x=2and displays the name and score to the right of the vertical dotted line.
3. Connect the score change line
plot()to connect the scores with a line, Rising is colored green, Down is redto separate them.
4. add a vertical dotted line
ax.axvline()to highlight midterm and final exam locations.
5. Remove the Y-axis and unnecessary elements
- Remove the y-axis and scale to create a cleaner graph.
Resulting graph features
- Leftshows the midterm score and name slightly off the vertical dotted line.
- Rightdisplays your final exam score and name.
- Increased score means Green lines and textand down is Red lines and textto separate them.
- Unnecessary Y-Axisand borders to give the graph a cleaner, more polished look.
Leverage and scale
This visualization shows not only grade changes, but also Sales data, project progress etc. Use to visualize changes between two points in timeYou can. Tweaking the colors and label placement can help you achieve more intuitive results.
Finalize
Now we can leverage Python to create a Graphs to show changes in midterm and final grades at a glancefor your data. Try out the code and apply it to your own data!
There are some unique types of Python visualizations, like this grade change comparison graph, Sleep Pattern Analysis and Python Visualization: Leveraging Data for Healthy Sleep Check out the post. You'll find it interesting!
# Code Explained
1. import the library
import matplotlib.pyplot as plt
- matplotlib.pyplot: A Python library used for data visualization.
- A module that contains all functions for plotting graphs.
2. Set student names and grade data
students = ['Tom', 'Richard', 'Anna', 'Emily', 'Grace'] midterm_scores = [75, 70, 65, 60, 45] # midterm grades final_scores = [55, 40, 75, 70, 65] # final exam grades
- students: A list of names for each student.
- midterm_scores: The students' midterm grades.
- final_scoresThe students' final exam grades.
3. Create a graph and set its size
fig, ax = plt.subplots(figsize=(10, 6)) ax.set_title("Student Exam Scores", fontsize=15)
- plt.subplots(figsize=(10, 6)): Creates a graph area that is 10 wide and 6 tall.
- ax.set_title(): Sets the title at the top of the graph.
fontsize=15: Set the font size to 15.4. display midterm scores and names
ax.scatter([1] * len(midterm_scores), midterm_scores, color='red', s=50, zorder=3)
- ax.scatter(): Visualize midterm scores with dots.
[1] * len(midterm_scores): Mark all points at the x=1 position.midterm_scores: Sets the y-axis position to each student's midterm score.color='red': Sets the color of the dot to red.s=50: Set the size of the dot to 50.zorder=3Sets the dot to be drawn over the line.for i, txt in enumerate(midterm_scores): color = 'green' if final_scores[i] > midterm_scores[i] else 'red' ax.text(0.75, midterm_scores[i], f"{students[i]} ({txt})", fontsize=10, color=color, va='center', ha='right')
- for i, txt in enumerate(midterm_scores): Repeatedly displays each student's midterm score and name.
- color condition:
- If the final exam score is higher than the midterm, the Greenotherwise Redin the text.
- ax.text(): Displays text (student name and score) to the left of the dot.
0.75: The distance to the left of the vertical dotted line (x=1).midterm_scores[i]: aligns the y position of the text to each student's score.f"{students[i]} ({txt})": Displays the student name and score as a string.ha='right': right-aligns the text.va='center': places the text in the vertical center of the dot.5. Display final exam scores and names
ax.scatter([2] * len(final_scores), final_scores, color='green', s=50, zorder=3)
- ax.scatter(): Visualize a final exam score with dots.
[2] * len(final_scores): Mark all points at position x=2.final_scores: Sets the y-axis position to each student's final exam score.color='green': Sets the color of the dot to green.for i, txt in enumerate(final_scores): color = 'green' if final_scores[i] > midterm_scores[i] else 'red' ax.text(2.25, final_scores[i], f"{students[i]} ({txt})", fontsize=10, color=color, va='center', ha='left')
- for i, txt in enumerate(final_scores): Repeats each student's final exam score and name.
- color condition:
- If the final exam score is higher than the midterm, the Greenotherwise Redin the text.
- ax.text(): Displays text (student name and score) to the right of the dot.
2.25: The position to the right of the vertical dotted line (x=2).final_scores[i]: aligns the y position of the text to each student's score.ha='left': Left-aligns text.va='center': places the text in the vertical center of the dot.6. Connect the score change line
for i in range(len(students)): color = 'green' if final_scores[i] > midterm_scores[i] else 'red' ax.plot([1, 2], [midterm_scores[i], final_scores[i]], color=color, linewidth=2, zorder=2)
- for i in range(len(students)): Connect each student's grade change with a line.
- color condition: On score increase Greenand when the score drops Redto the default.
- ax.plot(): Connect two points with a line.
[1, 2]: x-axis position (midterm → final)[midterm_scores[i], final_scores[i]]: Sets the y-axis position to the midterm and final exam scores.linewidth=2: Set the thickness of the line to 2.7. add a vertical dotted line
ax.axvline(x=1, color='gray', linestyle='--', linewidth=1) ax.axvline(x=2, color='gray', linestyle='--', linewidth=1)
- ax.axvline(): Draws a vertical dashed line at a specific location on the x-axis.
x=1: Add dotted lines to midterm locationsx=2: Add a dotted line to the final exam locationcolor='gray': Sets the dotted line color to gray.linestyle='--': Sets the dotted line.linewidth=1: Set the thickness of the dotted line to 1.8. Set up the X-axis
ax.set_xticks([1, 2]) ax.set_xticklabels(["Midterm", "Final"], fontsize=12, fontweight='bold')
- set_xticks([1, 2]): Adds a scale to the x-axis.
- set_xticklabels(): Set the scale label to "Midterm"and "Final"to the default.
9. Remove the Y-axis and border
ax.yaxis.set_visible(False) ax.set_yticks([]) ax.spines['left'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) ax.spines['bottom'].set_visible(False)
- Remove Y-Axis:
ax.yaxis.set_visible(False): Makes the Y-axis invisible.ax.set_yticks([])Removes the y-axis scale.- Remove borders:
spines['left'].set_visible(False): Remove left borderspines['right']: Remove right borderspines['top']: Remove top borderspines['bottom']: Remove Bottom Border10. print the graph
plt.show()
- plt.show(): Prints the graph to the screen.





