简体   繁体   English

当 plt.plot() 调用在其他单元格中完成时 plt.show() 为空

[英]plt.show() is empty when the plt.plot() calls are done in other cells

Context语境

I have to write a Jupyter notebook for a physics assignement, and I have to draw some graph showing the evolution of some quantities etc...我必须为物理作业写一个 Jupyter 笔记本,我必须绘制一些图表来显示某些量的演变等......

I have to explain almost each line of code I write by alternating between code and Markdown cells each time, including the plot() calls, which is where my problem comes from.我每次都必须通过在代码和 Markdown 单元格之间交替来解释我编写的几乎每一行代码,包括plot()调用,这就是我的问题的来源。 In very simple terms, I have something like :用非常简单的术语来说,我有类似的东西:


My code我的代码

We first import the matplotlib.pyplot module :我们首先导入matplotlib.pyplot模块:

from matplotlib import pyplot as plt

Next, we creates lists of values from the data :接下来,我们从数据创建值列表:

# making up stuff
x_values = range(10)
doubles = [x * 2 for x in x_values]
squares = [x**2 for x in x_values]

We first plot the curve for doubles :我们首先绘制doubles曲线:

%matplotlib inline

plt.plot(x_values, doubles, color='red', label='x * 2')

It shows the graph for the doubles (and only for the doubles)它显示了双打的图表(仅适用于双打)

Then we plot squares :然后我们绘制squares

%matplotlib inline

plt.plot(x_values, squares, color='red', label='x^2')

It shows the graph for the squares (and only for the squares)它显示了正方形的图形(并且仅显示了正方形)

And, finally, we configure our graph and show it :最后,我们配置我们的图表并显示它:

%matplotlib inline

plt.xlabel("x")
plt.ylabel("f(x)")
plt.ylim(0, 50)
plt.xlim(0, 11)
plt.legend()
plt.grid(True)
plt.show()

It shows an empty graph, with the labels, grid and limits set right, but no curve or legend at all它显示一个空图,标签、网格和限制设置正确,但根本没有曲线或图例


Expected预期的

The last cells shows a graph with the two curves and the legend and all the stuff I configured最后一个单元格显示了带有两条曲线和图例以及我配置的所有内容的图形

What happens/My problem会发生什么/我的问题

The last cells shows an empty graph with no curve or legend最后一个单元格显示一个没有曲线或图例的空图

What I've Tried我试过的

However, if I try to put the plot() calls in the last cell, with all the other stuff, I do get the expected graph, with the curves, legend, etc... But I cannot do that with my actual assignement, unfortunately :/但是,如果我尝试将plot()调用放在最后一个单元格中,以及所有其他内容,我确实会得到预期的图形、曲线、图例等......但我不能用我的实际分配来做到这一点,很遗憾 :/

I also tried almost every value for the %matplotlib inline line, I tried multiple cells, multiple placements, etc... And it doesn't solve my problem我还尝试了%matplotlib inline行的几乎所有值,我尝试了多个单元格、多个位置等......但它并没有解决我的问题

By googling a bit, it seems like the first line of a notebook behaves a bit differently in regards to matplotlib and all that, but this isn't nearly the first cell in my notebook, so it can't be that.通过谷歌搜索,似乎笔记本的第一行在 matplotlib 和所有这些方面的行为有点不同,但这几乎不是我笔记本中的第一个单元格,所以不可能是这样。

Honestly, I have no idea why that happens.老实说,我不知道为什么会这样。 I think I read somewhere that pyplot works as a sort of state-machine, so maybe the fact that it spreads over multiple lines is making it confused, but for other things (labels, style, etc...) it works as expected, so I'm clueless right now.我想我在某处pyplot作为一种状态机工作,所以也许它分布在多行上的事实使它感到困惑,但对于其他事物(标签、样式等...),它按预期工作,所以我现在一无所知。

So I guess my question is : is this normal behaviour, is it a bug, or am I just missing something ?所以我想我的问题是:这是正常行为,是错误,还是我只是遗漏了什么?

EDIT : It seems like using fig, ax = plt.subplots(1, 1) and then plotting on ax works, but the problem is that it adds complexity that I cannot easily explain, that it is rather clunky, and that it doesn't answer "Why does it work when it's in the same cell, but not when the lines are in different cells ?"编辑:似乎使用fig, ax = plt.subplots(1, 1)然后在ax绘图,但问题是它增加了我无法轻易解释的复杂性,它相当笨重,而且它没有t 回答“为什么当它在同一个单元格中时它起作用,而当线条在不同的单元格中时它不起作用?”

If you do not want to use fig and ax , then a (maybe not very elegant) solution but easier to explain would be to edit the last cell as following:如果您不想使用figax ,那么一个(可能不是很优雅)但更容易解释的解决方案是编辑最后一个单元格,如下所示:

plt.plot(x_values, doubles, x_values, squares)

instead of代替

plt.plot()

This is normal behavior for the Jupyter Notebook.这是 Jupyter Notebook 的正常行为。 It expects all the commands for one plot to be in the same cell, and then when the cell ends, it renders the plot.它期望一个绘图的所有命令都在同一个单元格中,然后当单元格结束时,它会呈现绘图。 That's why you don't need plt.show() .这就是为什么你不需要plt.show() By the way, you only need the IPython magic command %matplotlib inline once per notebook, unless you want to switch between different display modes.顺便说一句,除非您想在不同的显示模式之间切换,否则每个笔记本只需要%matplotlib inline一次 IPython 魔术命令%matplotlib inline

To annotate every line of plotting commands, you could of course just use Python comments within the cell.要注释每一行绘图命令,您当然可以只在单元格中使用 Python 注释。 If you have to use Markdown, a simple workaround is to enumerate the command lines by number comments, or just set notes at certain important lines, and then refer to these numbers in the next Markdown cell.如果必须使用 Markdown,一个简单的解决方法是通过数字注释枚举命令行,或者只是在某些重要行设置注释,然后在下一个 Markdown 单元格中引用这些数字。

Note that this annotation problem is not specific to plots.请注意,此注释问题并非特定于绘图。 For example, how would you annotate every line of a function definition?例如,您将如何注释函数定义的每一行? The whole definition has to be in one cell.整个定义必须在一个单元格中。

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

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