python plotly 使用教程
1、plotly介绍
lotly的Python图形库使互动的出版质量图表成为在线。 如何制作线图,散点图,面积图,条形图,误差线,箱形图,直方图,热图,子图,多轴,极坐标图和气泡图的示例。
推荐最好使用jupyter notebook,使用pycharm的话不是很方便。
2、安装
pip install plotly
2、使用
1)在线使用
在setting里找到用户名和api key
image.png
##在线使用
import plotly.plotly as py
from plotly import tools
from plotly.graph_objs import *
tools.set_credentials_file(username='yours', api_ke='yours')trace0 = Scatter(x=[1, 2, 3, 4],y=[10, 15, 13, 17],mode='markers'
)
trace1 = Scatter(x=[1, 2, 3, 4],y=[16, 5, 11, 9]
)
data = Data([trace0, trace1])py.iplot(data)
散点图
散点图.png
2)offline
import plotly.offline as of
import plotly.graph_objs as goof.offline.init_notebook_mode(connected=True)
trace0 = go.Scatter(x=[1, 2, 3, 4],y=[10, 15, 13, 17],mode='markers'
)
trace1 = go.Scatter(x=[1, 2, 3, 4],y=[16, 5, 11, 9]
)
data = go.Data([trace0, trace1])
of.plot(data)
3、其他图
下面我们画几个其他类型的图
柱状图
import plotly.figure_factory as ff
import pandas as pddf = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/school_earnings.csv")data = [Bar(x=df.School,y=df.Gap)]py.iplot(data)
image.png
3D图
import numpy as nps = np.linspace(0, 2 * np.pi, 240)
t = np.linspace(0, np.pi, 240)
tGrid, sGrid = np.meshgrid(s, t)r = 2 + np.sin(7 * sGrid + 5 * tGrid) # r = 2 + sin(7s+5t)
x = r * np.cos(sGrid) * np.sin(tGrid) # x = r*cos(s)*sin(t)
y = r * np.sin(sGrid) * np.sin(tGrid) # y = r*sin(s)*sin(t)
z = r * np.cos(tGrid) # z = r*cos(t)surface = Surface(x=x, y=y, z=z)
data = Data([surface])layout = Layout(title='Parametric Plot',scene=Scene(xaxis=XAxis(gridcolor='rgb(255, 255, 255)',zerolinecolor='rgb(255, 255, 255)',showbackground=True,backgroundcolor='rgb(230, 230,230)'),yaxis=YAxis(gridcolor='rgb(255, 255, 255)',zerolinecolor='rgb(255, 255, 255)',showbackground=True,backgroundcolor='rgb(230, 230,230)'),zaxis=ZAxis(gridcolor='rgb(255, 255, 255)',zerolinecolor='rgb(255, 255, 255)',showbackground=True,backgroundcolor='rgb(230, 230,230)'))
)fig = Figure(data=data, layout=layout)
py.iplot(fig,)
image.png
折线图
import numpy as npN = 100
random_x = np.linspace(0, 1, N)
random_y0 = np.random.randn(N)+5
random_y1 = np.random.randn(N)
random_y2 = np.random.randn(N)-5# Create traces
trace0 = go.Scatter(x = random_x,y = random_y0,mode = 'markers',name = 'markers'
)
trace1 = go.Scatter(x = random_x,y = random_y1,mode = 'lines+markers',name = 'lines+markers'
)
trace2 = go.Scatter(x = random_x,y = random_y2,mode = 'lines',name = 'lines'
)data = [trace0, trace1, trace2]
py.iplot(data)
image.png
堆叠图
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='stack'
)fig = go.Figure(data=data, layout=layout)
py.iplot(fig)
image.png
pie
labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']
values = [4500,2500,1053,500]
colors = ['#FEBFB3', '#E1396C', '#96D38C', '#D0F9B1']trace = go.Pie(labels=labels, values=values,hoverinfo='label+percent', textinfo='value', textfont=dict(size=20),marker=dict(colors=colors, line=dict(color='#000000', width=2)))py.iplot([trace])
image.png
不知道叫什么图
title = 'Main Source for News'labels = ['Television', 'Newspaper', 'Internet', 'Radio']colors = ['rgba(67,67,67,1)', 'rgba(115,115,115,1)', 'rgba(49,130,189, 1)', 'rgba(189,189,189,1)']mode_size = [8, 8, 12, 8]line_size = [2, 2, 4, 2]x_data = [[2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013],[2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013],[2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013],[2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013],
]y_data = [[74, 82, 80, 74, 73, 72, 74, 70, 70, 66, 66, 69],[45, 42, 50, 46, 36, 36, 34, 35, 32, 31, 31, 28],[13, 14, 20, 24, 20, 24, 24, 40, 35, 41, 43, 50],[18, 21, 18, 21, 16, 14, 13, 18, 17, 16, 19, 23],
]traces = []for i in range(0, 4):traces.append(go.Scatter(x=x_data[i],y=y_data[i],mode='lines',line=dict(color=colors[i], width=line_size[i]),connectgaps=True,))traces.append(go.Scatter(x=[x_data[i][0], x_data[i][11]],y=[y_data[i][0], y_data[i][11]],mode='markers',marker=dict(color=colors[i], size=mode_size[i])))layout = go.Layout(xaxis=dict(showline=True,showgrid=False,showticklabels=True,linecolor='rgb(204, 204, 204)',linewidth=2,autotick=False,ticks='outside',tickcolor='rgb(204, 204, 204)',tickwidth=2,ticklen=5,tickfont=dict(family='Arial',size=12,color='rgb(82, 82, 82)',),),yaxis=dict(showgrid=False,zeroline=False,showline=False,showticklabels=False,),autosize=False,margin=dict(autoexpand=False,l=100,r=20,t=110,),showlegend=False,
)annotations = []# Adding labels
for y_trace, label, color in zip(y_data, labels, colors):# labeling the left_side of the plotannotations.append(dict(xref='paper', x=0.05, y=y_trace[0],xanchor='right', yanchor='middle',text=label + ' {}%'.format(y_trace[0]),font=dict(family='Arial',size=16,color=colors,),showarrow=False))# labeling the right_side of the plotannotations.append(dict(xref='paper', x=0.95, y=y_trace[11],xanchor='left', yanchor='middle',text='{}%'.format(y_trace[11]),font=dict(family='Arial',size=16,color=colors,),showarrow=False))
# Title
annotations.append(dict(xref='paper', yref='paper', x=0.0, y=1.05,xanchor='left', yanchor='bottom',text='Main Source for News',font=dict(family='Arial',size=30,color='rgb(37,37,37)'),showarrow=False))
# Source
annotations.append(dict(xref='paper', yref='paper', x=0.5, y=-0.1,xanchor='center', yanchor='top',text='Source: PewResearch Center & ' +'Storytelling with data',font=dict(family='Arial',size=12,color='rgb(150,150,150)'),showarrow=False))layout['annotations'] = annotationsfig = go.Figure(data=traces, layout=layout)
py.iplot(fig)
image.png
4、各种具体语法
image.png
5、总结
画的图真是好看,而且划过的图会自动上传到云端。
image.png
python plotly 使用教程相关推荐
- Python数据可视化教程:基于Plotly的动态可视化绘图
1. plotly 介绍 Plotly是一个非常著名且强大的开源数据可视化框架,它通过构建基于浏览器显示的web形式的可交互图表来展示信息,可创建多达数十种精美的图表和地图, 下面我们以jupyter ...
- python画直方图代码-python plotly画柱状图代码实例
这篇文章主要介绍了python plotly画柱状图代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 代码 import pandas as ...
- 【Python plotly】零基础也能轻松掌握的学习路线与参考资料
Python plotly是一个优秀的数据可视化工具,通过使用Python语言和Plotly的图表支持库,可以轻松地创建交互式和动态图表,Python plotly的可视化效果美观且易于实现. 学习路 ...
- Python培训基础教程都教哪些
根据相关数据统计,目前学习Python技术的同学大多数是零基础,都是从其他行业转型来学习的,那么Python培训基础教程都教哪些呢?好不好学呢?来看看下面的详细介绍. Python培训基础教程都教哪些 ...
- python爬虫入门教程--优雅的HTTP库requests(二)
requests 实现了 HTTP 协议中绝大部分功能,它提供的功能包括 Keep-Alive.连接池.Cookie持久化.内容自动解压.HTTP代理.SSL认证等很多特性,下面这篇文章主要给大家介绍 ...
- python3里的pillow怎么安装_“python安装pillow教程“python3.4怎么安装pil
"python安装pillow教程"python3.4怎么安装pil python安装pillow教程2020-10-09 03:37:02人已围观 如何在python3.6中装p ...
- 面向回家编程!GitHub标星两万的Python抢票教程”,我们先帮你跑了一遍
来源:大数据文摘 本文约3400字,建议阅读8分钟 本文为你介绍Python抢票教程,带你回家! 盼望着,盼望着, 春节的脚步近了, 然而,每年到这个时候, 最难的, 莫过于一张回家的火车票. 据悉, ...
- Python编程系列教程第12讲——属性和方法
视频地址:http://v.youku.com/v_show/id_XNTgyOTg4NjQ4.html 普及网络安全知识,推动信息技术发展. 为祖国的网络安全撑起一片蓝天,为网络安全爱好者构建一方家 ...
- Python编程系列教程第16讲——拷贝自身到系统目录
分享知识,分享快乐,收获友谊,收获财富! 大家好,我是数字雨,QQ:798033502 http://itbook.taobao.com/ 今天给大家带来的教程是<Python编程系列教程第16 ...
最新文章
- boost log 能不能循环覆盖_前端基础进阶(十四):深入核心,详解事件循环机制...
- VC++动态链接库(DLL)编程(四)――MFC扩展 DLL
- 使用HTML5、CSS3和jQuery增强网站用户体验
- [头脑风暴] 解读Docker Bridge网络模型
- jigsaw kaggle_使用Project Jigsaw的JDK 9 Early Access上的Eclipse IDE
- 物联网常用的组网方式浅析
- 两轮差速机器人舵机转轴示意图_机器人教程2:舵机及转向控制原理
- 如何在 OS X Yosemite 中安装 Java
- 对多个有规律表进行更新剔重复操作的存储过程(up8000)
- 清空div中的内容而不刷新整个页面_Vue中的$nextTick机制
- MVC数据验证原理及自定义ModelValidatorProvider实现无编译修改验证规则和错误信息...
- PCB 工程系统 模拟windows域帐号登入
- windows 改变用户文件路径(对所有新用户)
- 为什么蓝鸽的听力下载完还是听不了_推荐这款练习英语听力的神器级免费App
- 使用wagtail搭建cms的安装流程
- SVN Cleanup的意思
- 利用percona-toolkit 工具来检测mysql 主从数据库同步以及实现同步
- 冻豆腐怎么烧才出味儿?——红烧冻豆腐
- 天天爱消除刷分脚本(终于打败大毛了。。)
- January 2008