[英]Matplotlib and Ipython-notebook: Displaying exactly the figure that will be saved
[英]Error displaying a matplotlib in ipython notebook
我正在看一个ipython笔记本教程,它说要在一个单元格中运行它。 导入numpy作为np导入数学导入matplotlib.pyplot作为plt
x = np.linspace(0, 2*math.pi)
plt.plot(x, np.sin(x), label=r'$\sin(x)$')
plt.plot(x, np.cos(x), 'ro', label=r'$\cos(x)$')
plt.title(r'Two plots in a graph')
plt.legend()
我应该得到一个实际的图。 Isntead我明白了
<matplotlib.legend.Legend at 0x1124a2fd0>
我该怎么办呢?
尝试在笔记本中更早地添加以下语句,该语句向matplotlib指示在何处呈现绘图(即,作为笔记本中嵌入的html元素):
%matplotlib inline
其背后的故事仅仅是因为matplotlib足够老,足以在jupyter和ipython笔记本普及之前就存在。 那时,创建绘图的标准方法是编写脚本,运行脚本并获取图像文件作为结果。 当前,相同的图像可以在笔记本电脑中轻松直接看到,但要付出上述补充“重新编写”声明的代价。
为了在笔记本中显示任何绘图,您可以将plot
语句作为该块代码的最后一行(即,绘图就是返回的值,由jupyter自动呈现),或使用plt.show()进行描述。由Abdou发表评论。
另外,请注意您的代码中有2个图:
# Put these 2 in two separate notebook blocks to get 2 separate plots.
# As-is the first one will never get displayed
plt.plot(x, np.sin(x), label=r'$\sin(x)$')
plt.plot(x, np.cos(x), 'ro', label=r'$\cos(x)$')
如果您想将所有图绘制为一个图像(使用matplotlib imho会很快变得麻烦),请查看子图文档
为了使结果更美观,请添加;
在图的末尾避免难看的<matplotlib.legend.Legend at 0x1124a2fd0>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.