Kar*_*awy 9 python charts plotly
我想在plotly中创建分组(barmode ='group')条形图子图.现在的问题是,图形不会创建条形图作为跟踪.而是将分组条形图创建为条形轨迹列表.因此,我不知道如何创建包含分组条形图作为子图的图形(即使用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')
Run Code Online (Sandbox Code Playgroud)
Sco*_*ieB 10
是的!plot.ly 的新手并且遇到了这个问题,正如我在评论中提到的,由于各种原因,我不能只在 pandas/matplotlib 中这样做。但是通过子图的魔力,您实际上可以通过将它们一起子图来重新创建多轨迹图。

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)
Run Code Online (Sandbox Code Playgroud)
我从未使用过plotly 包,但使用matplotlib 似乎你所追求的相当简单。这是一个将分组条形图显示为子图的最小示例。如果这不完全是您所要求的,请告诉我。
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()
Run Code Online (Sandbox Code Playgroud)