How to specify a variable number of xaxis on layout

I need help in determining how to handle a variable number of xaxis values when defining the layout.

I am doing a chart which has grouped stacked bar charts, and am using the technique of putting each group on its own xaxis. Works great, except for the fact that I have a variable number of groups to graph, and each xaxis has a label xaxis, xaxis2, xaxis3, etc.

Here is a snippet of code:

layout = go.Layout(barmode=‘stack’,
xaxis=dict(domain=[0, 0.11]
xaxis2=dict(domain=[0.125, 0.235]
xaxis3=dict(domain=[0.250, 0.365]

I will have between 1-10 xaxis depending on my data. So how do I construct code that works by dynamically constructing the parameters to go.Layout? Or is there another way to get the layout to have the xaxis?

Thanks in advance from a person new to to Plotly.

Probably best for #api:python

Hi @Doug

Could you re-post your code snippet using markdown code formatting? For example

layout = go.Layout(…)

This keeps the formatting from getting messed up so that people can copy and paste your example to run it. Also, can you add all of the necessary imports so the code snippet is self-contained.


I had the same question. I wanted to specify a variable number of Y-axes via the Python API, depending on the data set.

And with a little experimentation, I realized you can pass a dictionary in place of the named parameters:

# box_axis_list is a list of individual axis specifications;
#   each a dictionary specifying title, visibility, etc.

axis_dict = {}
for inx, box_axis in enumerate(box_axis_list):
    key = "yaxis" if inx == 0 else ("yaxis%d" % (inx+1))
    axis_dict[key] = box_axis
layout_dict = copy.deepcopy(axis_dict)
layout_dict['title'] = "Title of my chart"
# add other paramerters as desired
layout = go.Layout(