在绘图中将组条形图添加为子图 [英] Adding group bar charts as subplots in plotly

查看:120
本文介绍了在绘图中将组条形图添加为子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以绘图方式创建分组的( barmode ='group')条形图子图.现在的问题是,plotly不会创建条形图作为迹线.而是将分组的条形图创建为条形轨迹列表.因此,我不知道如何创建包含分组条形图作为子图的图形(即,使用 figure.append_trace()添加分组条形图).

I want to create grouped (barmode='group') bar chart subplots in plotly. Now the problem is that plotly doesn't create bar charts as traces. Instead grouped bar charts are created as lists of Bar traces. Because of this, I don't know how to create a figure that contains grouped bar charts as subplots (i.e. add a grouped bar chart using figure.append_trace()).

例如,如何使用在此示例中创建的条形图创建子图:

import plotly.plotly as py
import plotly.graph_objs as go
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='group'
)
fig = go.Figure(data=data, layout=layout)
plot_url = py.plot(fig, filename='grouped-bar')

推荐答案

是的! plot.ly的新手并有此问题,正如我的评论中所述,由于各种原因,我不能只在pandas/matplotlib中执行此操作.但是,通过子图的魔力,您实际上可以通过将它们一起子图化来重新创建多迹线图.

YES! New to plot.ly and had this issue, and as mentioned in my comment, I couldn't just do it in pandas/matplotlib for various reasons. But through the magic of subplots, you can in fact recreate multi-trace plots by just subploting them together.

import plotly.plotly as py
import plotly.graph_objs as go
from plotly import tools

trace1 = Bar(
    x=['giraffes', 'orangutans', 'monkeys'],
    y=[20, 14, 23],
    name='SF Zoo'
)
trace2 = Bar(
    x=['giraffes', 'orangutans', 'monkeys'],
    y=[12, 18, 29],
    name='LA Zoo'
)
trace3 = Scatter(
  x=['giraffes', 'orangutans', 'monkeys']
  ,y=[33,20,17]
  ,name='subplots ftw'
  )


fig = tools.make_subplots(rows=2, cols=1, shared_xaxes=True)

fig.append_trace(trace3, 1,1)
fig.append_trace(trace1, 2, 1)
fig.append_trace(trace2,2,1)


fig['layout'].update(height=600, width=600)
iplot(fig)

这篇关于在绘图中将组条形图添加为子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