1

I have created the following chart:

enter image description here

using the following code:

def plot_exercises_scores(exercise_list, scores, semester_id):
    print (exercise_list)
    plt.bar(exercise_list, scores, color='orange')
    plt.title("Exercise scores for " + semester_id, fontsize=20)
    plt.xlabel("exercise number", fontsize=16 )
    plt.ylabel("score", fontsize=16)
    plt.ylim(0, 100)
    plt.show()

Look at the x-axis - it shows the bar on values 0.75, 1.25, 1.75, 2.25 while they don't even exists in exercise_list. exercise_list = [1 2]

Any idea what I'm missing?

TheUnreal
  • 23,434
  • 46
  • 157
  • 277

1 Answers1

0

To my knowledge, pyplot always behaves like that, but by using a solution found here I made something like this:

enter image description here

If this is what you wanted, then here's full code:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker


def plot_exercises_scores(exercise_list, scores, semester_id):
    fig, ax = plt.subplots()

    # Be sure to only pick integer tick locations.
    for axis in [ax.xaxis, ax.yaxis]:
        axis.set_major_locator(ticker.MaxNLocator(integer=True))

    plt.bar(exercise_list, scores, color='orange')
    plt.title("Exercise scores for " + semester_id, fontsize=20)
    plt.xlabel("exercise number", fontsize=16)
    plt.ylabel("score", fontsize=16)
    plt.ylim(0, 100)
    plt.show()

    fig.tight_layout()


plot_exercises_scores([1, 2], [90, 85], "2018/2019a")
Hakej
  • 414
  • 4
  • 8