Skip to content

[MRG] histogram nb #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 21, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
some inlining
  • Loading branch information
emmanuelle committed Jun 14, 2019
commit a15e562b2cb08597a9f3f34a19c1317e0d378e36
63 changes: 35 additions & 28 deletions notebooks/histograms.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ jupyter:
display_name: Python 3
language: python
name: python3
language_info:
codemirror_mode:
name: ipython
version: 3
file_extension: .py
mimetype: text/x-python
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
version: 3.6.7
plotly:
description: How to make Histograms in Python with Plotly.
display_as: statistical
Expand All @@ -25,6 +35,7 @@ jupyter:
redirect_from: /python/histogram-tutorial/
thumbnail: thumbnail/histogram.jpg
title: Python Histograms | plotly
v4upgrade: true
---

## Histogram with plotly express
Expand Down Expand Up @@ -134,9 +145,8 @@ import plotly.graph_objs as go
import numpy as np

x = np.random.randn(500)
data = [go.Histogram(x=x, histnorm='probability')]
fig = go.Figure(data=[go.Histogram(x=x, histnorm='probability')])

fig = go.Figure(data=data)
fig.show()
```

Expand All @@ -149,9 +159,8 @@ import numpy as np

y = np.random.randn(500)
# Use `y` argument instead of `x` for horizontal histogram
data = [go.Histogram(y=y)]

fig = go.Figure(data=data)
fig = go.Figure(data=[go.Histogram(y=y)])
fig.show()
```

Expand All @@ -166,10 +175,10 @@ x0 = np.random.randn(500)
# Add 1 to shift the mean of the Gaussian distribution
x1 = np.random.randn(500) + 1

trace0 = go.Histogram(x=x0)
trace1 = go.Histogram(x=x1)
fig = go.Figure()
fig.add_trace(go.Histogram(x=x0))
fig.add_trace(go.Histogram(x=x1))

fig = go.Figure(data=[trace0, trace1])
# Overlay both histograms
fig.update(layout_barmode='overlay')
# Reduce opacity to see both histograms
Expand All @@ -186,10 +195,10 @@ import numpy as np
x0 = np.random.randn(2000)
x1 = np.random.randn(2000) + 1

trace0 = go.Histogram(x=x0)
trace1 = go.Histogram(x=x1)
fig = go.Figure()
fig.add_trace(go.Histogram(x=x0))
fig.add_trace(go.Histogram(x=x1))

fig = go.Figure(data=[trace0, trace1])
# The two histograms are drawn on top of another
fig.update(layout_barmode='stack')
```
Expand All @@ -203,7 +212,8 @@ import numpy as np
x0 = np.random.randn(500)
x1 = np.random.randn(500) + 1

trace0 = go.Histogram(
fig = go.Figure()
fig.add_trace(go.Histogram(
x=x0,
histnorm='percent',
name='control', # name used in legend and hover labels
Expand All @@ -214,8 +224,8 @@ trace0 = go.Histogram(
),
marker_color='#EB89B5',
opacity=0.75
)
trace1 = go.Histogram(
))
fig.add_trace(go.Histogram(
x=x1,
histnorm='percent',
name='experimental',
Expand All @@ -226,16 +236,16 @@ trace1 = go.Histogram(
),
marker_color='#330C73',
opacity=0.75
)
))

layout = go.Layout(
fig.update(layout=go.Layout(
title='Sampled Results', # title of plot
xaxis_title='Value', # xaxis label
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reading this reminds me that we should use the new-style title object instead of string: https://github.com/plotly/plotly.py-docs/issues/17

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I will change this. However in px the argument is called title and not title_text should this be changed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm comfortable with px keeping title as it's designed to be a lighter-weight API :)

yaxis_title='Count', # yaxis label
bargap=0.2, # gap between bars of adjacent location coordinates
bargroupgap=0.1 # gap between bars of the same location coordinates
)
fig = go.Figure(data=[trace0, trace1], layout=layout)
))

fig.show()
```

Expand All @@ -247,9 +257,8 @@ import plotly.graph_objs as go
import numpy as np

x = np.random.randn(500)
data = [go.Histogram(x=x, cumulative_enabled=True)]
fig = go.Figure(data=[go.Histogram(x=x, cumulative_enabled=True)])

fig = go.Figure(data=data)
fig.show()
```

Expand All @@ -261,23 +270,21 @@ import plotly.graph_objs as go
x = ["Apples","Apples","Apples","Oranges", "Bananas"]
y = ["5","10","3","10","5"]

data = [
go.Histogram(
fig = go.Figure()
fig.add_trace(go.Histogram(
histfunc = "count",
y=y,
x=x,
name="count"
),
go.Histogram(
))
fig.add_trace(go.Histogram(
histfunc="sum",
y=y,
x=x,
name="sum"
)
]
))

fig = go.Figure(data=data)
fig
fig.show()
```

### Custom Binning
Expand All @@ -290,6 +297,7 @@ from plotly.subplots import make_subplots
x = ['1970-01-01', '1970-01-01', '1970-02-01', '1970-04-01', '1970-01-02',
'1972-01-31', '1970-02-13', '1971-04-19']

fig = make_subplots(rows=3, cols=2)

trace0 = go.Histogram(x=x, nbinsx=4)
trace1 = go.Histogram(x=x, nbinsx = 8)
Expand All @@ -316,7 +324,6 @@ trace5 = go.Histogram(x=x,
autobinx = False
)

fig = make_subplots(rows=3, cols=2)
fig.append_trace(trace0, 1, 1)
fig.append_trace(trace1, 1, 2)
fig.append_trace(trace2, 2, 1)
Expand Down