我在使用matplotlibsplotfile
函数时遇到了一个奇怪的行为。你知道吗
我想对文件text.txt
的一个绘图进行注释,它包含:
x
0
1
1
2
3
使用以下代码:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
annot = ax.annotate("Test", xy=(1,1))
plt.plotfile('test.txt', newfig = False)
plt.show()
这使我得到以下奇怪的图,轴标签到处都是,注释在错误的位置(相对于我的数据):
但是,当我使用
fig = plt.figure()
ax = fig.add_subplot(111)
而不是
fig, ax = plt.subplots()
MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
因此,我认为在一种情况下,plt.plotfile
使用以前的轴,也用于生成注释,但这会给我一个警告,而在另一种情况下,它会生成一个新的轴实例(因此没有警告),但也会生成一个带有两个重叠轴的奇怪绘图。你知道吗
现在我想知道两件事:
ax.scatter, ax.plot
。。。我不能打电话给ax.plotfile
)
plotfile
是一个方便的函数,可以直接打印文件。这意味着它假定不存在先验轴和creates a new one。如果真的有人在场,这可能会导致有趣的行为。但你仍然可以按预期的方式使用它然而,正如the documentation所说
因此,一旦您想对图形或轴进行重大更改,最好不要依赖
plotfile
。类似的功能可以通过与面向对象的方法完全兼容
相关问题 更多 >
编程相关推荐