0

pyplot doesn't listen to any of my styling commands and also doesn't show any axes ticks.

I copied this example from https://www.tutorialspoint.com/matplotlib/matplotlib_bar_plot.htmand couldn't get it to work as theirs did, nor when I added my data

team_data = {
    "England": {
        "avg_game_difference": 5.0476190476190474,
        "articles_count": 51
    },
    "France": {
        "avg_game_difference": 3.857142857142857,
        "articles_count": 12
    },
    "Ireland": {
        "avg_game_difference": 5.2,
        "articles_count": 22
    },
    "Italy": {
        "avg_game_difference": 7.0,
        "articles_count": 5
    },
    "Scotland": {
        "avg_game_difference": 2.5,
        "articles_count": 9
    },
    "Wales": {
        "avg_game_difference": 3.5833333333333335,
        "articles_count": 24
    },
    "New Zealand": {
        "avg_game_difference": 1.25,
        "articles_count": 9
    }
}

fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1])
categories = list(team_data.keys())
data = [i['articles_count'] for i in team_data.values()]
ax.bar(categories, data)
ax.set_title('Teams with most articles written')
ax.set_xlabel('Teams')
ax.set_ylabel('Number articles written')
plt.savefig('output.png')

see output.png

enter image description here

Fraser Langton
  • 471
  • 3
  • 12

1 Answers1

0

Try this:

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
categories = list(team_data.keys())
data = [i['articles_count'] for i in team_data.values()]
ax.bar(categories, data)
ax.set_title('Teams with most articles written')
ax.set_xlabel('Teams')
ax.set_ylabel('Number articles written')
plt.savefig('output.png')

This answer explains in detail the difference between add_axes and subplot but what's important here is that add_axes([0, 0, 1, 1]) fills the entire canvas with your plot leaving no space for labels or title.

YuseqYaseq
  • 244
  • 5
  • 16