Okroshiashvili
Okroshiashvili

Reputation: 4159

Multiple Bar charts on the same X-axis and Y-axis in Plotly

I'm trying to plot multiple bar charts on the same x-axis and y-axis. I do not want stacked or grouped bar charts. I just want to have multiple bar charts side by side with a long x-axis where x-axis ticks repeat based on the number of bar charts and y-axis which is shared along bar charts and is placed on the leftmost side of the graph.

Here is my code but it does not produce the output what I want

import plotly.offline as pyo
import plotly.graph_objs as go
from plotly import tools



trace1 = go.Bar(
    x=[1, 2, 3],
    y=[10, 11, 12]
)
trace2 = go.Bar(
    x=[1, 2, 3],
    y=[100, 110, 120],
)
trace3 = go.Bar(
    x=[1, 2, 3],
    y=[1000, 1100, 1200],
)


fig = tools.make_subplots(rows=3, cols=1, specs=[[{}], [{}], [{}]],
                          shared_xaxes=True, shared_yaxes=True,
                          vertical_spacing=0.001)


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

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

pyo.plot(fig, filename='bar-charts-with-shared-axis.html')

Any help would be appreciated

Upvotes: 3

Views: 2428

Answers (1)

Dmitriy Kisil
Dmitriy Kisil

Reputation: 2998

If I correctly understand, you want that: Your output

Code:

import plotly.offline as pyo
import plotly.graph_objs as go
from plotly import tools

trace1 = go.Bar(
    x=[1, 2, 3],
    y=[10, 11, 12]
)
trace2 = go.Bar(
    x=[1, 2, 3],
    y=[100, 110, 120],
)
trace3 = go.Bar(
    x=[1, 2, 3],
    y=[1000, 1100, 1200],
)

fig = tools.make_subplots(rows=1, cols=3,
                          shared_xaxes=True, shared_yaxes=True,
                          vertical_spacing=0.001)

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

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

pyo.plot(fig, filename='bar-charts-with-shared-axis.html')

Upvotes: 1

Related Questions