简体   繁体   English

为什么 matplotlib.pyplot 在使用 jupyter notebook 时工作正常,但从 CMD 中的 a.py 文件运行时却不起作用?

[英]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.我有一段代码在 jupyter notebook 中工作得很好,没有问题,但是当我尝试从 CMD 上的 .py 文件运行它时,程序运行并且我没有收到任何错误,但我的情节没有出现。 all I get is an empty window.我得到的只是一个空的 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:这是我从 cmd 运行它时看到的:

在此处输入图像描述

here is what I see when I run the code in jupyter notebook (expected):这是我在 jupyter notebook 中运行代码时看到的内容(预期): 在此处输入图像描述

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 = plt.figure() object 似乎是一个问题,不确定是什么问题,但我没有执行原始代码,而是将其替换为:

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这基本上做同样的事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 matplotlib.pyplot 时,tkinter GUI 不会以“X”终止 - tkinter GUI does not terminate with 'X' when using matplotlib.pyplot 在 colab 中运行 py 文件时,使用 %matplotlib notebook 或 %matplotlib notebook 不起作用 - Using %matplotlib notebook or %matplotlib notebook does not work when run py file in colab 无法使用`matplotlib.pyplot` 在 jupyter notebook 上打印文本 - Can't print the text on jupyter notebook using `matplotlib.pyplot` 在iPython Notebook中使用matplotlib.pyplot - Using matplotlib.pyplot in iPython Notebook VSCode Intellisense 在编辑 python 文件时有效,但在 Jupyter Notebook 中无效 - VSCode Intellisense works when editing a python file but does not work in Jupyter Notebook 错误:在 Mac 的 jupyter notebook 中将 matplotlib.pyplot 作为 mlt 导入 - Error: Import matplotlib.pyplot as mlt in jupyter notebook in Mac 如何在 Jupyter 笔记本中重绘 matplotlib.pyplot 图? - How do I redraw a matplotlib.pyplot figure in a Jupyter notebook? 如何在使用matplotlib.pyplot时显示节点标签? - How to show node labels when using matplotlib.pyplot? 使用matplotlib.pyplot添加轴时如何保持分辨率? - How to preserve the resolution when adding axis using matplotlib.pyplot? Matplotlib.Pyplot不显示输出;没有错误 - Matplotlib.Pyplot does not show output; No Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM