简体   繁体   中英

Plot Dynamic graph of live dataframe of multiple columns using Python

I'm trying to plot dynamic graph with live data. Below is sample dataframe in which after every 30 seconds I want to add data which I'm collecting from other function.

Dynamic DataFrame

    TIME  CE_15750  CE_15800  CE_15850  CE_15900  CE_15950   PE_16000  PE_16050  PE_16100  PE_16150  PE_16200  PE_16250
0  18:54     -5146    -58520    -22849    -78925    -20435     59348     10805      5877       -22      2182     519
1  18:55    -20435    -30990     37085       108     -4634     13528      27239     64451     46905     83803    815

I'm trying below code to plot graph but its not giving expected output

def draw_method():           

    while True:
        plt.cla()
        temp_var = live_data()
        final_dataframe = pd.DataFrame(temp_var)
        final_dataframe.set_index('TIME').plot(figsize = (10,5),grid=True)
        plt.gcf()
        plt.show()
        sleep(30) #30 seconds sleep to add updated data field in dataframe

also, how can I give single color for all columns which has CE_ and single color to PE_ in graph? The graph I'm getting had different color for each line.

Thanks so much for help

Try this. And tell me how it went. Is worth noting, my changes were the following. I highly doubt you want to make more than one window be displayed every 30 seconds so i took out plt.show() from you func. And i utilized the FuncAnimation, cause that guy is going to call your dataframe multiple times, and update it for you. Very usefull.

from matplotlib.animation import FuncAnimation


def draw_method():           
    plt.cla()
    temp_var = live_data()
    final_dataframe = pd.DataFrame(temp_var)
    final_dataframe.set_index('TIME').plot(figsize = (10,5),grid=True)

ani = FuncAnimation(plt.gcf(), draw_method, interval=1000)

plt.tight_layout()
plt.show()

temp_var = live_data()
final_dataframe = pd.DataFrame(temp_var)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM