而不是两个颜色图,只显示一个颜色图。在matplotlib中

时间:2020-04-02 11:19:07

标签: python matplotlib

如果运行此代码,您将理解我的问题。我想查看基于z列表中值的红色和黄色图形。所有点仅显示一种颜色。

import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style

style.use("ggplot")
x = [1,2,3,4,5,6,7,8,9,10,15]
y = [6,5,7,2,9,8,3,4,1,10,15]
z = [1,1,2,2,2,1,1,2,1,2,1]
x_vals = []
y_vals = []
z_vals = []
fig = plt.figure()
ax1 = plt.subplot(111)

def animate(i,x_vals,y_vals,z_vals):
    print("i:",i)
    x_vals.append(x[i])
    y_vals.append(y[i])
    z_vals.append(z[i])
    ax1.clear()
    if z_vals[i] == 1:
        l1, = ax1.plot(x_vals, y_vals, 'ro')
    if z_vals[i] == 2:
        l2, = ax1.plot(x_vals, y_vals, 'yo')
    ax1.tick_params(axis='x')
    plt.xticks(x_vals, x_vals)

print("Animedotted")
line_ani = animation.FuncAnimation(fig, animate, fargs=(x_vals, y_vals, z_vals), interval=1000, blit = False)
plt.show()

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您是否希望每次迭代都使用两种颜色的点?

animate函数中,您执行了ax.clear,因此在每次迭代中,所有点均被删除。

也是

if z_vals[i] == 1:
    l1, = ax1.plot(x_vals, y_vals, 'ro')
if z_vals[i] == 2:
    l2, = ax1.plot(x_vals, y_vals, 'yo')

使您只能使用一种颜色。