Python 将轨迹添加到for循环中的Plotly Graph,仅显示循环中的最后一条轨迹
我试图为搜索到的人类基因的每个同义词名称添加痕迹,但当我在应用程序中搜索基因时,它只显示搜索到的基因和同义词列表中的最后一个基因。我不知道如何才能看到所有的同义基因与搜索到的基因以相同的颜色绘制 当前代码:Python 将轨迹添加到for循环中的Plotly Graph,仅显示循环中的最后一条轨迹,python,plotly,Python,Plotly,我试图为搜索到的人类基因的每个同义词名称添加痕迹,但当我在应用程序中搜索基因时,它只显示搜索到的基因和同义词列表中的最后一个基因。我不知道如何才能看到所有的同义基因与搜索到的基因以相同的颜色绘制 当前代码: import pandas as pd import dash import dash_core_components as dcc import dash_html_components as html import plotly.graph_objects as go from dash
import pandas as pd
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objects as go
from dash.dependencies import Input, Output
import plotly.express as px
import plotly.io as pio
from plotly.tools import make_subplots
pd.options.plotting.backend = "plotly"
pio.renderers.default = "browser"
try:
app = dash.Dash(__name__)
app.layout = html.Div([
html.Label('Search Box'),
dcc.Input(id="search_gene",
type="text",
value = '',
placeholder="Type a human gene name",
debounce = True,
minLength = 0, maxLength = 50,
autoComplete='on',
size='40'),
html.Br(),
html.Div([
html.Div([
dcc.Graph(id='mygraph')])])
])
@app.callback(
Output('mygraph', 'figure'),
[Input('search_gene', 'value')])
def update_output(search_gene):
df = pd.read_csv('list_out.txt', sep = '\t', dtype=str)
df = df.transpose().reset_index().rename(columns={'index':'Date'})
new_header = df.iloc[0]
df = df[1:]
df.columns = new_header
df = df.iloc[0:600]
df = df.set_index('Date')
lookup_df = pd.read_csv('Gene_Lookup.csv', dtype = str)
link = lookup_df.set_index('Approved_Symbol').Linked_Genes.str.split('|').to_dict()
link_date = lookup_df.set_index('Approved_Symbol').Date_Name_Changed.to_dict()
if search_gene:
search_gene = (search_gene).upper()
syns = link[search_gene]
date = link_date[search_gene]
trace1 = go.Scatter(x=df.index,
y = df[search_gene],
line_shape='linear',
line = dict(color='steelblue'),
name = search_gene)
fig = make_subplots(rows=1,subplot_titles=['Synonymous Gene Names'],
shared_yaxes=True)
fig.add_trace(trace1)
for i in syns:
fig.append_trace(go.Scatter(x=df.index,
y = df[i],
line_shape='linear',
line = dict(color='black'),
name = i), row=1, col=1)
fig.update_layout(title="Human Gene Name Occurances Per Month", xaxis_title="Date", yaxis_title="Count")
fig.add_shape(type="line", name = 'Date Human Genome Sequenced',
x0='2003-4', y0= '-10', x1='2003-4', y1=100,
line=dict(color="lightblue",width=3))
fig.add_shape(type="line", name = 'Date Name Changed',
x0=date, y0='-10', x1=date, y1=100,
line=dict(color="darkblue",width=3))
elif search_gene is None or len(search_gene) == 0:
df1 = pd.read_csv('list_out1.txt', sep='\t', dtype=str)
#df1 = df1.set_index('Date')
fig = px.line(df1, x=df1['Date'], y=df1.columns[1:])
fig.update_layout(title="Human Gene Name Occurances Per Month", xaxis_title="Date", yaxis_title="Count")
return fig
if __name__ == '__main__':
app.run_server(debug=True)
except:
pass
正在绘制的数据(df):
基因查找文件:
Approved_Symbol ... Linked_Genes
0 A12M1~ ... NaN
1 A2MRAP~ ... NaN
2 ASIC1 ... ACCN2|BNaC2|hBNaC2
3 BNaC2 ... ACCN2|ASIC1|hBNaC2
4 hBNaC2 ... ACCN2|ASIC1|BNaC2
@请提供您的数据集样本。如果你花三分钟阅读,你得到有用答案的机会会大大增加。@vestland好的,我更新了。我不确定是否需要这些数据来解决这个问题issue@LaurenKirsh你看过我之前评论中的链接了吗?@LaurenKirsh请提供你的数据集样本。如果你花三分钟阅读,你得到有用答案的机会会大大增加。@vestland好的,我更新了。我不确定是否需要这些数据来解决这个问题issue@LaurenKirsh你看过我之前评论中的链接了吗?
Approved_Symbol ... Linked_Genes
0 A12M1~ ... NaN
1 A2MRAP~ ... NaN
2 ASIC1 ... ACCN2|BNaC2|hBNaC2
3 BNaC2 ... ACCN2|ASIC1|hBNaC2
4 hBNaC2 ... ACCN2|ASIC1|BNaC2