GP Visualizing The Gender Gap - matplotlib Syntax Feedback, bottom labels and ticks

Hello!

Thanks very much to the Dataquest team (and the wonderful community!) for the amazing course and tools and support. This project was very helpful, as they all have been.

One piece of feedback I will make the community (and DQ team) aware of, when I downloaded the script at the end of the project and ran it on my own Jupyter instance, the bottom tick marks were not removed from the charts as they were when I was working in the online terminal.

I’m not sure why the difference is (perhaps an older version of matplotlib gets imported to the online utility?) but I looked at the matplotlib documentation and discovered that the labelbottom parameter in tick_params method wants a boolean value:

Worked on DQ instance only:
ax.tick_params(labelbottom=‘on’)
ax.tick_params(labelbottom=‘off’)

Worked on my local server / Jupyter instance:
ax.tick_params(labelbottom=True)
ax.tick_params(labelbottom=False)

I’d be interested in a bit more detail of why this is happening for my own personal interest, but mostly wanted to let the team and community know in case someone else runs into the issue!

Regards,

-LZ

1 Like

Hey @liam.zsolt, welcome to the community.

Could you please share with us the graphs? So we can take a closer look.

Hi @lsuepb - sure thing.

My original code in the cell:

fig = plt.figure(figsize=(16, 20))

for sp in range(0,6):
ax = fig.add_subplot(6,3,3*sp+1)
ax.plot(women_degrees[‘Year’], women_degrees[stem_cats[sp]], c=cb_dark_blue, label=‘Women’, linewidth=3)
ax.plot(women_degrees[‘Year’], 100-women_degrees[stem_cats[sp]], c=cb_orange, label=‘Men’, linewidth=3)
ax.spines[“right”].set_visible(False)
ax.spines[“left”].set_visible(False)
ax.spines[“top”].set_visible(False)
ax.spines[“bottom”].set_visible(False)
ax.set_xlim(1968, 2011)
ax.set_ylim(0,100)
ax.set_title(stem_cats[sp])
ax.tick_params(bottom=“off”, top=“off”, left=“off”, right=“off”, labelbottom=“off”)
ax.set_yticks([0,100])
ax.axhline(50, c=(171/255, 171/255, 171/255), alpha=0.3)
if sp == 0:
ax.text(2005, 82, ‘Women’)
ax.text(2005, 12, ‘Men’)
elif sp == 5:
ax.text(2005, 72, ‘Men’)
ax.text(2005, 25, ‘Women’)
ax.tick_params(labelbottom=‘on’)

for sp in range(0,5):
ax = fig.add_subplot(6,3,3*sp+2)
ax.plot(women_degrees[‘Year’], women_degrees[lib_arts_cats[sp]], c=cb_dark_blue, label=‘Women’, linewidth=3)
ax.plot(women_degrees[‘Year’], 100-women_degrees[lib_arts_cats[sp]], c=cb_orange, label=‘Men’, linewidth=3)
ax.spines[“right”].set_visible(False)
ax.spines[“left”].set_visible(False)
ax.spines[“top”].set_visible(False)
ax.spines[“bottom”].set_visible(False)
ax.set_xlim(1968, 2011)
ax.set_ylim(0,100)
ax.set_title(lib_arts_cats[sp])
ax.tick_params(bottom=“off”, top=“off”, left=“off”, right=“off”, labelbottom=“off”)
ax.set_yticks([0,100])
ax.axhline(50, c=(171/255, 171/255, 171/255), alpha=0.3)
if sp == 0:
ax.text(2005, 87, ‘Women’)
ax.text(2005, 8, ‘Men’)
elif sp == 4:
ax.tick_params(labelbottom=‘on’)

for sp in range(0,6):
ax = fig.add_subplot(6,3,3*sp+3)
ax.plot(women_degrees[‘Year’], women_degrees[other_cats[sp]], c=cb_dark_blue, label=‘Women’, linewidth=3)
ax.plot(women_degrees[‘Year’], 100-women_degrees[other_cats[sp]], c=cb_orange, label=‘Men’, linewidth=3)
ax.spines[“right”].set_visible(False)
ax.spines[“left”].set_visible(False)
ax.spines[“top”].set_visible(False)
ax.spines[“bottom”].set_visible(False)
ax.set_xlim(1968, 2011)
ax.set_ylim(0,100)
ax.set_title(other_cats[sp])
ax.tick_params(bottom=“off”, top=“off”, left=“off”, right=“off”, labelbottom=“off”)
ax.set_yticks([0,100])
ax.axhline(50, c=(171/255, 171/255, 171/255), alpha=0.3)
if sp == 0:
ax.text(2005, 90, ‘Women’)
ax.text(2005, 5, ‘Men’)
elif sp == 5:
ax.text(2005, 62, ‘Men’)
ax.text(2005, 35, ‘Women’)
ax.tick_params(labelbottom=‘on’)

