python数组plot_有没有可以渲染numpy数组数据的python plotly / dash图像小部件?

1586010002-jmsa.png

I'm in the process of evaluating python plotly and/or dash as an alternative to bokeh/holoviews for linked plots that update images.

Requirements:

linking data point to image:

I have scatter plots and heatmaps in which individual data points represent values derived from images. I would like to link back from a data point in a scatterplot to the image that the numerical value for this data point was derived from.

The image data is in a numpy array or can be provided by a callback function. I would like to avoid writing a .png file to disk and embedding the png file in a html element.

linking image selections to data points:

e.g. Display an image. Update a plot according to the selection in the image (e.g. a simple histogram).

However, I can't seem to find any widget for image display in plotly/dash. Am I missing something or is there really no such thing?

解决方案I would like to link back from a data point in a scatterplot to the image that the numerical value for this data point was derived from.

See https://plot.ly/dash/interactive-graphing. You can assign a callback to selectedData, hoverData, or clickData property of the dash_core_components.Graph.

linking image selections to data points: e.g. Display an image. Update a plot according to the selection in the image (e.g. a simple histogram).

You could display a background image on a plotly graph and then use the same selectedData tools to update callbacks based off of the selected region. Here is a simple example:

import dash

from dash.dependencies import Input, Output

import dash_core_components as dcc

import dash_html_components as html

import base64

import json

app = dash.Dash()

app.css.append_css({'external_url': 'https://codepen.io/chriddyp/pen/dZVMbK.css'})

RANGE = [0, 1]

def InteractiveImage(id, image_path):

encoded_image = base64.b64encode(open(image_path, 'rb').read())

return dcc.Graph(

id=id,

figure={

'data': [],

'layout': {

'xaxis': {

'range': RANGE

},

'yaxis': {

'range': RANGE,

'scaleanchor': 'x',

'scaleratio': 1

},

'height': 600,

'images': [{

'xref': 'x',

'yref': 'y',

'x': RANGE[0],

'y': RANGE[1],

'sizex': RANGE[1] - RANGE[0],

'sizey': RANGE[1] - RANGE[0],

'sizing': 'stretch',

'layer': 'below',

'source': 'data:image/png;base64,{}'.format(encoded_image)

}],

'dragmode': 'select' # or 'lasso'

}

}

)

app.layout = html.Div([

html.Div(className='row', children=[

html.Div(InteractiveImage('image', 'dash_app.png'), className='six columns'),

html.Div(dcc.Graph(id='graph'), className='six columns')

]),

html.Pre(id='console')

])

# display the event data for debugging

@app.callback(Output('console', 'children'), [Input('image', 'selectedData')])

def display_selected_data(selectedData):

return json.dumps(selectedData, indent=2)

@app.callback(Output('graph', 'figure'), [Input('image', 'selectedData')])

def update_histogram(selectedData):

x_range = selectedData['range']['x']

x_range = selectedData['range']['y']

# filter data based off of selection in here

# for simple example purposes, we'll just display the selected RANGE

return {

'data': [{

'x': x_range,

'y': x_range,

'mode': 'markers',

'marker': {

'size': 20

}

}],

'layout': {

'xaxis': {'range': RANGE},

'yaxis': {'range': RANGE, 'scaleanchor': 'x', 'scaleratio': 1},

'height': 600

}

}

if __name__ == '__main__':

app.run_server(debug=True)

pDukO.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值