I have a problem using pyplot. I am new to Python so sorry if I am doing some obvious mistake.

After I have plotted something using pyplot it shows the graph, but when I then try and add e.g. ylabel it will not update the current graph. It results in a new graph with only the ylabel, not previously entered information. So to me it seems to be a problem with recognizing the current graph/axis, but the ishold delivers a True statement.

My setup is Python 2.7 in Python(x,y). The problem occurs both in the Spyder IDE and the IPython Qt Console. It does however not occur in the regular IPython console (which, by constrast, is not interactive, but everything is included when using show(). When I turn off interactive in Spyder/Qt console it does not show anything after using the show() command).

import matplotlib.pyplot as plt

plt.plot([1,2,3,4])
Out[2]: [<matplotlib.lines.Line2D at 0x78ca370>]


plt.ylabel('test')
Out[3]: <matplotlib.text.Text at 0x5bd5990>


plt.ishold()
Out[4]: True


matplotlib.get_backend()
Out[6]: 'module://IPython.kernel.zmq.pylab.backend_inline'

Hope any of you have some input. Thanks.

有帮助吗?

解决方案

This is one of the things were InlineBackend have to behave differently from other backend or you would have sort of a memory leak. You have to keep explicit handle to matplotlib figure and/or set close_figure to False in config. Usually pyplot is a compatibility layer for matlab for convenience, try to learn to do using the Object Oriented way.

fig,ax = subplots()
ax.plot(range(4))
ax.set_ylabel('my label')
...
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top