[英]How to force xlim unchanged permanently, even after plt.cla() in matplotlib
I want to force the xlim unchanged permanently, even after plt.cla()
in matplotlib with Jupyter Notebook.我想强制 xlim 永久不变,即使在带有 Jupyter Notebook 的 matplotlib 中的
plt.cla()
之后plt.cla()
如此。
data=[[0,1],[0,1]]
plt.ylim(0,4)
plt.plot(*data)
plt.cla()
data=[[0,2],[0,2]]
plt.plot(*data) # <- rescaled automatically...
How to solve?怎么解决?
plt.cla()
clears the axes and all the information about limits stored in it. plt.cla()
清除轴和存储在其中的所有有关限制的信息。 Options you have:您拥有的选项:
Store the previous values and reset them to the new axes.存储以前的值并将它们重置为新轴。
import matplotlib.pyplot as plt data=[[0,1],[0,1]] plt.ylim(0,4) plt.plot(*data) xlims = plt.xlim() plt.cla() data=[[0,2],[0,2]] plt.plot(*data) plt.xlim(xlims) plt.show()
Don't clear the axes.不要清除轴。 Instead maybe just remove the content from the axes.
相反,也许只是从轴中删除内容。
import matplotlib.pyplot as plt data=[[0,1],[0,1]] plt.ylim(0,4) plt.plot(*data) for line in plt.gca().lines: line.remove() data=[[0,2],[0,2]] plt.plot(*data) plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.