Skip to content

Commit 795009a

Browse files
authored
Merge pull request #1951 from plotly/bingroup2
bingroup
2 parents 5a05be1 + 5aafb5d commit 795009a

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

doc/python/2D-Histogram.md

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: '1.1'
9-
jupytext_version: 1.1.1
8+
format_version: '1.2'
9+
jupytext_version: 1.3.0
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.6.7
23+
version: 3.7.5
2424
plotly:
2525
description: How to make 2D Histograms in Python with Plotly.
2626
display_as: statistical
@@ -70,6 +70,38 @@ fig = go.Figure(go.Histogram2d(x=x, y=y, histnorm='probability',
7070
))
7171
fig.show()
7272
```
73+
### Sharing bin settings between 2D Histograms
74+
This example shows how to use [bingroup](https://plot.ly/python/reference/#histogram-bingroup) attribute to have a compatible bin settings for both histograms. To define `start`, `end` and `size` value of x-axis and y-axis seperatly, set [ybins](https://plot.ly/python/reference/#histogram2dcontour-ybins) and `xbins`.
75+
76+
```python
77+
import plotly.graph_objects as go
78+
from plotly.subplots import make_subplots
79+
80+
fig = make_subplots(2,2)
81+
fig.add_trace(go.Histogram2d(
82+
x = [ 1, 2, 2, 3, 4 ],
83+
y = [ 1, 2, 2, 3, 4 ],
84+
coloraxis = "coloraxis",
85+
xbins = {'start':1, 'size':1}), 1,1)
86+
fig.add_trace(go.Histogram2d(
87+
x = [ 4, 5, 5, 5, 6 ],
88+
y = [ 4, 5, 5, 5, 6 ],
89+
coloraxis = "coloraxis",
90+
ybins = {'start': 3, 'size': 1}),1,2)
91+
fig.add_trace(go.Histogram2d(
92+
x = [ 1, 2, 2, 3, 4 ],
93+
y = [ 1, 2, 2, 3, 4 ],
94+
bingroup = 1,
95+
coloraxis = "coloraxis",
96+
xbins = {'start':1, 'size':1}), 2,1)
97+
fig.add_trace(go.Histogram2d(
98+
x = [ 4, 5, 5, 5, 6 ],
99+
y = [ 4, 5, 5, 5, 6 ],
100+
bingroup = 1,
101+
coloraxis = "coloraxis",
102+
ybins = {'start': 3, 'size': 1}),2,2)
103+
fig.show()
104+
```
73105

74106
### 2D Histogram Overlaid with a Scatter Chart ###
75107

@@ -139,4 +171,3 @@ fig.show()
139171

140172
#### Reference
141173
See https://plot.ly/python/reference/#histogram2d for more information and chart attribute options!
142-

doc/python/histograms.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,28 @@ fig1.show()
350350
fig2.show()
351351
```
352352

353+
### Share bins between histograms
354+
In this example both histograms have a compatible bin settings using [bingroup](https://plot.ly/python/reference/#histogram-bingroup) attribute. Note that traces on the same subplot, and with the same `barmode` ("stack", "relative", "group") are forced into the same `bingroup`, however traces with `barmode = "overlay"` and on different axes (of the same axis type) can have compatible bin settings. Histogram and [histogram2d](https://plot.ly/python/2D-Histogram/) trace can share the same `bingroup`.
355+
356+
```python
357+
import plotly.graph_objects as go
358+
import numpy as np
359+
360+
fig = go.Figure(go.Histogram(
361+
x=np.random.randint(7, size=100),
362+
bingroup=1))
363+
364+
fig.add_trace(go.Histogram(
365+
x=np.random.randint(7, size=20),
366+
bingroup=1))
367+
368+
fig.update_layout(
369+
barmode="overlay",
370+
bargap=0.1)
371+
372+
fig.show()
373+
```
374+
353375
### Dash Example
354376

355377

0 commit comments

Comments
 (0)