简体   繁体   English

在绘图中将组条形图添加为子图

[英]Adding group bar charts as subplots in plotly

I want to create grouped ( barmode='group' ) bar chart subplots in plotly. 我想在绘图中创建分组的( barmode ='group' )条形图子图。 Now the problem is that plotly doesn't create bar charts as traces. 现在的问题是,plotly不会创建条形图作为迹线。 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 (ie add a grouped bar chart using figure.append_trace() ). 因此,我不知道如何创建包含分组条形图作为子图的图形(即,使用fig.append_trace()添加分组条形图)。

For example, how can I create subplots using bar charts created in this sample : 例如,如何使用此示例中创建的条形图创建子图:

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')

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. plot.ly的新手并有此问题,正如我的评论中所述,由于各种原因,我不能仅在pandas / matplotlib中执行此操作。 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)

I've never used the plotly package but what it seems you are after is fairly straightforward using matplotlib. 我从未使用过plotly软件包,但是使用matplotlib看起来很简单。 Here is a pretty minimal example of showing grouped bar charts as a subplot. 这是将分组条形图显示为子图的一个非常简单的示例。 Let me know if this is not quite what you were asking for. 让我知道这是否不是您要的。

import numpy as np
import matplotlib.pyplot as plt

# First subplot
plt.subplot(2, 1, 1)

x = np.linspace(0, 10)
y = np.sin(np.pi * x)

plt.plot(x, y)

# Second subplot
plt.subplot(2, 1, 2)
titles = ('Canada', 'US', 'England', 'Other')
y_pos = np.arange(len(titles))
width = 0.2
bar_height1 = [6,5,7,2]
bar_height2 = [x+1 for x in bar_height1]

plt.bar(y_pos, bar_height1, width, align='center', alpha=0.8, color='r')
plt.bar(y_pos+width, bar_height2, width, align='center', alpha=0.8, color='b')

plt.xticks(y_pos + width/2, titles)

# Show the plots
plt.show()

Matplotlib图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM