Skip to content

Commit 3de41cb

Browse files
author
mahdis-z
committed
offsetgroup and alignmentgroup
1 parent 136f1d4 commit 3de41cb

File tree

3 files changed

+416
-0
lines changed

3 files changed

+416
-0
lines changed

python/bar-charts.md

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,158 @@ fig.update_layout(
304304
fig.show()
305305
```
306306

307+
### Control Bar Position in Different Subplots
308+
309+
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`.
310+
You also can line up bars of the same positional coordinate by setting [offsetgroup](https://plot.ly/python/reference/#bar-offsetgroup).
311+
312+
```python
313+
import plotly.graph_objects as go
314+
315+
fig = go.Figure(go.Bar(
316+
alignmentgroup = "a",
317+
offsetgroup = 0,
318+
x = [1,2,3],
319+
y = [2,3,4],
320+
xaxis = 'x',
321+
yaxis = 'y2'))
322+
323+
fig.add_trace(go.Bar(
324+
alignmentgroup = "a",
325+
offsetgroup = 1,
326+
x = [1,2,3],
327+
y = [2,3,4],
328+
xaxis = 'x',
329+
yaxis = 'y2'))
330+
331+
fig.add_trace(go.Bar(
332+
alignmentgroup = "a",
333+
offsetgroup = 1,
334+
x = [1,2,3],
335+
y = [2,3,4],
336+
xaxis = 'x',
337+
yaxis = 'y'))
338+
339+
340+
fig.update_layout(
341+
xaxis = {
342+
'anchor': 'y'},
343+
yaxis2 = {
344+
'domain': [.55,1],
345+
'anchor': 'x'},
346+
yaxis = {
347+
'domain': [0,.45],
348+
'anchor': 'x'})
349+
350+
fig.show()
351+
```
352+
Let's compare the impact of `offsetgroup` vs. `alignmentgroup`.
353+
354+
```python
355+
356+
import plotly.graph_objects as go
357+
358+
fig = go.Figure(go.Bar(
359+
alignmentgroup = "a",
360+
offsetgroup = 0,
361+
x = [1,2,3],
362+
y = [2,3,4],
363+
xaxis = 'x',
364+
yaxis = 'y2'))
365+
366+
fig.add_trace(go.Bar(
367+
alignmentgroup = "a",
368+
offsetgroup = 1,
369+
x = [1,2,3],
370+
y = [2,3,4],
371+
xaxis = 'x',
372+
yaxis = 'y2'))
373+
374+
fig.add_trace(go.Bar(
375+
alignmentgroup = "a",
376+
offsetgroup = 2,
377+
x = [1,2,3],
378+
y = [2,3,4],
379+
xaxis = 'x',
380+
yaxis = 'y'))
381+
382+
fig.add_trace(go.Bar(
383+
alignmentgroup = "a",
384+
offsetgroup = 0,
385+
x = [1,2,3],
386+
y = [2,3,4],
387+
xaxis = 'x2',
388+
yaxis = 'y3'))
389+
390+
fig.add_trace(go.Bar(
391+
alignmentgroup = "b",
392+
offsetgroup = 1,
393+
x = [1,2,3],
394+
y = [2,3,4],
395+
xaxis = 'x2',
396+
yaxis = 'y4'))
397+
398+
fig.add_trace(go.Bar(
399+
alignmentgroup = "a",
400+
offsetgroup = 1,
401+
x = [1,2,3],
402+
y = [2,3,4],
403+
xaxis = 'x2',
404+
yaxis = 'y3'))
405+
406+
fig.add_trace(go.Bar(
407+
alignmentgroup = "a",
408+
offsetgroup = 0,
409+
x = [1,2,3],
410+
y = [2,3,4],
411+
xaxis = 'x3',
412+
yaxis = 'y5'))
413+
414+
fig.add_trace(go.Bar(
415+
alignmentgroup = "a",
416+
offsetgroup = 1,
417+
x = [1,2,3],
418+
y = [2,3,4],
419+
xaxis = 'x3',
420+
yaxis = 'y6'))
421+
422+
fig.add_trace(go.Bar(
423+
alignmentgroup = "a",
424+
offsetgroup = 1,
425+
x = [1,2,3],
426+
y = [2,3,4],
427+
xaxis = 'x3',
428+
yaxis = 'y5'))
429+
430+
fig.update_layout(
431+
xaxis = {
432+
'domain': [0, .35],
433+
'anchor': 'y',
434+
'title': "=alignment<br>≠offset"},
435+
xaxis2 = {
436+
'domain': [.42, .65],
437+
'title': "≠alignment<br>=offset",
438+
'anchor': 'y'
439+
},
440+
xaxis3 = {
441+
'domain': [.72, 1],
442+
'title': "=alignment<br>=offset",
443+
'anchor': 'y'
444+
},
445+
yaxis2 = {
446+
'domain': [.55,1],
447+
'anchor': 'x'},
448+
yaxis = {
449+
'domain': [0,.45],
450+
'anchor': 'x'},
451+
yaxis3 = {'domain': [.55,1], 'anchor': 'x2'},
452+
yaxis4 = {'domain': [0, .5], 'anchor': 'x2'},
453+
yaxis5 = {'domain': [.55, 1], 'anchor': 'x3'},
454+
yaxis6 = {'domain': [0, .5], 'anchor': 'x3'})
455+
456+
fig.show()
457+
```
458+
307459
### Bar Chart with Relative Barmode
308460

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

python/box-plots.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,147 @@ fig.update_layout(
398398
showlegend=False
399399
)
400400

401+
fig.show()
402+
```
403+
### Control Box Position in Different Subplots
404+
405+
To control box plot positional range among several subplots, set the same axes to the same [alignmentgroup](https://plot.ly/python/reference/#box-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`.
406+
You also can line up bars of the same positional coordinate by setting [offsetgroup](https://plot.ly/python/reference/#box-offsetgroup).
407+
408+
```python
409+
import plotly.graph_objects as go
410+
411+
fig = go.Figure(go.Box(
412+
alignmentgroup = "a",
413+
offsetgroup = 0,
414+
y = [2,6,10,2,7,1,5],
415+
x = [1,1,1,1,1,1,1],
416+
xaxis = 'x',
417+
yaxis = 'y2'))
418+
419+
fig.add_trace(go.Box(
420+
y = [2,6,10,2,7,1,5],
421+
x = [1,1,1,1,1,1,1],
422+
alignmentgroup = "a",
423+
offsetgroup = 1,
424+
xaxis = 'x',
425+
yaxis = 'y2'))
426+
427+
fig.add_trace(go.Box(
428+
alignmentgroup = "a",
429+
offsetgroup = 0,
430+
y = [2,6,10,2,7,1,5],
431+
x = [1,1,1,1,1,1,1],
432+
xaxis = 'x',
433+
yaxis = 'y'))
434+
435+
fig.update_layout(
436+
yaxis2 = {'domain': [.55,1]},
437+
yaxis = {'domain': [0,.45]},
438+
boxmode = 'group')
439+
440+
fig.show()
441+
```
442+
Let's compare the impact of `offsetgroup` vs. `alignmentgroup`.
443+
444+
```python
445+
446+
import plotly.graph_objects as go
447+
448+
fig = go.Figure(go.Bar(
449+
alignmentgroup = "a",
450+
offsetgroup = 0,
451+
x = [1,2,3],
452+
y = [2,3,4],
453+
xaxis = 'x',
454+
yaxis = 'y2'))
455+
456+
fig.add_trace(go.Bar(
457+
alignmentgroup = "a",
458+
offsetgroup = 1,
459+
x = [1,2,3],
460+
y = [2,3,4],
461+
xaxis = 'x',
462+
yaxis = 'y2'))
463+
464+
fig.add_trace(go.Bar(
465+
alignmentgroup = "a",
466+
offsetgroup = 2,
467+
x = [1,2,3],
468+
y = [2,3,4],
469+
xaxis = 'x',
470+
yaxis = 'y'))
471+
472+
fig.add_trace(go.Bar(
473+
alignmentgroup = "a",
474+
offsetgroup = 0,
475+
x = [1,2,3],
476+
y = [2,3,4],
477+
xaxis = 'x2',
478+
yaxis = 'y2'))
479+
480+
fig.add_trace(go.Bar(
481+
alignmentgroup = "b",
482+
offsetgroup = 1,
483+
x = [1,2,3],
484+
y = [2,3,4],
485+
xaxis = 'x2',
486+
yaxis = 'y'))
487+
488+
fig.add_trace(go.Bar(
489+
alignmentgroup = "a",
490+
offsetgroup = 1,
491+
x = [1,2,3],
492+
y = [2,3,4],
493+
xaxis = 'x2',
494+
yaxis = 'y2'))
495+
496+
fig.add_trace(go.Bar(
497+
alignmentgroup = "a",
498+
offsetgroup = 0,
499+
x = [1,2,3],
500+
y = [2,3,4],
501+
xaxis = 'x3',
502+
yaxis = 'y2'))
503+
504+
fig.add_trace(go.Bar(
505+
alignmentgroup = "a",
506+
offsetgroup = 1,
507+
x = [1,2,3],
508+
y = [2,3,4],
509+
xaxis = 'x3',
510+
yaxis = 'y'))
511+
512+
fig.add_trace(go.Bar(
513+
alignmentgroup = "a",
514+
offsetgroup = 1,
515+
x = [1,2,3],
516+
y = [2,3,4],
517+
xaxis = 'x3',
518+
yaxis = 'y2'))
519+
520+
fig.update_layout(
521+
xaxis = {
522+
'domain': [0, .35],
523+
'anchor': 'y',
524+
'title': "=alignment<br>≠offset"},
525+
xaxis2 = {
526+
'domain': [.42, .65],
527+
'title': "≠alignment<br>=offset",
528+
'anchor': 'y'
529+
},
530+
xaxis3 = {
531+
'domain': [.72, 1],
532+
'title': "=alignment<br>=offset",
533+
'anchor': 'y'
534+
},
535+
yaxis2 = {
536+
'domain': [.55,1],
537+
'anchor': 'x'},
538+
yaxis = {
539+
'domain': [0,.45],
540+
'anchor': 'x'})
541+
401542
fig.show()
402543
```
403544

0 commit comments

Comments
 (0)