2016-10-17 254 views
1

我似乎無法通過RGB定義更改Matplotlib散點圖的顏色。我錯了嗎?更改matplotlib動畫中的RGB顏色

下面是一個代碼(在堆棧溢出已經給出)與浮法索引顏色搭配:

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

def main(): 
    numframes = 100 
    numpoints = 10 

    color_data = np.random.random((numframes, numpoints)) 
    x, y, c = np.random.random((3, numpoints)) 

    fig = plt.figure() 
    scat = plt.scatter(x, y, c=c, s=100) 

    ani = animation.FuncAnimation(fig, update_plot, frames=range(numframes), 
            fargs=(color_data, scat)) 
    plt.show() 

def update_plot(i, data, scat): 
    scat.set_array(data[i]) 
    return scat, 

main() 

但如果color_data通過RGB顏色定義,我得到一個錯誤:

ValueError: Collections can only map rank 1 arrays

相關代碼如下(在此代碼中,我只是每次更改一個樣本的顏色):

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

def main(): 
    numframes = 100 
    numpoints = 10 

    rgb_color_data = np.random.random((numpoints, 3)) 
    x, y = np.random.random((2, numpoints)) 

    fig = plt.figure() 
    scat = plt.scatter(x, y, c=rgb_color_data, s=100) #this work well at this level 

    ani = animation.FuncAnimation(fig, update_plot2, frames=range(numframes), 
            fargs=(rgb_color_data, scat)) 

    plt.show() 

def update_plot2(i,data,scat): 
    data[ i%10 ] = np.random.random((3)) 
    scat.set_array(data) # this fails 
    return scat, 

main() 

有沒有辦法使用set_array與RGB顏色數組?

回答

3

不知道你想達到什麼。但是,如果您嘗試更改顏色,爲什麼不使用Collectionset_color()功能?

def update_plot2(i,data,scat): 
    data[ i%10 ] = np.random.random((3)) 
    scat.set_color(data) # <<<<<<<<<<<<<<<<<<< 
    return scat,