https://plot.ly/python/bar-charts/#bar-chart-with-line-plot
我想使用plotly和iPython创建一个带有线条图的条形图,如上例所示。另一方面,我希望条形图是一个水平堆积条形图,如下例中使用plotly和iPython。我该怎么做呢?
https://plot.ly/python/bar-charts/#colored-bar-chart
y_saving_yes = [1, 2, 4, 6, 7, 7]
y_saving_no = [10, 10, 10, 10, 10, 10]
y_net_worth = [93453, 81666, 69889, 78381, 141395, 92969]
x_saving = ['Premium', 'Spot Shadow', 'Slow Motion', 'Highlight Music','Extra Text', 'Top Play']
x_net_worth = ['Premium', 'Spot Shadow', 'Slow Motion', 'Highlight Music','Extra Text', 'Top Play']
trace1 = Bar(
x=y_saving,
y=x_saving,
marker=Marker(
color='rgba(50, 171, 96, 0.6)',
line=Line(
color='rgba(50, 171, 96, 1.0)',
width=1,
),
),
name='Highlight Properties',
orientation='h',
)
trace2 = Bar(
x=y_saving,
y=x_saving,
marker=Marker(
color='rgba(50, 171, 96, 0.6)',
line=Line(
color='rgba(50, 171, 96, 1.0)',
width=1,
),
),
name='Highlight Properties',
orientation='h',
)
data = Data([trace1, trace2])
layout = Layout(barmode='stack')
fig1 = Figure(data=data, layout=layout)
trace3 = Scatter(
x=y_net_worth,
y=x_net_worth,
mode='lines+markers',
line=Line(
color='rgb(128, 0, 128)',
),
name='Highlight Views',
)
fig = tools.make_subplots(rows=1, cols=2, specs=[[{}, {}]], shared_xaxes=True,
shared_yaxes=False, vertical_spacing=0.001)
fig.append_trace(trace1, 1, 1)
fig.append_trace(trace3, 1, 2)
fig['layout'].update(layout)
py.iplot(fig, filename='oecd-networth-saving-bar-line')
答案 0 :(得分:5)
安德鲁来自Plotly。超级亲密!我想你错过了fig.append_trace(trace2, 1, 1)
。这是一个简单的例子,基本上做同样的事情以供参考。
import plotly.plotly as py
from plotly import tools
from plotly.graph_objs import Bar, Data, Figure, Layout, Marker, Scatter
x_0 = [1, 2, 4, 6, 7, 7]
x_1 = [10, 10, 10, 10, 10, 10]
y_0 = [2, 3, 4, 2, 3, 3]
trace1 = Bar(
x=x_0,
marker=Marker(color='#001f3f'),
orientation='h',
)
trace2 = Bar(
x=x_1,
marker=Marker(color='#0074D9'),
orientation='h',
)
trace3 = Scatter(y=y_0)
fig = tools.make_subplots(1, 2)
fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 1, 1)
fig.append_trace(trace3, 1, 2)
fig['layout'].update(barmode='stack')
py.iplot(fig, filename='oecd-networth-saving-bar-line')