【问题标题】:python-using comma's when implementing before animation在动画之前实现时使用逗号的python
【发布时间】:2016-01-03 22:23:51
【问题描述】:

为了开始使用 matplotlib 动画,我看到了 double_pendulum 的精彩示例。 这是代码的一部分:

fig = plt.figure()
ax=fig.add_subplot(111,aspect='equal',autoscale_on=True)
ax.grid()

line, = ax.plot([], [], 'b-', lw=2)
time_text = ax.text(0.02, 0.95, '', transform=ax.transAxes)
energy_text = ax.text(0.02, 0.90, '', transform=ax.transAxes)

我想问他为什么在实现 ax.plot 时使用逗号。 当我使用类似的代码时,我还需要使用 line,作为 init 函数的返回语句,即使它是唯一返回的对象。我试着看了一下,找不到答案。有人可以帮我搞定吗?

谢谢

【问题讨论】:

    标签: python animation matplotlib


    【解决方案1】:

    ax.plot 返回添加到绘图中的线的列表。由于您只添加一行,ax.plot 返回一个包含一行的列表。 line, = 使用 Python 解包语法将单行从列表中拉出。相当于写

    line = ax.plot(...)[0]
    

    【讨论】:

      【解决方案2】:

      ax.plot([], [], 'b-', lw=2) 在这种情况下返回一个 1 元素列表,它在分配时被解包。没有逗号

      line = x.plot([], [], 'b-', lw=2)
      

      line 将是列表,而不是该列表的单个元素。

      基本上类似于这个:

      a, b = (1, 2)
      

      a 被分配 1,b 被分配 2。

      【讨论】:

        猜你喜欢
        • 2021-05-28
        • 2022-01-06
        • 2020-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多