plotfile没有使用正确的轴,注释问题 [英] plotfile not using correct axes, annotation problem

查看:31
本文介绍了plotfile没有使用正确的轴,注释问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用matplotlibs plotfile 函数时,我遇到了奇怪的行为.

I came across a weird behaviour when using matplotlibs plotfile function.

我想注释一个文件的图,text.txt,其中包含:

I wanted to annotate a plot of a file, text.txt, which contains:

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()

这让我得到以下奇怪的图,轴标签到处都是,注释在错误的地方(相对于我的数据):

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:

但是,当我使用

fig = plt.figure()
ax = fig.add_subplot(111)

代替

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.

所以我在想,在一种情况下,plt.plotfile 使用先前也用于制作注释的轴,但这给了我一个警告,而在另一种情况下它使一个新的轴实例(所以没有警告),但也用两个重叠的轴制作了一个奇怪的图.

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.

现在我想知道两件事:

  1. 根据这个答案,为什么我声明图形和轴的方式会有所不同?可互换?
  2. 如何告诉plotfile要绘制到哪个轴并避免折旧警告以及将其绘制到正确的轴?我假设这不仅仅是绘图文件的问题,而是所有未直接在轴上调用的绘图类型(与 ax.scatter, ax.plot 不同,......我无法调用 ax.plotfile )
  1. Why does it make a difference how I declare my figure and axes, when, according to this answer they should be interchangeble?
  2. 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 call ax.plotfile)

推荐答案

plotfile 是直接绘制文件的便捷功能.这意味着它假设不存在先验轴并且 rel="nofollow areferrer新的.如果确实已经存在轴,则这可能导致有趣的行为.你仍然可以按照预期的方式使用它,

plotfile is a convenience function to directly plot a file. This means it assumes that no prior axes exist and creates a new one. This may lead to funny behaviour if indeed there is already an axes present. You may still use it in the intended way though,

import matplotlib.pyplot as plt

plt.plotfile('test.txt')
annot = plt.annotate("Test", xy=(1,1))
plt.show()

然而,正如文档所述,

注意:plotfile 旨在方便从平面文件中快速绘制数据;它不打算作为使用 pyplot 或 matplotlib 进行一般绘图的替代接口.

Note: plotfile is intended as a convenience for quickly plotting data from flat files; it is not intended as an alternative interface to general plotting with pyplot or matplotlib.

因此,一旦要对图形或轴进行重大更改,最好不要依赖 plotfile .

So once you want to make significant changes to figure or axes, best do not rely on plotfile. Similar functionality can be achieved with

import numpy as np
import matplotlib.pyplot as plt

plt.plot(np.loadtxt('test.txt', skiprows=1))
annot = plt.annotate("Test", xy=(1,1))
plt.show()

然后与面向对象的方法完全兼容

which is then totally compatible with the object oriented approach,

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
annot = ax.annotate("Test", xy=(1,1))
ax.plot(np.loadtxt('test.txt', skiprows=1))

plt.show()

这篇关于plotfile没有使用正确的轴,注释问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