Python Plotly:折线图的下拉菜单

时间:2020-10-27 13:43:24

标签: python-3.x pandas graph plotly plotly-python

问题陈述:-我有来自VM的mpstat数据。现在要绘制所有cpu核心的图形。但是,当CPU内核数量变得非常大时,将很难看到和解释该图。在某些情况下,虚拟机也有64个vCPU。

目标:-我要绘制折线图,​​默认情况下,我们将显示所有CPU内核的折线图,并且应该有一个下拉选项,使用户可以选择是否希望查看所有或单个CPU的图形。

我的输入csv如下所示:-

TIME_UTC,Average,CPU_CORE,USER,NICE,SYSTEM,IOWAIT,IRQ,SOFT,STEAL,GUEST,GNICE,IDLE
7:10:58,Average:,all,3.4,0,2.27,0,0,0,0.01,0,0,94.32
7:10:58,Average:,0,0,0,0,0,0,0,0,0,0,100
7:10:58,Average:,1,2,0,0.5,0,0,0,0,0,0,97.5
7:10:58,Average:,2,0,0,0.5,0,0,0,0,0,0,99.5
7:10:58,Average:,3,0.5,0,0.5,0,0,0,0,0,0,99
7:11:28,Average:,all,0.23,0,0.4,0,0,0,0.01,0,0,99.36
7:11:28,Average:,0,0,0,0,0,0,0,0.5,0,0,99.5
7:11:28,Average:,1,0,0,0,0,0,0,0,0,0,100
7:11:28,Average:,2,0,0,0,0,0,0,0,0,0,100
7:11:28,Average:,3,0.5,0,0.5,0,0,0,0,0,0,99
7:11:58,Average:,all,0.24,0,0.39,0,0,0,0.01,0,0,99.36
7:11:58,Average:,0,0,0,0,0,0,0,0,0,0,100
7:11:58,Average:,1,0.5,0,0,0,0,0,0,0,0,99.5
7:11:58,Average:,2,0,0,0.5,0,0,0,0,0,0,99.5
7:11:58,Average:,3,1.5,0,3,0,0,0,0,0,0,95.5
...
7:18:00,Average:,all,2.74,0,1.03,1.9,0,0.02,0.02,0,0,94.29
7:18:00,Average:,0,0,0,1.04,4.15,0,0.52,0,0,0,94.3
7:18:00,Average:,1,0,0,0,0,0,0,0,0,0,100
7:18:00,Average:,2,70.85,0,6.53,3.52,0,0,0,0,0,19.1
7:18:00,Average:,3,0,0,0.5,0,0,0,0,0,0,99.5
7:18:30,Average:,all,1.9,0,0.93,1.67,0,0.04,0.01,0,0,95.45
7:18:30,Average:,0,0,0,0.51,0,0,0,0,0,0,99.49
7:18:30,Average:,1,0.5,0,0.5,0,0,0,0.5,0,0,98.51
7:18:30,Average:,2,0,0,0.5,0,0,0,0.5,0,0,99
7:18:30,Average:,3,0,0,0,0,0,0,0,0,0,100

我已经写了下面的代码,但是没有下拉选项。在输出html的所有图表中都需要下拉菜单时,需要帮助。

import sys
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

csv_path = "/graph/plotly_dropdown/mpstat.csv"
html_path = "/graph/plotly_dropdown/mpstat.html"

df = pd.read_csv(csv_path)
df_sorted_by_time = df.sort_values(["TIME_UTC"], axis=0, ascending=True)



graph1 = px.line(df_sorted_by_time, x='TIME_UTC', y='USER', color='CPU_CORE', title='Avg %User CPU')
graph2 = px.line(df_sorted_by_time, x='TIME_UTC', y='NICE', color='CPU_CORE', title='Avg %Nice CPU')
graph3 = px.line(df_sorted_by_time, x='TIME_UTC', y='SYSTEM', color='CPU_CORE', title='Avg %System CPU')
graph4 = px.line(df_sorted_by_time, x='TIME_UTC', y='IOWAIT', color='CPU_CORE', title='Avg %iowait CPU')

with open(html_path, 'w') as f:
    f.write(graph1.to_html(full_html=False, include_plotlyjs='cdn'))
    f.write(graph2.to_html(full_html=False, include_plotlyjs='cdn'))
    f.write(graph3.to_html(full_html=False, include_plotlyjs='cdn'))
    f.write(graph4.to_html(full_html=False, include_plotlyjs='cdn'))

注意:-我确实仅出于某些特定原因希望使用plotly。

以下一个示例图:- enter image description here

0 个答案:

没有答案