基于python plotly交互式图表大全


Posted in Python onDecember 07, 2019

plotly可以制作交互式图表,直接上代码:

import plotly.offline as py
from plotly.graph_objs import Scatter, Layout
import plotly.graph_objs as go
py.init_notebook_mode(connected=True)
import pandas as pd
import numpy as np

In [412]:

#读取数据
df=pd.read_csv('seaborn.csv',sep=',',encoding='utf-8',index_col=0)
#展示数据
df.head()
Out[412]:
Name Type 1 Type 2 Total HP Attack Defense Sp. Atk Sp. Def Speed Stage Legendary
#
1 Bulbasaur Grass Poison 318 45 49 49 65 65 45 1 False
2 Ivysaur Grass Poison 405 60 62 63 80 80 60 2 False
3 Venusaur Grass Poison 525 80 82 83 100 100 80 3 False
4 Charmander Fire NaN 309 39 52 43 60 50 65 1 False
5 Charmeleon Fire NaN 405 58 64 58 80 65 80 2 False

In [413]:

#plotly折线图,trace就代表折现的条数
trace1=go.Scatter(x=df['Attack'],y=df['Defense'])
trace1=go.Scatter(x=[1,2,3,4,5],y=[2,1,3,5,2])
trace2=go.Scatter(x=[1,2,3,4,5],y=[2,1,4,6,7])
py.iplot([trace1,trace2])

基于python plotly交互式图表大全

#填充区域
trace1=go.Scatter(x=[1,2,3,4,5],y=[2,1,3,5,2],fill="tonexty",fillcolor="#FF0")
py.iplot([trace1])

基于python plotly交互式图表大全

# 散点图
trace1=go.Scatter(x=[1,2,3,4,5],y=[2,1,3,5,2],mode='markers')
trace1=go.Scatter(x=df['Attack'],y=df['Defense'],mode='markers')
py.iplot([trace1],filename='basic-scatter')

基于python plotly交互式图表大全

#气泡图
x=df['Attack']
y=df['Defense']
colors = np.random.rand(len(x))#set color equal to a variable
sz =df['Defense']
fig = go.Figure()
fig.add_scatter(x=x,y=y,mode='markers',marker={'size': sz,'color': colors,'opacity': 0.7,'colorscale': 'Viridis','showscale': True})
py.iplot(fig)

基于python plotly交互式图表大全

#bar 柱状图
df1=df[['Name','Defense']].sort_values(['Defense'],ascending=[0])
data = [go.Bar(x=df1['Name'],y=df1['Defense'])]
py.iplot(data, filename='jupyter-basic_bar')

基于python plotly交互式图表大全

#组合bar  group
trace1 = go.Bar(x=['giraffes', 'orangutans', 'monkeys'],y=[20, 14, 23],name='SF Zoo')
trace2 = go.Bar(x=['giraffes', 'orangutans', 'monkeys'],y=[12, 18, 29],name='LA Zoo')
data = [trace1, trace2]
layout = go.Layout( barmode='group')
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='grouped-bar')

基于python plotly交互式图表大全

#组合bar  gstack上下组合
trace1 = go.Bar(x=['giraffes', 'orangutans', 'monkeys'],y=[20, 14, 23],name='SF Zoo')
trace2 = go.Bar(x=['giraffes', 'orangutans', 'monkeys'],y=[12, 18, 29],name='LA Zoo',text=[12, 18, 29],textposition = 'auto')
data = [trace1, trace2]
layout = go.Layout( barmode='stack')
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='grouped-bar')

基于python plotly交互式图表大全

#饼图
fig = {
 "data": [
  {
   "values": df['Defense'][0:3],
   "labels": df['Name'][0:3],
   "domain": {"x": [0,1]},
   "name": "GHG Emissions",
   "hoverinfo":"label+percent+name",
   "hole": .4,
   "type": "pie"
  }
    ],
  
 "layout": {
    "title":"Global Emissions 1990-2011",
    "annotations": [
      {
        "font": {"size": 20},
        "showarrow": False,
        "text": "GHG",
        "x": 0.5,
        "y": 0.5
      }
            ]
      }
  }
