简体   繁体   中英

why does matplotlib.pyplot works fine when using jupyter notebook but it does not work when running from a .py file in CMD?

I have a block of code that works just fine in the jupyter notebook with no issues, but then when I try to run it from a.py file on the CMD the program runs and I get no errors but I my plots dont show up. all I get is an empty window.

Does anyone know what im doing wrong?

this is the code im trying to run:

import matplotlib.pyplot as plt

fig = plt.figure()
axes1 = fig.add_axes([1,1,1,1])
axes1.set(title = 'Apple',
         xlabel = 'Date',
         ylabel = 'Adj Close')
axes1.plot(data['aapl'].index, data['aapl']['Adj Close'])

axes2 = fig.add_axes([1,2.2,1,1])
axes2.set(title = 'Microsoft',
         xlabel = 'Date',
         ylabel = 'Adj Close')
axes2.plot(data['msft'].index, data['msft']['Adj Close'])

axes3 = fig.add_axes([2.2,1,1,1])
axes3.set(title = 'Google',
         xlabel = 'Date',
         ylabel = 'Adj Close')
axes3.plot(data['googl'].index, data['googl']['Adj Close'])

axes4 = fig.add_axes([2.2,2.2,1,1])
axes4.set(title = 'Tesla',
         xlabel = 'Date',
         ylabel = 'Adj Close')
axes4.plot(data['tsla'].index, data['tsla']['Adj Close'])


plt.show(block=True)

this is what I see when I run it from cmd:

在此处输入图像描述

here is what I see when I run the code in jupyter notebook (expected): 在此处输入图像描述

I figured it out. Seems to be an issue with the fig = plt.figure() object for some reason, not sure what the problem is but instead of doing the original code, I replaced it with:

fig, axs = plt.subplots(2,2)
axs[0,0].plot(data['aapl'].index, data['aapl']['Adj Close'])
axs[0,0].set_title('APPLE')
axs[0,1].plot(data['tsla'].index, data['tsla']['Adj Close'])
axs[0,1].set_title('TESLA')
axs[1,0].plot(data['msft'].index, data['msft']['Adj Close'])
axs[1,0].set_title('MICROSOFT')
axs[1,1].plot(data['googl'].index, data['googl']['Adj Close'])
axs[1,1].set_title('GOOGLE')
for ax in axs.flat:
    ax.set(xlabel='Date', ylabel='Adj. Close')

this does essentially the same thing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM