Python Plotly:创建一个带有下拉菜单的绘图直方图图,然后将其放入HTML中
目标:使用下拉菜单创建一个模块直方图,然后将其放入HTML中。 HTML将具有多种不同类型的图形(在此问题中未提及)。
我实际上具有许多列的大数据框/CSV。但是对于这个问题,我正在考虑一个非常简单的CSV/DataFrame。 CSV/DataFrame有三列 - 主机,请求,事件。以下是样本CSV。
HOST,REQUEST,INCIDENT
host1,GET,error
host1,GET,warning
host1,GET,warning
host1,POST,warning
host1,POST,error
host1,POST,warning
host2,GET,warning
host2,GET,error
host2,GET,warning
host2,POST,error
host2,POST,warning
host2,POST,error
host3,GET,error
host3,GET,error
host3,GET,error
host3,POST,error
host3,POST,error
host3,POST,warning
host4,GET,warning
host4,GET,error
host4,GET,error
host4,POST,error
host4,POST,warning
host4,POST,warning
目前,我正在为每个主机绘制“请求与事件”的单独直方图图,然后从中创建一个HTML。意味着,如果有四个不同的主机,那么我正在绘制HTML中的四个不同的直方图图。 以下是我的代码。
import pandas as pd
import plotly.express as px
print(f"START")
df = pd.read_csv("dropdown.csv")
hosts = list(df['HOST'].unique())
print(hosts)
for host in hosts:
title = "Dropdown grap for host = " + host
df1 = df.loc[(df['HOST'] == host)]
graph = px.histogram(df1, x='REQUEST', color='INCIDENT', title=title)
with open("dropdown.html", 'a') as f:
f.write(graph.to_html(full_html=False, include_plotlyjs=True))
print(f"END")
但是我的目标是在输出HTML中绘制单个直方图图,主机是下拉列表。我应该能够从下拉菜单中选择不同的主机,以获取每个相应主机的图形。 使用Plotly Express,我没有任何选择来实现所需的输出。需要帮助。 ,特别是如果我可以使用情节实现这一目标。表达本身会很棒! 也欢迎其他选项。
Objective: Create a plotly HISTOGRAM with a dropdown menu and put it in a html. The html will have multiple different types of graphs (not mentioned in this question).
I actually have a large dataframe/csv with many columns; but for this question I'm considering a very SIMPLE csv/dataframe.
The csv/dataframe has three columns - HOST,REQUEST,INCIDENT. Below is the sample csv.
HOST,REQUEST,INCIDENT
host1,GET,error
host1,GET,warning
host1,GET,warning
host1,POST,warning
host1,POST,error
host1,POST,warning
host2,GET,warning
host2,GET,error
host2,GET,warning
host2,POST,error
host2,POST,warning
host2,POST,error
host3,GET,error
host3,GET,error
host3,GET,error
host3,POST,error
host3,POST,error
host3,POST,warning
host4,GET,warning
host4,GET,error
host4,GET,error
host4,POST,error
host4,POST,warning
host4,POST,warning
Currently I'm plottting separate HISTOGRAM graphs for 'REQUEST Vs INCIDENT' for each HOST and then creating a html out of it. Means if there're four different hosts, then I'm plotting four different HISTOGRAM graphs in my html.
Below is my code.
import pandas as pd
import plotly.express as px
print(f"START")
df = pd.read_csv("dropdown.csv")
hosts = list(df['HOST'].unique())
print(hosts)
for host in hosts:
title = "Dropdown grap for host = " + host
df1 = df.loc[(df['HOST'] == host)]
graph = px.histogram(df1, x='REQUEST', color='INCIDENT', title=title)
with open("dropdown.html", 'a') as f:
f.write(graph.to_html(full_html=False, include_plotlyjs=True))
print(f"END")
Below is my output html having four graphs
But My Objective is to plot a single HISTOGRAM graph in my output html, with HOST being the dropdown. I should be able to select different HOSTs from the dropdown to get graph for each respective HOST.
Using plotly express I'm NOT getting any option to achieve my required output. Need help with this. Especially if I can achieve this using plotly.express itself that'll be great!
Other options are also welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以循环遍历所有可能的主机,并使用
fig = px.histogram(df_subset_by_host,x ='request',color ='iscod')
,然后提取存储在中的x数组数据中图。_DATA
对象,并将此数据分配给每个主机选择按钮的“ X” arg。例如:
You can loop through all possible hosts, and create a corresponding fig using
fig = px.histogram(df_subset_by_host, x='REQUEST', color='INCIDENT')
, then extract the x array data stored in thefig._data
object, and assign this data to the "x" arg of each host selection button.For example: