本文实例为大家分享了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')
运行上述代码,可以得到如上图所示的层叠水平条形图。
水平条形图和柱状图的画法基本上相同。剩下的就不细讲了。
本文python使用Plotly绘图工具绘制水平条形图到此结束。不问收获,但问耕耘!天道酬勤。小编再次感谢大家对我们的支持!