python画折线图
python使用Plotly绘图工具绘制水平条形图本文实例为大家分享了python绘制水平条形图的具体代码,供大家参考,具体内容如下
水平条形图与绘制柱状图类似,大家可以先看看我之前写的博客,如何绘制柱状图
水平条形图需要在bar函数中设置orientation= 'h'
其他的参数与柱状图相同。也可以通过设置barmode = 'stack',
绘制层叠水平条形图和瀑布式水平条形图
|
import plotly as py import plotly.graph_objs as go pyplt = py.offline.plot data = [go.bar( x = [ 29.41 , 34.62 , 30.16 ], y = [ '资产1' , '资产2' , '资产3' ], orientation = 'h' )] layout = go.layout( title = '净资产收益率对比' ) figure = go.figure(data = data, layout = layout) pyplt(figure, filename = 'tmp/1.html' ) |
运行上述代码,得到如上图所示的图例,可以看到其画法跟柱状图一样,只是变成水平方向。
如何画水平的层叠条形图,只需要我们将参数,barmode = 'stack',即可画出响应的水平图
|
import plotly as py import plotly.graph_objs as go pyplt = py.offline.plot trace1 = go.bar( y = [ 'cu.shf' , 'ag.shf' , 'au.shf' ], x = [ 21258 , 30279 , 8056 ], name = '期货1' , orientation = 'h' , marker = dict ( color = '#104e8b' , line = dict ( color = '#104e8b' , width = 3 ) ) ) trace2 = go.bar( y = [ 'cu.shf' , 'ag.shf' , 'au.shf' ], x = [ 19853 , 9375 , 4063 ], name = '期货2' , orientation = 'h' , marker = dict ( color = '#1874cd' , line = dict ( color = '#104e8b' , width = 3 ) ) ) trace3 = go.bar( y = [ 'cu.shf' , 'ag.shf' , 'au.shf' ], x = [ 4959 , 13018 , 8731 ], name = '期货3' , orientation = 'h' , marker = dict ( color = '#1c86ee' , line = dict ( color = '#104e8b' , width = 3 ) ) ) data = [trace1, trace2,trace3] layout = go.layout( title = '稀有金属期货持仓量对比图' , barmode = 'stack' ) fig = go.figure(data = data, layout = layout) pyplt(fig, filename = 'tmp/2.html' ) |
运行上述代码,可以得到如上图所示的层叠水平条形图。
水平条形图和柱状图的画法基本上相同。剩下的就不细讲了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持开心学习网。
原文链接:https://blog.csdn.net/u012798683/article/details/88814486