fig = plt.figure(figsize=(16, 16))
#Generate first column of line charts. STEM degrees.
for sp in range(0,18,3):
cat_index = int(sp/3)
ax = fig.add_subplot(6,3,sp+1)
ax.plot(women_degrees['Year'], women_degrees[stem_cats[cat_index]], c=cb_dark_blue, label='Women', linewidth=3)
ax.plot(women_degrees['Year'], 100-women_degrees[stem_cats[cat_index]], c=cb_orange, label='Men', linewidth=3)
for key,spine in ax.spines.items():
spine.set_visible(False)
ax.set_xlim(1968, 2011)
ax.set_ylim(0,100)
ax.set_title(stem_cats[cat_index])
ax.tick_params(bottom=False, top=False, left=False, right=False,labelbottom=False)
ax.set_yticks([0,100])
#Set to the third color (light gray) in the Color Blind 10 palette
ax.axhline(50, c=(171/255, 171/255, 171/255), alpha=0.3)
if cat_index == 0:
ax.text(2003, 85, 'Women')
ax.text(2005, 10, 'Men')
elif cat_index == 5:
ax.text(2005, 87, 'Men')
ax.text(2003, 7, 'Women')
ax.tick_params(labelbottom='on')
#Generate second column of line charts. Liberal arts degrees.
for sp in range(1,16,3):
cat_index = int((sp-1)/3)
ax = fig.add_subplot(6,3,sp+1)
ax.plot(women_degrees['Year'], women_degrees[lib_arts_cats[cat_index]], c=cb_dark_blue, label='Women', linewidth=3)
ax.plot(women_degrees['Year'], 100-women_degrees[lib_arts_cats[cat_index]], c=cb_orange, label='Men', linewidth=3)
for key,spine in ax.spines.items():
spine.set_visible(False)
ax.set_xlim(1968, 2011)
ax.set_ylim(0,100)
ax.set_title(lib_arts_cats[cat_index])
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 cat_index == 0:
ax.text(2003, 78, 'Women')
ax.text(2005, 18, 'Men')
elif cat_index == 4:
ax.tick_params(labelbottom='on')
#Generate third column of line charts. Other degrees.
for sp in range(2,20,3):
cat_index = int((sp-2)/3)
ax = fig.add_subplot(6,3,sp+1)
ax.plot(women_degrees['Year'], women_degrees[other_cats[cat_index]], c=cb_dark_blue, label='Women', linewidth=3)
ax.plot(women_degrees['Year'], 100-women_degrees[other_cats[cat_index]], c=cb_orange, label='Men', linewidth=3)
for key,spine in ax.spines.items():
spine.set_visible(False)
ax.set_xlim(1968, 2011)
ax.set_ylim(0,100)
ax.set_title(other_cats[cat_index])
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 cat_index == 0:
ax.text(2003, 90, 'Women')
ax.text(2005, 5, 'Men')
elif cat_index == 5:
ax.text(2005, 62, 'Men')
ax.text(2003, 30, 'Women')
ax.tick_params(labelbottom='on')
# fig.savefig('path/to/save/image/to.png') \\---save the figure to file
# Export file before calling pyplot.show()
fig.savefig("degrees.png")
plt.show()
I use this code to export the file, but when I run this line (in a new code block ) to show it
fig.show("degrees.png")
I get this error message
/Users/jamesberentsen/opt/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:1: UserWarning: Matplotlib is currently using module://ipykernel.pylab.backend_inline, which is a non-GUI backend, so cannot show the figure.
ââ"Entry point for launching an IPython kernel.
Also could it please be explained, what the point of running matplotlib.get_backend() is here I get ouput but do not know what to do with it
or what kind of useful information it is telling me
T o help support these different use cases, matplotlib can target different outputs or backends . If you import matplotlib and run matplotlib.get_backend()
, youâll see the specific backend youâre currently using.
With the current backend we're using, we can use [Figure.savefig()](http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.savefig) or [pyplot.savefig()](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.savefig) to export all of the plots contained in the figure as a single image file. Note that these have to be called before we display the figure using `pyplot.show()` .:
plt.plot(women_degrees['Year'], women_degrees['Biology'])
plt.savefig('biology_degrees.png')
Why does plt.plot() have to be called before running plt.savefig? Is it because the image is not created in memory by plt.savefig, how does savefig know which plot we are referring to since it is only given a name -âdegrees.pngâ and that name has not been referenced earlier?