Exporting to File local installtion of jupyter :Percentage of Bachelor degrees conferred to women

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?

https://app.dataquest.io/m/149/guided-project%3A-visualizing-the-gender-gap-in-college-degrees/6/exporting-to-a-file

1 Like

hey @jamesberentsen

  • have you tried to google this error? I got one post on stack-overflow which had a link to the solution on GitHub here not sure how much this is related to your error though.

  • About the backend, this is the official documentation from Matplotib - https://matplotlib.org/faq/usage_faq.html#what-is-a-backend.

  • It’s the other way round. plt.show() should be used after the Figure.save() or plt.save() method.
    Think of plt.show() as a cache clear for plots. so once we put this command in our flow, all of the plots/figures that have been coded before this command will be rendered/ displayed.

if we code plt.save() method after plt.show() we will get an empty “.png” file. Try the following code and examine both the images.

# fig, ax = .... 
# some code 
plt.savefig("before_show.png") # will give you image with the current figure you are working
plt.show()
plt.savefig("after_show.png") # will give an empty image

As the documentation for plt.save() specifies, it saves the current figure (you are working with, in the code cell).

Of course, to test this out, need to debug the error you are getting. Try the GitHub recommendations and let us know if that worked

StackOverflow post is here

1 Like

Hi Rucha,

Thanks for the link and Github, but the fixes mentioned did not help :

>     (base) Jamess-MacBook:~ jamesberentsen$ sudo apt-get install python3-tk
>     sudo: apt-get: command not found
>     (base) Jamess-MacBook:~ jamesberentsen$ sudo apt install python3-tk

>     Unable to locate an executable at "/Library/Java/JavaVirtualMachines/amazon-corretto-11.jdk/Contents/Home/bin/apt" (-1)

Yes I tried googling and looking at the doc . I know what a backend is but it seemed a bit above my head with things like this in the link and I did not want to be changing things which I did not know the details of:

  • Setting the MPLBACKEND environment variable, either for your current shell or for a single script:

Regards
JB

Oh I really suck at looking for actual issues! :gun:

Don’t use the fig.show(“image.png”) command. This is an invalid command.

The fig.save(“image.png”) or plt.save() method, must have stored the image file in your local folder, basically where you project_workbook.ipynb file is.

for example: I have this folder in which I have stored my project work, and the image(s) gets saved here as well.

1 Like