Black Lives Matter. Please consider donating to Black Girls Code today.

Formatting a date axis

If I have 15 subplots on a chart id there an easy way to format all of them the same way rather than doing this:
fig[‘layout’].update(xaxis1= {‘tickformat’: ‘%m/%y’})
fig[‘layout’].update(xaxis2= {‘tickformat’: ‘%m/%y’})
fig[‘layout’].update(xaxis3= {‘tickformat’: ‘%m/%y’})
fig[‘layout’].update(xaxis4= {‘tickformat’: ‘%m/%y’})
fig[‘layout’].update(xaxis5= {‘tickformat’: ‘%m/%y’})
fig[‘layout’].update(xaxis6= {‘tickformat’: ‘%m/%y’})
fig[‘layout’].update(xaxis7= {‘tickformat’: ‘%m/%y’})
fig[‘layout’].update(xaxis8= {‘tickformat’: ‘%m/%y’})
fig[‘layout’].update(xaxis9= {‘tickformat’: ‘%m/%y’})
fig[‘layout’].update(xaxis10= {‘tickformat’: ‘%m/%y’})
fig[‘layout’].update(xaxis11= {‘tickformat’: ‘%m/%y’})
fig[‘layout’].update(xaxis12= {‘tickformat’: ‘%m/%y’})
fig[‘layout’].update(xaxis13= {‘tickformat’: ‘%m/%y’})
fig[‘layout’].update(xaxis14= {‘tickformat’: ‘%m/%y’})
fig[‘layout’].update(xaxis15= {‘tickformat’: ‘%m/%y’})

I also want to be able to set the range for all axes in one line of code if that is possible. Thanks!

These objects are just dictionaries, so you can modify them using idiomatic python like:

for key in fig['layout']:
   if 'axis' in key:
       fig['layout'][key]['tickformat'] = '/%m/%y'
1 Like

Thanks Chris. Is there an easy way to see what attributes I can modify for the figure object? Do I just go to the plotly documentation?

Yup - https://plot.ly/python has examples and https://plot.ly/python/reference has all of the available attributes. You can also use the interactive editor in https://plot.ly/create (see the JSON panel)