我试图在MacOS X上使用IPython笔记本,使用Python 2.7.2和IPython 1.1.0。
我无法让matplotlib图形内联显示。
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
我还尝试了%pylab inline和ipython命令行参数——pylab=inline,但这没有什么区别。
x = np.linspace(0, 3*np.pi, 500)
plt.plot(x, np.sin(x**2))
plt.title('A simple chirp')
plt.show()
而不是内联图形,我得到这个:
<matplotlib.figure.Figure at 0x110b9c450>
matplotlib.get_backend()显示我有'模块://IPython.kernel.zmq.pylab。backend_inline的后端。
当我在Jupyter的独立单元中运行绘图命令时,我也遇到了同样的问题:
In [1]: %matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
In [2]: x = np.array([1, 3, 4])
y = np.array([1, 5, 3])
In [3]: fig = plt.figure()
<Figure size 432x288 with 0 Axes> #this might be the problem
In [4]: ax = fig.add_subplot(1, 1, 1)
In [5]: ax.scatter(x, y)
Out[5]: <matplotlib.collections.PathCollection at 0x12341234> # CAN'T SEE ANY PLOT :(
In [6]: plt.show() # STILL CAN'T SEE IT :(
通过将绘图命令合并到一个单元格中,问题得到了解决:
In [1]: %matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
In [2]: x = np.array([1, 3, 4])
y = np.array([1, 5, 3])
In [3]: fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.scatter(x, y)
Out[3]: <matplotlib.collections.PathCollection at 0x12341234>
# AND HERE APPEARS THE PLOT AS DESIRED :)
您可以用一个语法错误来模拟这个问题,但是%matplotlib inline不能解决这个问题。
首先是一个创建情节的正确方法的例子。使用eNord9提供的导入和魔法,一切都按照预期工作。
df_randNumbers1 = pd.DataFrame(np.random.randint(0,100,size=(100, 6)), columns=list('ABCDEF'))
df_randNumbers1.ix[:,["A","B"]].plot.kde()
但是,如果将()放在plot类型的末尾,则会收到一个有点模糊的非错误。
Erronious代码:
df_randNumbers1.ix[:,["A","B"]].plot.kde
错误示例:
<bound method FramePlotMethods.kde of <pandas.tools.plotting.FramePlotMethods object at 0x000001DDAF029588>>
除了这一行消息之外,没有堆栈跟踪或其他明显的理由认为您犯了语法错误。情节没有打印出来。
当我在Jupyter的独立单元中运行绘图命令时,我也遇到了同样的问题:
In [1]: %matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
In [2]: x = np.array([1, 3, 4])
y = np.array([1, 5, 3])
In [3]: fig = plt.figure()
<Figure size 432x288 with 0 Axes> #this might be the problem
In [4]: ax = fig.add_subplot(1, 1, 1)
In [5]: ax.scatter(x, y)
Out[5]: <matplotlib.collections.PathCollection at 0x12341234> # CAN'T SEE ANY PLOT :(
In [6]: plt.show() # STILL CAN'T SEE IT :(
通过将绘图命令合并到一个单元格中,问题得到了解决:
In [1]: %matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
In [2]: x = np.array([1, 3, 4])
y = np.array([1, 5, 3])
In [3]: fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.scatter(x, y)
Out[3]: <matplotlib.collections.PathCollection at 0x12341234>
# AND HERE APPEARS THE PLOT AS DESIRED :)