Python 熊猫图不显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34347145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas plot doesn't show
提问by Basj
When using this in a script (not IPython), nothing happens, i.e. the plot window doesn't appear :
在脚本(不是 IPython)中使用它时,没有任何反应,即不会出现绘图窗口:
import numpy as np
import pandas as pd
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts.plot()
Even when adding time.sleep(5)
, there is still nothing. Why?
即使添加time.sleep(5)
,仍然什么都没有。为什么?
Is there a way to do it, without having to manually call matplotlib?
有没有办法做到这一点,而不必手动调用 matplotlib?
采纳答案by tmdavison
Once you have made your plot, you need to tell matplotlib to show
it. The usual way to do things is to import matplotlib.pyplot
and call show
from there:
一旦你完成了你的情节,你需要告诉 matplotlib 给show
它。通常的做法是从那里导入matplotlib.pyplot
和调用show
:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts.plot()
plt.show()
Since you have requested not to do that (why?), you could use the following [NOTE: This no longer appears to work with newer versions of pandas
]:
由于您已要求不这样做(为什么?),您可以使用以下 [注意:这似乎不再适用于较新版本的pandas
]:
import numpy as np
import pandas as pd
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts.plot()
pd.tseries.plotting.pylab.show()
But all you are doing there is finding somewhere that matplotlib
has been imported in pandas
, and calling the same show
function from there.
但是您在那里所做的就是找到matplotlib
已导入的某个地方pandas
,并show
从那里调用相同的函数。
Are you trying to avoid calling matplotlib
in an effort to speed things up? If so then you are really not speeding anything up, since pandas
already imports pyplot
:
您是否试图避免打电话matplotlib
以加快速度?如果是这样,那么您实际上并没有加快任何速度,因为pandas
已经导入了pyplot
:
python -mtimeit -s 'import pandas as pd'
100000000 loops, best of 3: 0.0122 usec per loop
python -mtimeit -s 'import pandas as pd; import matplotlib.pyplot as plt'
100000000 loops, best of 3: 0.0125 usec per loop
Finally, the reason the example you linked in commentsdoesn't need the call to matplotlib
is because it is being run interactively in an iPython notebook
, not in a script.
最后,您在注释中链接的示例不需要调用matplotlib
的原因是因为它是在 中交互运行的iPython notebook
,而不是在脚本中。
回答by Hamed MP
In case you are using matplotlib,
and still, things don't show up in iPython notebook (or Jupyter Lab as well) remember to set the inline option for matplotlib
in the notebook.
如果您正在使用matplotlib,
并且仍然没有出现在 iPython notebook(或 Jupyter Lab)中,请记住matplotlib
在 notebook 中设置 inline 选项。
import matplotlib.pyplot as plt
%matplotlib inline
Then the following code will work flawlessly:
然后以下代码将完美运行:
fig, ax = plt.subplots(figsize=(16,9));
change_per_ins.plot(ax=ax, kind='hist')
If you don't set the inline option it won't show up and by adding a plt.show()
in the end you will get duplicate outputs.
如果您不设置 inline 选项,它将不会显示,并且通过plt.show()
在最后添加 a您将获得重复的输出。