【发布时间】:2019-01-28 13:36:12
【问题描述】:
我在使用 matplotlibs plotfile 函数时遇到了一个奇怪的行为。
我想注释一个文件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 使用了以前也用于制作注释的轴,但这会给我一个警告,而在另一种情况下,它会创建一个新的轴实例(所以没有警告)但也会用两个重叠的轴制作一个奇怪的情节。
现在我想知道两件事:
- 为什么我声明图形和轴的方式会有所不同?根据this answer,它们应该可以互换?
- 如何告诉 plotfile 绘制到哪些轴并避免折旧警告以及将其绘制到正确的轴?我假设这不仅仅是绘图文件的问题,而是所有未直接在轴上调用的绘图类型(不像
ax.scatter, ax.plot,...我不能调用ax.plotfile)
【问题讨论】:
-
您是否有任何理由使用
plotfile而不是以其他方式读取数据并使用其他绘图功能? -
@DavidG 在这种情况下没有特殊原因,它在这里的另一个问题中使用过,所以我玩了一下这个功能并遇到了这种行为。
标签: python matplotlib plot axes