plt.show()

When I run it in the dataquest instance of Jupyter, I get the result we want:

However, when I run the same code on my local server, the marks sneak back in:

I fix it by replacing the strings with boolean in the tick_params method:

fig = plt.figure(figsize=(16, 20))

for sp in range(0,6):
ax = fig.add_subplot(6,3,3*sp+1)
ax.plot(women_degrees[‘Year’], women_degrees[stem_cats[sp]], c=cb_dark_blue, label=‘Women’, linewidth=3)
ax.plot(women_degrees[‘Year’], 100-women_degrees[stem_cats[sp]], c=cb_orange, label=‘Men’, linewidth=3)
ax.spines[“right”].set_visible(False)
ax.spines[“left”].set_visible(False)
ax.spines[“top”].set_visible(False)
ax.spines[“bottom”].set_visible(False)
ax.set_xlim(1968, 2011)
ax.set_ylim(0,100)
ax.set_title(stem_cats[sp])
ax.tick_params(bottom=False, top=False, left=False, right=False, labelbottom=False)
ax.set_yticks([0,100])
ax.axhline(50, c=(171/255, 171/255, 171/255), alpha=0.3)
if sp == 0:
ax.text(2005, 82, ‘Women’)
ax.text(2005, 12, ‘Men’)
elif sp == 5:
ax.text(2005, 72, ‘Men’)
ax.text(2005, 25, ‘Women’)
ax.tick_params(labelbottom=True, bottom=True)

for sp in range(0,5):
ax = fig.add_subplot(6,3,3*sp+2)
ax.plot(women_degrees[‘Year’], women_degrees[lib_arts_cats[sp]], c=cb_dark_blue, label=‘Women’, linewidth=3)
ax.plot(women_degrees[‘Year’], 100-women_degrees[lib_arts_cats[sp]], c=cb_orange, label=‘Men’, linewidth=3)
ax.spines[“right”].set_visible(False)
ax.spines[“left”].set_visible(False)
ax.spines[“top”].set_visible(False)
ax.spines[“bottom”].set_visible(False)
ax.set_xlim(1968, 2011)
ax.set_ylim(0,100)
ax.set_title(lib_arts_cats[sp])
ax.tick_params(bottom=False, top=False, left=False, right=False, labelbottom=False)
ax.set_yticks([0,100])
ax.axhline(50, c=(171/255, 171/255, 171/255), alpha=0.3)
if sp == 0:
ax.text(2005, 87, ‘Women’)
ax.text(2005, 8, ‘Men’)
elif sp == 4:
ax.tick_params(labelbottom=True, bottom=True)

for sp in range(0,6):
ax = fig.add_subplot(6,3,3*sp+3)
ax.plot(women_degrees[‘Year’], women_degrees[other_cats[sp]], c=cb_dark_blue, label=‘Women’, linewidth=3)
ax.plot(women_degrees[‘Year’], 100-women_degrees[other_cats[sp]], c=cb_orange, label=‘Men’, linewidth=3)
ax.spines[“right”].set_visible(False)
ax.spines[“left”].set_visible(False)
ax.spines[“top”].set_visible(False)
ax.spines[“bottom”].set_visible(False)
ax.set_xlim(1968, 2011)
ax.set_ylim(0,100)
ax.set_title(other_cats[sp])
ax.tick_params(bottom=False, top=False, left=False, right=False, labelbottom=False)
ax.set_yticks([0,100])
ax.axhline(50, c=(171/255, 171/255, 171/255), alpha=0.3)
if sp == 0:
ax.text(2005, 90, ‘Women’)
ax.text(2005, 5, ‘Men’)
elif sp == 5:
ax.text(2005, 62, ‘Men’)
ax.text(2005, 35, ‘Women’)
ax.tick_params(labelbottom=True, bottom=True)

plt.savefig(‘degrees.png’)

Perhaps I am missing something?

Thanks for the response!