I came across a weird behaviour when using matplotlibs plotfile
function.
I wanted to annotate a plot of a file, text.txt
, which contains:
x
0
1
1
2
3
using the following code:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
annot = ax.annotate("Test", xy=(1,1))
plt.plotfile('test.txt', newfig = False)
plt.show()
This gets me the following, weird looking plot with axis labels all over the place and the annotation in the wrong (relative to my data) place:
However, when I use
fig = plt.figure()
ax = fig.add_subplot(111)
instead of
fig, ax = plt.subplots()
I get the plot I want and a depreciation warning:
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.
So I'm thinking that in one case, plt.plotfile
uses the previous axes that was also used to make the annotation, but this gets me a warning, whereas in the other case it makes a new axes instance (so no warning) but also makes a weird plot with two overlayed axes.
Now I'd like to know two things:
- Why does it make a difference how I declare my figure and axes, when, according to this answer they should be interchangeble?
- How do I tell plotfile which axes to plot to and avoid the depreciation warning as well as plot it to the correct axes? I'm assuming this isn't just an issue with plotfiles, but with all plot types that aren't called directly on the axes (unlike e.g.
ax.scatter, ax.plot
, ... I can't callax.plotfile
)