diff --git a/python/2D-Histogram.md b/python/2D-Histogram.md index 89059d50a..471719061 100644 --- a/python/2D-Histogram.md +++ b/python/2D-Histogram.md @@ -6,7 +6,7 @@ jupyter: extension: .md format_name: markdown format_version: '1.1' - jupytext_version: 1.1.1 + jupytext_version: 1.2.1 kernelspec: display_name: Python 3 language: python @@ -20,7 +20,7 @@ jupyter: name: python nbconvert_exporter: python pygments_lexer: ipython3 - version: 3.6.7 + version: 3.7.3 plotly: description: How to make 2D Histograms in Python with Plotly. display_as: statistical @@ -71,6 +71,36 @@ fig = go.Figure(go.Histogram2d(x=x, y=y, histnorm='probability', fig.show() ``` +### Sharing bin settings between 2D Histograms +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`. + +```python +import plotly.graph_objects as go +from plotly.subplots import make_subplots +fig = make_subplots(2,2) +fig.add_trace(go.Histogram2d( + x = [ 1, 2, 2, 3, 4 ], + y = [ 1, 2, 2, 3, 4 ], + #bingroup = 1, + xbins = {'start':1, 'size':1}), 1,1) +fig.add_trace(go.Histogram2d( + x = [ 4, 5, 5, 5, 6 ], + y = [ 4, 5, 5, 5, 6 ], + #bingroup = 1, + ybins = {'start': 3, 'size': 1}),1,2) +fig.add_trace(go.Histogram2d( + x = [ 1, 2, 2, 3, 4 ], + y = [ 1, 2, 2, 3, 4 ], + bingroup = 1, + xbins = {'start':1, 'size':1}), 2,1) +fig.add_trace(go.Histogram2d( + x = [ 4, 5, 5, 5, 6 ], + y = [ 4, 5, 5, 5, 6 ], + bingroup = 1, + ybins = {'start': 3, 'size': 1}),2,2) +fig.show() +``` + ### 2D Histogram Overlaid with a Scatter Chart ### ```python diff --git a/python/histograms.md b/python/histograms.md index 6ff910166..a2bdb6124 100644 --- a/python/histograms.md +++ b/python/histograms.md @@ -6,7 +6,7 @@ jupyter: extension: .md format_name: markdown format_version: '1.1' - jupytext_version: 1.1.1 + jupytext_version: 1.2.1 kernelspec: display_name: Python 3 language: python @@ -20,7 +20,7 @@ jupyter: name: python nbconvert_exporter: python pygments_lexer: ipython3 - version: 3.6.7 + version: 3.7.3 plotly: description: How to make Histograms in Python with Plotly. display_as: statistical @@ -297,29 +297,25 @@ x = ['1970-01-01', '1970-01-01', '1970-02-01', '1970-04-01', '1970-01-02', fig = make_subplots(rows=3, cols=2) trace0 = go.Histogram(x=x, nbinsx=4) -trace1 = go.Histogram(x=x, nbinsx = 8) +trace1 = go.Histogram(x=x, nbinsx=8) trace2 = go.Histogram(x=x, nbinsx=10) trace3 = go.Histogram(x=x, xbins=dict( start='1969-11-15', end='1972-03-31', - size='M18'), # M18 stands for 18 months - autobinx=False - ) + size='M18')) # M18 stands for 18 months + trace4 = go.Histogram(x=x, xbins=dict( start='1969-11-15', end='1972-03-31', - size='M4'), # 4 months bin size - autobinx=False - ) + size='M4')) # M4 months bin size + trace5 = go.Histogram(x=x, xbins=dict( start='1969-11-15', end='1972-03-31', - size= 'M2'), # 2 months - autobinx = False - ) + size='M2')) # M2 months fig.append_trace(trace0, 1, 1) fig.append_trace(trace1, 1, 2) @@ -331,6 +327,28 @@ fig.append_trace(trace5, 3, 2) fig.show() ``` +### Share bins between histograms +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`. + +```python +import plotly.graph_objects as go +import numpy as np + +fig = go.Figure(go.Histogram( + x=np.random.randint(7, size=100), + bingroup=1)) + +fig.add_trace(go.Histogram( + x=np.random.randint(7, size=20), + bingroup=1)) + +fig.update_layout( + barmode="overlay", + bargap=0.1) + +fig.show() +``` + ### Dash Example