Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何更快地绘制绘图仪热图?_Python_Numpy_Plotly_Heatmap - Fatal编程技术网

Python 如何更快地绘制绘图仪热图?

Python 如何更快地绘制绘图仪热图?,python,numpy,plotly,heatmap,Python,Numpy,Plotly,Heatmap,我正在尝试使用python plotly制作带有滑块的热图 这是我的示例代码 import numpy as np import plotly as py import pandas as pd from PIL import Image from plotly.subplots import make_subplots import plotly.express as px import plotly.graph_objects as go import time # make test

我正在尝试使用python plotly制作带有滑块的热图

这是我的示例代码

import numpy as np
import plotly as py
import pandas as pd
from PIL import Image

from plotly.subplots import make_subplots
import plotly.express as px
import plotly.graph_objects as go

import time

# make test histogram. np.array
histogram_meter = np.random.randint(0, 500,(50,100))

# get figures
fig = go.Figure()

# add sliders
cnt_max = int(histogram_meter.max())           

if(len(tmp_histogram) != 0):
    avg = histogram_meter[histogram_meter != 0].mean()
    std = histogram_meter[histogram_meter != 0].std()
else:
    avg = 0
    std = 0
step_max  = int(avg + 2 *std)  # 2-sigma                      
step_list = list(range(0, step_max))
time_check = time.time()
for step in step_list:
    fig.add_trace(
        go.Heatmap(
            visible=False,
            z=histogram_meter,
            zsmooth='fast',
            connectgaps=True,
            colorscale=[[0, 'rgba(0, 0, 255,0)'],
                        [0.1, 'rgba(0, 0, 255,1)'],
                        [1.0, 'rgba(255, 0, 0, 1.0)']],
            zmin=0,
            zmax=step
        )
    )

print("time check for drawing heatmap = ", time.time()-time_check)
time_check = time.time()
fig.data[step_max-1].visible = True

time_check = time.time()
steps = []
for i in range(step_max):  # range(len(fig.data)):
    # time_check2 = time.time()
    step = dict(
        method="update",
        args=[
            {"visible": [False]*len(fig.data)},
            {"title": "Threshold switched to : " + str(i)}],  # layout attribute
        label=" {}".format(i)
    )
    # Toggle i'th trace to "visible"
    step["args"][0]["visible"][i] = True
    steps.append(step)
    # print("time check add 1-step to slider = ", time.time()-time_check2)

print("time check for drawing slider = ", time.time()-time_check)
time_check = time.time()

sliders = [dict(
    active=step_max-1,
    currentvalue={"prefix": "Threshold: "},
    pad={"t": 50},
    steps=steps
)]
fig.update_layout(sliders=sliders)
print("time check for update_layout(slider) = ", time.time()-time_check)
time_check = time.time()

# configure color bar
colorbar = dict(
    title="count",
    thicknessmode="pixels", thickness=50,
    lenmode="pixels", len=400,
    yanchor="top", y=1,
    ticks="outside", ticksuffix="count",
    dtick=5
)
fig.update_layout(coloraxis_colorbar=colorbar)
fig.update_layout(font_size=20)
print("time check for update_layout(color_bar) = ", time.time()-time_check)
time_check = time.time()
fig.show()

print("end")
当样本数据数为100时

time check for drawing heatmap =  0.15591144561767578
time check for drawing slider =  0.0
time check for update_layout =  0.0879526138305664
time check for update_layout(color_bar) =  0.1019136905670166
然后在第二秒内生成绘图

当样本数据数为500时

time check for drawing heatmap =  0.4447438716888428
time check for drawing slider =  0.007018089294433594
time check for update_layout(slider) =  0.47070741653442383
time check for update_layout(color_bar) =  0.08794975280761719
在这种情况下,绘制热图的时间太长。(确切地说,热图图似乎仍在加载。)

我想,我的代码中有一些问题。 有没有办法改进我的代码

你能帮我吗