matplotlib图在y轴上显示了2个标签

发布于 2025-02-04 12:39:20 字数 807 浏览 1 评论 0原文

我正在尝试在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?

This is how the graph looks:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〃温暖了心ぐ 2025-02-11 12:39:20

plt.cla清除电流轴。首次调用plt.cla时,当前轴为axax2尚不存在)。清除这些轴将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.claax2.cla

我认为一种更好的方法是在Animate之外创建线条,并在Animate中修改其数据。当我们这样做时,让我们将这些引用替换为plt.gcf(),用引用到 ,然后设置tiva via plt.subplots的参数

将上述更改放在一起,我们得到,

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import pandas as pd
import numpy as np


def dummy_prices():
    samples = 101
    xs = np.linspace(0, 10, samples)
    ys = np.random.randn(samples)
    zs = np.random.randn(samples) * 10 + 50
    return pd.DataFrame.from_records({'value1': ys, 'value2': zs}, index=xs)


def doAnimation():
    fig, ax = plt.subplots(1, 1, tight_layout=True)
    fig.autofmt_xdate()
    ax2 = ax.twinx()

    data = dummy_prices()
    line = ax.plot(data.index, data.value1)[0]
    line2 = ax2.plot(data.index, data.value2, 'r')[0]

    def animate(i):
        data = dummy_prices()
        line.set_data(data.index, data.value1)
        line2.set_data(data.index, data.value2)
        return line, line2

    animator = FuncAnimation(fig, animate, frames=10)
    return animator


def main():
    animator = doAnimation()
    animator.save('animation.gif')


if __name__ == '__main__':
    main()

andimation.gif应该看起来像

“

plt.cla clears the current axes. The first time you call plt.cla, the current axes are ax (ax2 doesn't exist yet). Clearing these axes resets both the x and y range of ax to (0,1). However, on line 8, you plot to ax, and both ranges are appropriately adjusted.

On line 9, you create a new set of axes and call them ax2. When you leave the animate function, the name ax2 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 them ax2. 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 call plt.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 of animate, and replace plt.cla with separate calls to ax.cla and ax2.cla.

I think a better approach would be to create the lines outside of animate, and modify their data within animate. While we're at it, let's replace those references to plt.gcf() with references to fig, and set tight_layout via an argument to plt.subplots.

Putting said changes together, we get,

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import pandas as pd
import numpy as np


def dummy_prices():
    samples = 101
    xs = np.linspace(0, 10, samples)
    ys = np.random.randn(samples)
    zs = np.random.randn(samples) * 10 + 50
    return pd.DataFrame.from_records({'value1': ys, 'value2': zs}, index=xs)


def doAnimation():
    fig, ax = plt.subplots(1, 1, tight_layout=True)
    fig.autofmt_xdate()
    ax2 = ax.twinx()

    data = dummy_prices()
    line = ax.plot(data.index, data.value1)[0]
    line2 = ax2.plot(data.index, data.value2, 'r')[0]

    def animate(i):
        data = dummy_prices()
        line.set_data(data.index, data.value1)
        line2.set_data(data.index, data.value2)
        return line, line2

    animator = FuncAnimation(fig, animate, frames=10)
    return animator


def main():
    animator = doAnimation()
    animator.save('animation.gif')


if __name__ == '__main__':
    main()

where animation.gif should look something like,

animation.gif

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文