matplotlib图在y轴上显示了2个标签
我正在尝试在matplotlib中生成持续生成的图。我面临的问题与正确的Y轴上的标签有关。显示的范围是我想要的,但是还有一个额外的标签(0,0.2,... 1,0)。
def doAnimation():
fig, ax = plt.subplots()
def animate(i):
data=prices(a,b,c) #this gives a DataFrame with 2 columns (value 1 and 2)
plt.cla()
ax.plot(data.index, data.value1)
ax2 = ax.twinx()
ax2.plot(data.index, data.value2)
plt.gcf().autofmt_xdate()
plt.tight_layout()
return ax, ax2
call = FuncAnimation(plt.gcf(), animate, 1000)
return call
callSave = doAnimation()
plt.show()
有什么想法如何摆脱集合:0.0、0.2、0.4、0.6、0.8、1.0?
I am trying to generate a continously generated plot in matplotlib. The problem that I am facing is related to the labelling on the right y-axis. The range that shows is my desired, however there is also an additional set off labels (0, 0.2, ... 1,0).
def doAnimation():
fig, ax = plt.subplots()
def animate(i):
data=prices(a,b,c) #this gives a DataFrame with 2 columns (value 1 and 2)
plt.cla()
ax.plot(data.index, data.value1)
ax2 = ax.twinx()
ax2.plot(data.index, data.value2)
plt.gcf().autofmt_xdate()
plt.tight_layout()
return ax, ax2
call = FuncAnimation(plt.gcf(), animate, 1000)
return call
callSave = doAnimation()
plt.show()
Any ideas how can I get rid of the set: 0.0, 0.2, 0.4, 0.6, 0.8, 1.0?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
plt.cla
清除电流轴。首次调用plt.cla
时,当前轴为ax
(ax2
尚不存在)。清除这些轴将ax
的X和Y范围重置为(0,1)。但是,在第8行上,您将绘制到ax
,并且两个范围都经过适当调整。在第9行上,您可以创建 new 轴集,并将其称为
ax2
。当您留下animate
函数时,名称ax2
将脱离范围,但是所指的轴对象将持续存在。这些轴现在是电流轴。第二次调用Animate时,
plt.cla
清除了这些轴,将X和Y范围重置为(0,1)。然后,在第9行中,您创建了一个 new 集合的轴,并将其称为ax2
。 这些轴与以前的轴不同!ax2
实际上是指第三组轴,下次您调用plt.cla时将清除。
。每个动画的新调用都会制作一组新的轴。令人讨厌的轴标签似乎是粗体的。实际上,它们被吸引了一千次。最简单的(最少的更改)修复程序将是移动
ax2 = ax.twinx()
Animate
之外,然后用单独的调用ax.cla
和ax2.cla
。我认为一种更好的方法是在
Animate
之外创建线条,并在Animate
中修改其数据。当我们这样做时,让我们将这些引用替换为plt.gcf()
,用引用到图
,然后设置tiva
viaplt.subplots的参数
。将上述更改放在一起,我们得到,
andimation.gif
应该看起来像plt.cla
clears the current axes. The first time you callplt.cla
, the current axes areax
(ax2
doesn't exist yet). Clearing these axes resets both the x and y range ofax
to (0,1). However, on line 8, you plot toax
, and both ranges are appropriately adjusted.On line 9, you create a new set of axes and call them
ax2
. When you leave theanimate
function, the nameax2
will go out of scope, but the axes object to which it refers will persist. These axes are now the current axes.The second time you call animate,
plt.cla
clears those axes, resetting the x and y range to (0,1). Then, on line 9, you create a new set of axes and call themax2
. These axes are not the same axes as before!ax2
in fact refers to a third set of axes, which will be cleared the next time you callplt.cla
. Each new call to animate makes a new set of axes. The offending axes labels appear to be bolded; in fact, they have been drawn a thousand times.The simplest (fewest changes) fix would be to move
ax2 = ax.twinx()
outside ofanimate
, and replaceplt.cla
with separate calls toax.cla
andax2.cla
.I think a better approach would be to create the lines outside of
animate
, and modify their data withinanimate
. While we're at it, let's replace those references toplt.gcf()
with references tofig
, and settight_layout
via an argument toplt.subplots
.Putting said changes together, we get,
where
animation.gif
should look something like,