py.iplot(fig, filename='donut')

基于python plotly交互式图表大全

# Learn about API authentication here: https://plot.ly/pandas/getting-started
# Find your api_key here: https://plot.ly/settings/api
#雷达图
data = [
  go.Scatterpolar(
   r = [39, 28, 8, 7, 28, 39],
   theta = ['A','B','C', 'D', 'E', 'A'],
   fill = 'toself',
   name = 'Group A'
  ),
  go.Scatterpolar(
   r = [1.5, 10, 39, 31, 15, 1.5],
   theta = ['A','B','C', 'D', 'E', 'A'],
   fill = 'toself',
   name = 'Group B'
  )
]
 
layout = go.Layout(
 polar = dict(
  radialaxis = dict(
   visible = True,
   range = [0, 50]
  )
 ),
 showlegend = False
)
 
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename = "radar/multiple")

基于python plotly交互式图表大全

#box 箱子图
df_box=df[['HP','Attack','Defense','Speed']]
data = []
for col in df_box.columns:
  data.append(go.Box(y=df_box[col], name=col, showlegend=True ) )
#data.append( go.Scatter(x= df_box.columns, y=df.mean(), mode='lines', name='mean' ) )
py.iplot(data, filename='pandas-box-plot')

基于python plotly交互式图表大全

#箱子图加平均线
df_box=df[['HP','Attack','Defense','Speed']]
data = []
for col in df_box.columns:
  data.append(go.Box(y=df_box[col], name=col, showlegend=True) )
data.append( go.Scatter(x= df_box.columns, y=df.mean(), mode='lines', name='mean' ) )
py.iplot(data, filename='pandas-box-plot')

基于python plotly交互式图表大全

#Basic Horizontal Bar Chart 条形图 plotly条形图
df_hb=df[['Name','Attack','Defense','Speed']][0:5].sort_values(['Attack'],ascending=[1])
data = [
  go.Bar(
    y=df_hb['Name'], # assign x as the dataframe column 'x'
    x=df_hb['Attack'],
    orientation='h',
    text=df_hb['Attack'],
    textposition = 'auto'
  )
]
py.iplot(data, filename='pandas-horizontal-bar')

基于python plotly交互式图表大全

#直方图Histogram
data = [go.Histogram(x=df['Attack'])]
py.iplot(data, filename='basic histogram')

基于python plotly交互式图表大全

#distplot
import plotly.figure_factory as ff 
hist_data =[df['Defense']]
group_labels = ['distplot']
fig = ff.create_distplot(hist_data, group_labels)
# Add title
fig['layout'].update(title='Hist and Rug Plot',xaxis=dict(range=[0,200]))
py.iplot(fig, filename='Basic Distplot')

基于python plotly交互式图表大全

# Add histogram data
x1 = np.random.randn(200)-2 
x2 = np.random.randn(200) 
x3 = np.random.randn(200)+2 
x4 = np.random.randn(200)+4 
 
# Group data together
hist_data = [x1, x2, x3, x4]
group_labels = ['Group 1', 'Group 2', 'Group 3', 'Group 4']
# Create distplot with custom bin_size
fig = ff.create_distplot(hist_data, group_labels,)
# Plot!
py.iplot(fig, filename='Distplot with Multiple Datasets')

基于python plotly交互式图表大全

好了,以上就是我研究的plotly,欢迎朋友们评论,补充,一起学习!

