python安装plotly教程_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_key='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 go
of.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 pd
df = 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 np
s = 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 np
N = 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 plot
annotations.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 plot
annotations.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'] = annotations
fig = go.Figure(data=traces, layout=layout)
py.iplot(fig)
image.png
4、各种具体语法
image.png
5、总结
画的图真是好看,而且划过的图会自动上传到云端。
image.png
python安装plotly教程_python plotly 使用教程相关推荐
- python图形绘制星空图_Python数据可视化教程:基于Plotly的动态可视化绘图
1. plotly 介绍 Plotly是一个非常著名且强大的开源数据可视化框架,它通过构建基于浏览器显示的web形式的可交互图表来展示信息,可创建多达数十种精美的图表和地图, 下面我们以jupyter ...
- python安装系统要求_python需要什么系统 | window重装系统教程
python的spyder怎么打开,或者具体的安装教程 1.spyder 打https://pypi.python.org/pypi/spyder 下载里面最新的源码zip包,在解压后,cmd里cd到 ...
- python导入模块教程_Python 极简教程(二十四) - 导入模块
每种语言都有一些现成的模块可以调用,这些模块提供各种各样的功能,比如 time 模块提供关于时间的处理,re 模块提供正则表达式的函数, os 模块提供系统级别的操作等. 这些模块存在于 Python ...
- python安装 pip_多版本Python安装pip及pip版本管理终极教程
有时候电脑或服务器里需要安装多个版本的Python,比如Python2.7.Python3.5.Python3.6.同时存在多个版本的Python,如何安装对应的pip成为一个头疼的问题,本文从原理入 ...
- python安装多少位_python安装流程
如何安装??? 在python的官网下载python版本,需要下载对应版本(在计算机-属性中查看自己是32位操作系统还是64位操作系统),我是64位的(Windows10系统),就下载64位对应的安装 ...
- python安装失败了_python安装库安装失败怎么解决
详细内容 相信很多刚开始入门Python的菜鸟们在安装python第三方库的时候,多多少少都会遇到一些安装失败的问题. 下面,我将结合自身经验,分享一下在windows操作系统上此类问题的解决办法. ...
- python安装默认路径_Python pip install如何修改默认下载路径
Python pip install如何修改默认下载路径 pip动不动就下载数百M的文件.这些文件默认在C:盘,那么为了节省空间需要修改这些路径: 打开cmd命令窗口.输入: python -m si ...
- python安装离线包_python离线包
广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 那么我现在开发了一套python程序,需要一些模块,怎么运行? 二.离线包制作有 ...
- python安装gdal包_python安装gdal的两种方法
1.不用手动下载文件,直接执行以下命令即可 conda install gdal 2.首先,下载gdal的whl文件 链接, 官网下载比较慢,GDAL-2.2.4-cp27-cp27m-win_am ...
- python安装oracle驱动_python安装oracle扩展及数据库连接方法
python安装oracle扩展及数据库连接方法 这篇文章主要介绍了 python 安装 oracle 扩展及数据库连接方法, 较为详细的分析了 Python 下载 oracle 扩展及 Window ...
最新文章
- WIN7 任务栏放右侧 有个BUG
- TCP协议的粘包问题(数据的无边界性)
- mysql 123456_$myconn=mysql_connect(192.168.1.xxx,root,123456);怎么连不上数据库啊?
- Linux 基础命令讲解--加密解密
- css 背景色渐变 background linear-gradient
- Go语言反射之反射调用
- 一个把时间戳转成日期的javascript函数
- python装饰器详解-python中的装饰器详解
- asp.net_php_jsp,对ASP、JSP、PHP、ASP.NET进行实际应用%统计
- android studio 上手使用 大水逼问题
- 535. Encode and Decode TinyURL - LeetCode
- JS手册和参考教程网址
- 华为路由器配置备忘录
- 我的vscode插件和setting设置(解决vscode保存出现提示运行“XXX“的保存参与者: 快速修复“的问题;二来修复“明明开启的是去分号和单引号,自动保存又自动添加了分号和双引号)
- 福建农林大学校赛(同步赛)
- 仿抖音写上下滑动切换视频
- 强烈安利:小顽简报,一款高效实用的PPT新插件
- 函数function的使用方法
- 数据库审计:DDL与DML
- MVNO忽略国内漫游(ignore national roaming)
热门文章
- CSDN如何获得积分?
- MySQL书籍和资料
- VS2017下载安装
- Java后端开发实习记录
- 「Activiti精品 悟纤出品」开发一个简单的SpringBoot activiti应用 - 第330篇
- oracle分析函数over(Partition by...)及开窗函数详解
- 电力网络故障的计算机算法,电力系统故障分析的计算机算法.pdf
- 给pmbus添加mfr_id, mfr_version, mfr_model节点显示
- 强烈推荐《全景探秘游戏设计艺术》
- MSI Afterburner 官网链接指南