python plotly 使用教程

python plotly 使用教程1、plotly介绍lotly的Python图形库使互动的出版质量图表成为在线。如何制作线图,散点图,面积图,条形图,误差线,箱形图,直方图,热图,子图,多轴,极坐标图和气泡图的示例。推荐最好使

大家好,又见面了,我是你们的朋友全栈君。

1、plotly介绍

lotly的Python图形库使互动的出版质量图表成为在线。 如何制作线图,散点图,面积图,条形图,误差线,箱形图,直方图,热图,子图,多轴,极坐标图和气泡图的示例。
推荐最好使用jupyter notebook,使用pycharm的话不是很方便。

2、安装

pip install plotly

2、使用

1)在线使用

在setting里找到用户名和api key

 
<span role="heading" aria-level="2">python plotly 使用教程

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) 

散点图

 
<span role="heading" aria-level="2">python plotly 使用教程

散点图.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) 
 
<span role="heading" aria-level="2">python plotly 使用教程

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,) 
 
<span role="heading" aria-level="2">python plotly 使用教程

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) 
 
<span role="heading" aria-level="2">python plotly 使用教程

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) 
 
<span role="heading" aria-level="2">python plotly 使用教程

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]) 
 
<span role="heading" aria-level="2">python plotly 使用教程

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) 
 
<span role="heading" aria-level="2">python plotly 使用教程

image.png

4、各种具体语法

pdf

 
<span role="heading" aria-level="2">python plotly 使用教程

image.png

 

5、总结

画的图真是好看,而且划过的图会自动上传到云端。

 
<span role="heading" aria-level="2">python plotly 使用教程

image.png

作者:五长生

链接:https://www.jianshu.com/p/57bad75139ca

来源:简书

简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/155979.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 大数据科研解决方案「建议收藏」

    大数据科研解决方案「建议收藏」第一章建设背景1.1国家政策2017年1月工业和信息化部正式发布了《大数据产业发展规划(2016-2020年)》,明确了“十三五”时期大数据产业的发展思路、原则和目标,将引导大数据产业持续健康发展,有力支撑制造强国和网络强国建设。2018年9月工信部公示“2018年大数据产业发展试点示范项目名单”,公布了包括大数据存储管理、大数据分析挖掘、大数据安全保障、产业创新大数据应用、…

    2022年5月30日
    54
  • 业务架构师如何进阶成长[通俗易懂]

    业务架构师如何进阶成长[通俗易懂]一、前言接触业务架构工作之后,除了单位提供的方法论外,为了做好这项工作,认真学习了软件过程、系统分析与设计、架构设计、设计模式、Java语言等内容,并研读了敏捷开发、领域驱动设计、工作流分析等方面的书籍,为了拓展对新技术的理解,在人工智能和区块链两方面也阅读了一些著作,总体而言,不够系统,都是为了能够更好地理解企业级业务架构的方法论及其向技术端的传导。除了恶补基础知识,更重要的当然是实践,珍惜每一个设计任务,珍惜每一次与业务人员、技术人员的沟通机会…

    2022年10月12日
    4
  • Linux查看网卡带宽[通俗易懂]

    Linux查看网卡带宽[通俗易懂]ifconfig查看网卡信息执行命令:ethtool网卡名称,例:ethtooleth1输出内容如下:Settingsforeth1:Supportedports:[FIBRE]Supportedlinkmodes:1000baseT/Full10000baseT/FullSupportedpauseframeuse:Symmetr..

    2022年10月19日
    3
  • 云服务器需要备案吗(国内服务器免备案方法)

    国内不需要备案的服云服务器内容精选换一换不想看文字,请直接戳视频链接。可以不做备案吗?根据《互联网信息服务管理办法》等相关规定,使用中国大陆节点服务器开办的网站,必须先办理网站备案,备案成功并获取通信管理局下发的ICP备案号后才能开通访问。根据《互联网信息服务管理办法》等相关规定,使用中国大陆节点服务器开办的网站,必须先办理网站备案,备案成功并获取通信管理局下发的ICP备案号后视频点播不需要购买…

    2022年4月14日
    108
  • intelj idea 2021 激活码[免费获取]

    (intelj idea 2021 激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~MLZP…

    2022年3月20日
    222
  • busybox rootfs 与 ubuntu rootfs_boot.img root

    busybox rootfs 与 ubuntu rootfs_boot.img root下载busybox的源码,解压后,设定ARCH和CROSS_COMPILE的两个基本环境变量,选择defconfig作为默认配置,大部分的busybox工具都会被编译出来。如果不指定输出目录,默认输出到根目录的_install目录下面,如果需要指定目录,配置CONFIG_PREFIX=/a/b/c/rootfs,这样make生成的/bin,/sbin,/usr三个默认文件夹就直接在rootfs目录下。makeARCH=armCROSS_COMPILE=arm…

    2022年10月6日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号