python柱状图第四节(python使用Plotly绘图工具绘制柱状图)
python柱状图第四节
python使用Plotly绘图工具绘制柱状图本文实例为大家分享了python使用plotly绘图工具绘制柱状图的具体代码,供大家参考,具体内容如下
使用plotly绘制基本的柱状图,需要用到的函数是graph_objs 中 bar函数
通过参数,可以设置柱状图的样式。
通过barmod进行设置可以绘制出不同类型的柱状图出来。
我们先来实现一个简单的柱状图:
|
# -*- coding: utf-8 -*- import plotly as py import plotly.graph_objs as go pyplt = py.offline.plot # trace trace_basic = [go.bar( x = [ 'variable_1' , 'variable_2' , 'variable_3' , 'variable_4' , 'variable_5' ], y = [ 1 , 2 , 3 , 2 , 4 ], )] # layout layout_basic = go.layout( title = 'the graph title' , xaxis = go.xaxis( range = [ - 0.5 , 4.5 ], domain = [ 0 , 1 ]) ) # figure figure_basic = go.figure(data = trace_basic, layout = layout_basic) # plot pyplt(figure_basic, filename = 'tmp/1.html' ) |
上面这个例子,就是一个简单的柱状图。
下面我们讲下另外一种图,柱状簇
实现过程则是,在基本的柱状图中,加入多租数据即可实现,柱状簇
|
import plotly as py import plotly.graph_objs as go pyplt = py.offline.plot # traces trace_1 = go.bar( x = [ "西南石油" , "东方明珠" , "海泰发展" ], y = [ 4.12 , 5.32 , 0.60 ], name = "201609" ) trace_2 = go.bar( x = [ "西南石油" , "东方明珠" , "海泰发展" ], y = [ 3.65 , 6.14 , 0.58 ], name = "201612" ) trace_3 = go.bar( x = [ "西南石油" , "东方明珠" , "海泰发展" ], y = [ 2.15 , 1.35 , 0.19 ], name = "201703" ) trace = [trace_1, trace_2, trace_3] # layout layout = go.layout( title = '净资产收益率对比图' ) # figure figure = go.figure(data = trace, layout = layout) # plot pyplt(figure, filename = 'tmp/2.html' ) |
执行上述代码,我们可以看到如上图所示柱状簇图例
可将数据堆叠生成。
接下来在讲讲如何绘制层叠柱状图
层叠柱状图的绘制方法与柱状簇的绘制方法基本差不多
也就是对同一个柱状簇进行叠加,实现方法是对layout中的barmode属性进行设置
barmode = 'stack'
其余参数,与柱状簇相同。
|
# -*- coding: utf-8 -*- import plotly as py import plotly.graph_objs as go pyplt = py.offline.plot # stacked bar chart trace_1 = go.bar( x = [ '深证50' , '上证50' , '西南50' , '西北50' , '华中50' ], y = [ 0.7252 , 0.9912 , 0.5347 , 0.4436 , 0.9911 ], name = '股票投资' ) trace_2 = go.bar( x = [ '深证50' , '上证50' , '西南50' , '西北50' , '华中50' ], y = [ 0.2072 , 0 , 0.4081 , 0.4955 , 0.02 ], name = '其它投资' ) trace_3 = go.bar( x = [ '深证50' , '上证50' , '西南50' , '西北50' , '华中50' ], y = [ 0 , 0 , 0.037 , 0 , 0 ], name = '债券投资' ) trace_4 = go.bar( x = [ '深证50' , '上证50' , '西南50' , '西北50' , '华中50' ], y = [ 0.0676 , 0.0087 , 0.0202 , 0.0609 , 0.0087 ], name = '银行存款' ) trace = [trace_1, trace_2, trace_3, trace_4] layout = go.layout( title = '基金资产配置比例图' , barmode = 'stack' ) fig = go.figure(data = trace, layout = layout) pyplt(fig, filename = 'tmp/1.html' ) |
瀑布式柱状图
瀑布式柱状图是层叠柱状图的另外一种表现
可以选择性地显示层叠部分来实现柱状图的悬浮效果。
|
# -*- coding: utf-8 -*- import plotly as py import plotly.graph_objs as go pyplt = py.offline.plot x_data = [ '资产1' , '资产2' , '资产3' , '资产4' , '总资产' ] y_data = [ 56000000 , 65000000 , 65000000 , 81000000 , 81000000 ] text = [ '666,999,888万元' , '8,899,666万元' , '88,899,666万元' , '16,167,657万元' , '888,888,888万元' ] # base trace0 = go.bar( x = x_data, y = [ 0 , 57999848 , 0 , 66899764 , 0 ], marker = dict ( color = 'rgba(1,1,1, 0.0)' , ) ) # trace trace1 = go.bar( x = x_data, y = [ 57999848 , 8899916 , 66899764 , 16167657 , 83067421 ], marker = dict ( color = 'rgba(55, 128, 191, 0.7)' , line = dict ( color = 'rgba(55, 128, 191, 1.0)' , width = 2 , ) ) ) data = [trace0, trace1] layout = go.layout( title = '测试图例' , barmode = 'stack' , showlegend = false ) annotations = [] for i in range ( 0 , 5 ): annotations.append( dict (x = x_data[i], y = y_data[i], text = text[i], font = dict (family = 'arial' , size = 14 , color = 'rgba(245, 246, 249, 1)' ), showarrow = false,)) layout[ 'annotations' ] = annotations fig = go.figure(data = data, layout = layout) pyplt(fig, filename = 'tmp/1.html' ) |
运行上述代码,可以得到如上图所示的瀑布式柱状图。
下面我们说说,图形样式的设置。
对于柱状图颜色与样式的设置可以通过设置下面这个案例来说明。
|
import plotly as py import plotly.graph_objs as go pyplt = py.offline.plot # customizing inliidual bar colors volume = [ 0.49 , 0.71 , 1.43 , 1.4 , 0.93 ] width = [each * 3 / sum (volume) for each in volume] trace0 = go.bar( x = [ 'au.shf' , 'ag.shf' , 'sn.shf' , 'pb.shf' , 'cu.shf' ], y = [ 0.85 , 0.13 , - 0.93 , 0.46 , 0.06 ], width = width, marker = dict ( color = [ 'rgb(205,38,38)' , 'rgb(205,38,38)' , 'rgb(34,139,34)' , 'rgb(205,38,38)' , 'rgb(205,38,38)' ], line = dict ( color = 'rgb(0,0,0)' , width = 1.5 , )), opacity = 0.8 , ) data = [trace0] layout = go.layout( title = '有色金属板块主力合约日内最高涨幅与波动率图' , xaxis = dict (tickangle = - 45 ), ) fig = go.figure(data = data, layout = layout) pyplt(fig, filename = 'tmp/4.html' ) |
运行上述代码,可以看到上图所示图例
柱状图展示了5种金属,在某个交易日的最高涨幅与波动率情况,柱形图宽度表示相对波动率的高低。
柱形图越宽,波动率越大,高度表示涨幅,红色表示上涨,绿色表示下跌。
用line设置柱状图外部线框,用width设置柱状图的宽度,用opacity设置柱状图颜色的透明度情况。
基本的柱状图情况,就讲到这里。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持开心学习网。
原文链接:https://blog.csdn.net/u012798683/article/details/88800743
- python 3.10 循环语法(Python3.4学习笔记之常用操作符,条件分支和循环用法示例)
- python调用支付宝支付接口(python实现支付宝转账接口)
- python实现删除列表重复元素功能(Python实现删除排序数组中重复项的两种方法示例)
- pythonmatplotlib怎么设置图像(完美解决Python matplotlib绘图时汉字显示不正常的问题)
- python如何用md5作为文档名(Python生成MD5值的两种方法实例分析)
- python 常用模块函数(Python OOP类中的几种函数或方法总结)
- python爬取数据总结(python3爬虫学习之数据存储txt的案例详解)
- python微信红包分配(PHP切割整数工具类似微信红包金额分配的思路详解)
- python同步钉钉用户(python 调用钉钉机器人的方法)
- python怎么自动生成报告(python根据文章标题内容自动生成摘要的实例)
- jupyter如何编写python(windows系统中Python多版本与jupyter notebook使用虚拟环境的过程)
- python统计出现文字最多的词(使用Python 统计高频字数的方法)
- python处理各种xml文件(Python使用sax模块解析XML文件示例)
- python迭代列表解析(Python可迭代对象操作示例)
- pythonlist类型教程(Python List cmp知识点总结)
- python自动处理图片(python制作图片缩略图)
- 26岁杨紫的白素贞遇上24岁鞠婧祎,哪个最美(26岁杨紫的白素贞遇上24岁鞠婧祎)
- 鞠婧祎和杨紫两种风格的女生,你心里谁比较美(鞠婧祎和杨紫两种风格的女生)
- 三千年第一美女鞠婧祎出演白素贞pk杨紫,颜值胜赵雅芝王祖贤(三千年第一美女鞠婧祎出演白素贞pk杨紫)
- 成都轨道交通13号线一期工程最新进展(成都轨道交通13号线一期工程最新进展)
- 越南没事(越南没事情做)
- 重庆旅游攻略(重庆旅游3-4天攻略最佳线路)
热门推荐
- javascript 数组排序
- js中的内存机制(详解JS内存空间)
- vmwaredeepin安装(vmware虚拟机安装deepin20最全详细过程)
- python字典键对应的值(Python 互换字典的键值对实例)
- windows终端远程连接linux服务器(linux通过跳板机连接远程服务器并进行文件传输的方法)
- sql server作业的调度信息中各列的含义
- sqlserver 查询锁(sqlserver:查询锁住sql以及解锁方法)
- sql server案例(SQL Server作业报错特殊案例分析)
- dede搜索模块下载(dede会员列表调用适用于企业、个人)
- 自动化播放声音(video实现有声音自动播放的实现方法)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9