为什么轴不出现在此动画中?

时间:2019-05-11 04:21:32

标签: python-3.x matplotlib

在此动画中,轴未出现,我看不到代码中缺少任何东西。我不知道动画是否那样做。我尝试了两种方法而没有解决方案。那么如何显示轴呢?

感谢您的帮助。

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


fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1])


n_drops = 100

rain_drops = np.zeros(n_drops, dtype=[('position', float, 2),
                                      ('size',     float, 1),
                                      ('growth',   float, 1),
                                      ('color',    float, 4)])



scat = ax.scatter(rain_drops['position'][:, 0], rain_drops['position'][:, 1], s=rain_drops['size'], lw=0.5, edgecolors=rain_drops['color'], facecolors='midnightblue',marker='s')



def update(frame_number):

    current_index = frame_number % n_drops

    rain_drops['size'] += rain_drops['growth']

    rain_drops['position'][current_index] = np.random.uniform(0, 0, 1)

    rain_drops['size'][current_index] = 5

    rain_drops['color'][current_index] = (0.05, 0.05, 0.09, 1)

    rain_drops['growth'][current_index] = 200 #np.random.uniform(50, 200)

    scat.set_edgecolors(rain_drops['color'])

    scat.set_sizes(rain_drops['size'])

    scat.set_offsets(rain_drops['position'])


animation = FuncAnimation(fig, update, interval=10)

plt.show()

1 个答案:

答案 0 :(得分:0)

此问题源于您的图形设置。一种解决方案是替换:

ax = fig.add_axes([0, 0, 1, 1])

具有:

ax = fig.add_axes([0.05, 0.05, 0.85, 0.85])

或标准

ax = fig.add_subplot(111)
ax.set_ylim(-1,1)

这给了我类似的东西(动画中间某处的快照):

enter image description here