Skip to content

Commit 98e08ea

Browse files
author
mahdis-z
committed
offsetgroup and alignmentgroup
1 parent 3e6f449 commit 98e08ea

File tree

3 files changed

+563
-0
lines changed

3 files changed

+563
-0
lines changed

doc/python/bar-charts.md

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,158 @@ fig.update_layout(
315315
fig.show()
316316
```
317317

318+
### Control Bar Position in Different Subplots
319+
320+
To control bars positional range among several subplots, set the same axes to the same [alignmentgroup](https://plot.ly/python/reference/#bar-alignmentgroup). In the following example we have two subplots sharing an x axis with two bar traces (trace0, trace1) on the top, and one bar trace (trace2) on the bottom, that all are aligned by setting the same `alignmentgroup`.
321+
You also can line up bars of the same positional coordinate by setting [offsetgroup](https://plot.ly/python/reference/#bar-offsetgroup).
322+
323+
```python
324+
import plotly.graph_objects as go
325+
326+
fig = go.Figure(go.Bar(
327+
alignmentgroup = "a",
328+
offsetgroup = 0,
329+
x = [1,2,3],
330+
y = [2,3,4],
331+
xaxis = 'x',
332+
yaxis = 'y'))
333+
334+
fig.add_trace(go.Bar(
335+
alignmentgroup = "a",
336+
offsetgroup = 1,
337+
x = [1,2,3],
338+
y = [2,3,4],
339+
xaxis = 'x',
340+
yaxis = 'y'))
341+
342+
fig.add_trace(go.Bar(
343+
alignmentgroup = "a",
344+
offsetgroup = 1,
345+
x = [1,2,3],
346+
y = [2,3,4],
347+
xaxis = 'x',
348+
yaxis = 'y2'))
349+
350+
351+
fig.update_layout(
352+
xaxis = {
353+
'anchor': 'y'},
354+
yaxis2 = {
355+
'domain': [.55,1],
356+
'anchor': 'x'},
357+
yaxis = {
358+
'domain': [0,.45],
359+
'anchor': 'x'})
360+
361+
fig.show()
362+
```
363+
364+
### Offsetgroup vs. Alignmentgroup
365+
366+
```python
367+
import plotly.graph_objects as go
368+
369+
fig = go.Figure(go.Bar(
370+
alignmentgroup = "a",
371+
offsetgroup = 0,
372+
x = [1,2,3],
373+
y = [2,3,4],
374+
xaxis = 'x',
375+
yaxis = 'y'))
376+
377+
fig.add_trace(go.Bar(
378+
alignmentgroup = "a",
379+
offsetgroup = 1,
380+
x = [1,2,3],
381+
y = [2,3,4],
382+
xaxis = 'x',
383+
yaxis = 'y'))
384+
385+
fig.add_trace(go.Bar(
386+
alignmentgroup = "a",
387+
offsetgroup = 2,
388+
x = [1,2,3],
389+
y = [2,3,4],
390+
xaxis = 'x',
391+
yaxis = 'y2'))
392+
393+
fig.add_trace(go.Bar(
394+
alignmentgroup = "a",
395+
offsetgroup = 0,
396+
x = [1,2,3],
397+
y = [2,3,4],
398+
xaxis = 'x2',
399+
yaxis = 'y3'))
400+
401+
fig.add_trace(go.Bar(
402+
alignmentgroup = "b",
403+
offsetgroup = 1,
404+
x = [1,2,3],
405+
y = [2,3,4],
406+
xaxis = 'x2',
407+
yaxis = 'y4'))
408+
409+
fig.add_trace(go.Bar(
410+
alignmentgroup = "a",
411+
offsetgroup = 1,
412+
x = [1,2,3],
413+
y = [2,3,4],
414+
xaxis = 'x2',
415+
yaxis = 'y3'))
416+
417+
fig.add_trace(go.Bar(
418+
alignmentgroup = "a",
419+
offsetgroup = 0,
420+
x = [1,2,3],
421+
y = [2,3,4],
422+
xaxis = 'x3',
423+
yaxis = 'y5'))
424+
425+
fig.add_trace(go.Bar(
426+
alignmentgroup = "a",
427+
offsetgroup = 1,
428+
x = [1,2,3],
429+
y = [2,3,4],
430+
xaxis = 'x3',
431+
yaxis = 'y6'))
432+
433+
fig.add_trace(go.Bar(
434+
alignmentgroup = "a",
435+
offsetgroup = 1,
436+
x = [1,2,3],
437+
y = [2,3,4],
438+
xaxis = 'x3',
439+
yaxis = 'y5'))
440+
441+
fig.update_layout(
442+
xaxis = {
443+
'domain': [0, .35],
444+
'anchor': 'y',
445+
'title': "=alignment<br>≠offset"},
446+
xaxis2 = {
447+
'domain': [.42, .65],
448+
'title': "≠alignment<br>=offset",
449+
'anchor': 'y'
450+
},
451+
xaxis3 = {
452+
'domain': [.72, 1],
453+
'title': "=alignment<br>=offset",
454+
'anchor': 'y'
455+
},
456+
yaxis2 = {
457+
'domain': [.55,1],
458+
'anchor': 'x'},
459+
yaxis = {
460+
'domain': [0,.45],
461+
'anchor': 'x'},
462+
yaxis3 = {'domain': [.55,1], 'anchor': 'x2'},
463+
yaxis4 = {'domain': [0, .5], 'anchor': 'x2'},
464+
yaxis5 = {'domain': [.55, 1], 'anchor': 'x3'},
465+
yaxis6 = {'domain': [0, .5], 'anchor': 'x3'})
466+
467+
fig.show()
468+
```
469+
318470
### Bar Chart with Relative Barmode
319471

320472
With "relative" barmode, the bars are stacked on top of one another, with negative values

0 commit comments

Comments
 (0)