基于Plot ly Dash 使用 Python 开发交互式互动数据图

本文介绍了如何在Anaconda3环境中安装Plot.ly Dash,并通过Python编写代码,创建一个包含滑块交互的动态数据图。教程详细解释了组件的导入、Dash应用程序的初始化、滑块组件的配置以及回调函数的设定,展示了如何实现数据图随滑块变化的效果。
摘要由CSDN通过智能技术生成

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                       

基于Plot.ly Dash 使用 Python 开发交互式互动数据图

在 Anaconda3 5.0.0 中安装 Plot.ly Dash

http://blog.csdn.net/hu_zhenghui/article/details/78303032

代码分析

导入 dash

import dash
  
  
  
  • 1

导入 dash_core_components,本例中会用到 dash_core_components.Graph 用于显示数据图, dash_core_components.Slider 用于显示滑块

import dash_core_components
  
  
  
  • 1

导入 dash_html_components,本例中使用 dash_html_components.Div 包裹 dash_core_components 的元素

import dash_html_components
  
  
  
  • 1

导入 plotly

import plotly
  
  
  
  • 1

导入 numpy

import numpy
  
  
  
  • 1

dash 中创建一个 Dash 应用程序

app = dash.Dash()
  
  
  
  • 1

app 的类型为 dash.dash.Dash

app?
  
  
  
  • 1

    Type:        Dash
    String form: <dash.dash.Dash object at 0x000001DBB8F2BB70>
    File:        c:\programdata\anaconda3\lib\site-packages\dash\dash.py
    Docstring:   <no docstring>
    

dash_core_components 中创建一个 dash_core_components.Graph 数据图组件

dash_core_components.Graph(id='graph')
  
  
  
  • 1
Graph('graph')
  
  
  
  • 1

dash_core_components.Graph 的参数较多,本例只使用 id,用于后面的 dash.dependencies.Output 中的组件 id

dash_core_components.Graph?
  
  
  
  • 1

    Init signature: dash_core_components.Graph(**kwargs)
    Docstring:     
    A Graph component.
    
    
    Keyword arguments:
    - id (string; required)
    - clickData (dict; optional): Data from latest click event
    - hoverData (dict; optional): Data from latest hover event
    - clear_on_unhover (boolean; optional): If True, clear&#95;on&#95;unhover will clear the hoverData property
    when the user "unhovers" from a point.
    If False, then the hoverData property will be equal to the
    data from the last point that was hovered over.
    - selectedData (dict; optional): Data from latest select event
    - relayoutData (dict; optional): Data from latest relayout event which occurs
    when the user zooms or pans on the plot
    - figure (dict; optional): Plotly figure object. See schema:
    https://plot.ly/javascript/reference
    - style (dict; optional): Generic style overrides on the plot div
    - className (string; optional): className of the parent div
    - animate (boolean; optional): Beta: If true, animate between updates using
    plotly.js's animate function
    - animation_options (dict; optional): Beta: Object containing animation settings.
    Only applies if animate is true
    - config (optional): Plotly.js config options.
    See https://plot.ly/javascript/configuration-options/
    for more info.. config has the following type: dict containing keys 'staticPlot', 'editable', 'edits', 'autosizable', 'queueLength', 'fillFrame', 'frameMargins', 'scrollZoom', 'doubleClick', 'showTips', 'showAxisDragHandles', 'showAxisRangeEntryBoxes', 'showLink', 'sendData', 'linkText', 'displayModeBar', 'modeBarButtonsToRemove', 'modeBarButtonsToAdd', 'modeBarButtons', 'displaylogo', 'plotGlPixelRatio', 'topojsonURL', 'mapboxAccessToken'.
    Those keys have the following types: 
      - staticPlot (boolean; optional): no interactivity, for export or image generation
      - editable (boolean; 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python提供了多个库来实现交互式数据可视化展示,其中比较流行的有: 1. Matplotlib:一个基于Python的绘库,可以绘制静态像和动态像。 2. Plotly:一个基于JavaScript的绘库,可以生成交互式表和报告。 3. Bokeh:一个基于JavaScript的绘库,可以生成交互式的Web应用程序。 下面以Matplotlib和Plotly为例,介绍如何实现交互式数据可视化展示。 ## 使用Matplotlib绘制交互式数据可视化展示 Matplotlib提供了多个工具包,其中包括`mpld3`和`bokeh`,可以实现交互式数据可视化展示。 ### 使用mpld3 mpld3是一个Matplotlib的插件,可以将Matplotlib生成的静态像转换为交互式像。 安装mpld3: ```python pip install mpld3 ``` 以下是一个示例代码,可以绘制交互式的散点: ```python import matplotlib.pyplot as plt import mpld3 # 生成数据 x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] # 绘制散点 fig, ax = plt.subplots() ax.scatter(x, y) # 转换为交互式像 mpld3.show(fig) ``` 运行代码后,会显示一个包含散点的窗口,可以使用鼠标滚轮缩放像,也可以单击数据点查看数据。 ### 使用bokeh bokeh是一个Python交互式可视化库,可以生成HTML、JavaScript和SVG格式的像。 安装bokeh: ```python pip install bokeh ``` 以下是一个示例代码,可以绘制交互式的散点: ```python from bokeh.plotting import figure, output_file, show # 设置输出文件的路径 output_file("scatter.html") # 生成数据 x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] # 绘制散点 p = figure() p.scatter(x, y) # 显示像 show(p) ``` 运行代码后,会在当前目录下生成一个名为`scatter.html`的文件,可以用浏览器打开该文件查看交互式散点。 ## 使用Plotly绘制交互式数据可视化展示 Plotly是一个基于JavaScript的绘库,可以生成交互式表和报告。Plotly提供了Python、R、MATLAB和Julia等多个语言的接口。 安装Plotly: ```python pip install plotly ``` 以下是一个示例代码,可以绘制交互式的散点: ```python import plotly.graph_objs as go import plotly.offline as pyo # 生成数据 x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] # 绘制散点 data = [go.Scatter(x=x, y=y, mode='markers')] # 设置布局 layout = go.Layout(title='Scatter Plot') # 绘制像 fig = go.Figure(data=data, layout=layout) # 显示像 pyo.iplot(fig) ``` 运行代码后,会在浏览器上显示一个交互式散点,可以使用鼠标滚轮缩放像,也可以单击数据点查看数据
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值