以上这篇基于python plotly交互式图表大全就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python网络编程学习笔记(三):socket网络服务器
Jun 09 Python
Python爬虫之xlml解析库(全面了解)
Aug 08 Python
Python下载网络小说实例代码
Feb 03 Python
Python比较2个时间大小的实现方法
Apr 10 Python
python日期相关操作实例小结
Jun 24 Python
Pytorch加载部分预训练模型的参数实例
Aug 18 Python
windows下Python安装、使用教程和Notepad++的使用教程
Oct 06 Python
python实现视频读取和转化图片
Dec 10 Python
Pytorch 保存模型生成图片方式
Jan 10 Python
python实现五子棋游戏(pygame版)
Jan 19 Python
解决python运行启动报错问题
Jun 01 Python
python 安全地删除列表元素的方法
Mar 16 Python
Python数据可视化:顶级绘图库plotly详解
Dec 07 #Python
python将时分秒转换成秒的实例
Dec 07 #Python
pandas实现将日期转换成timestamp
Dec 07 #Python
Python 装饰器原理、定义与用法详解
Dec 07 #Python
Python Pandas 转换unix时间戳方式
Dec 07 #Python
Pandas-Cookbook 时间戳处理方式
Dec 07 #Python
Python数据可视化:饼状图的实例讲解
Dec 07 #Python
You might like
杏林同学录(一)
2006/10/09 PHP
php操作mysql数据库的基本类代码
2014/02/25 PHP
支持生僻字且自动识别utf-8编码的php汉字转拼音类
2014/06/27 PHP
使用Thinkphp框架开发移动端接口
2015/08/05 PHP
PHP实现对数组分页处理实例详解
2017/02/07 PHP
jquery tools之tooltip
2009/07/25 Javascript
javascript instanceof,typeof的区别
2010/03/24 Javascript
基于jquery的文本框与autocomplete结合使用(asp.net+json)
2012/05/30 Javascript
JavaScript 用Node.js写Shell脚本[译]
2012/09/20 Javascript
javascript内置对象arguments详解
2014/03/16 Javascript
调整小数的格式保留小数点后两位
2014/05/14 Javascript
zeroclipboard 单个复制按钮和多个复制按钮的实现方法
2014/06/14 Javascript
jQuery中slideUp 和 slideDown 的点击事件
2015/02/26 Javascript
详解Node.js access_token的获取、存储及更新
2017/06/20 Javascript
原生JS实现移动端web轮播图详解(结合Tween算法造轮子)
2017/09/10 Javascript
微信小程序实现列表下拉刷新上拉加载
2020/07/29 Javascript
微信小程序实现星级评分和展示
2018/07/05 Javascript
解决Layui中layer报错的问题
2019/09/03 Javascript
[59:35]DOTA2上海特级锦标赛主赛事日 - 3 败者组第三轮#1COL VS Alliance第二局
2016/03/04 DOTA
python实现类的静态变量用法实例
2015/05/08 Python
Python中list初始化方法示例
2016/09/18 Python
Python内置模块ConfigParser实现配置读写功能的方法
2018/02/12 Python
使用Python实现在Windows下安装Django
2018/10/17 Python
Python爬虫beautifulsoup4常用的解析方法总结
2019/02/25 Python
Python IDE Pycharm中的快捷键列表用法
2019/08/08 Python
关于ResNeXt网络的pytorch实现
2020/01/14 Python
Selenium自动化测试工具使用方法汇总
2020/06/12 Python
html5简介_动力节点Java学院整理
2017/07/07 HTML / CSS
小学中秋节活动方案
2014/02/06 职场文书
财务科科长岗位职责
2014/03/10 职场文书
授权收款委托书
2014/09/23 职场文书
护士求职简历自我评价
2015/03/10 职场文书
家长必看:义务教育,不得以面试 评测等名义选拔学生
2019/07/09 职场文书
oracle通过存储过程上传list保存功能
2021/05/12 Oracle
spring项目中切面及AOP的使用方法
2021/06/26 Java/Android
JMeter对MySQL数据库进行压力测试的实现步骤
2022/01/22 MySQL