# Plot of a discrete distribution

**URL:** <https://community.plotly.com/t/plot-of-a-discrete-distribution/20697>\
**Category:** 📊 Plotly Python\
**Created:** [March 10, 2019, 12:28am UTC](https://community.plotly.com/t/plot-of-a-discrete-distribution/20697 "2019-03-10T00:28:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ursus](https://avatars.discourse-cdn.com/v4/letter/u/e99b99/32.png) [@ursus](https://community.plotly.com/u/ursus)\
**Post date:** [March 10, 2019, 12:28am UTC](https://community.plotly.com/t/plot-of-a-discrete-distribution/20697/1 "2019-03-10T00:28:48Z")

</div>

The image below contains plots of the binomial distribution produced with Mathematica:

![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/8/8c4b2efdc4162595525a2b5c8bb347dc557cb35f.png)

Source: [https://reference.wolfram.com/language/ref/DiscretePlot.html](https://reference.wolfram.com/language/ref/DiscretePlot.html)

How do I create similar plots, i.e. of a discrete distribution, in Python?  
I can plot the dots, but, I want the distribution to appear as shown in the image.

---

<div class="post-metadata">

**Author:** ![jmmease](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/jmmease/32/3706_2.png) [@jmmease](https://community.plotly.com/u/jmmease)\
**Post date:** [March 11, 2019, 9:54am UTC](https://community.plotly.com/t/plot-of-a-discrete-distribution/20697/2 "2019-03-11T09:54:38Z")

</div>

Hi @ursus,

If you’ve already calculated the point locations then you could draw the vertical bars using `bar` traces ([https://plot.ly/python/bar-charts/](https://plot.ly/python/bar-charts/)) underneath the `scatter` trace holding the points.

To match the aesthetic of your example, set the `layout.bargap` to something like 0.8 (0 is no gap between and 1 is a bar width of zero), and set the `bar.marker.color` to the same color as the scatter markers but set the `bar.marker.opacity` to something like 0.6.

Hope that helps!  
-Jon

---

<div class="post-metadata">

**Author:** ![ursus](https://avatars.discourse-cdn.com/v4/letter/u/e99b99/32.png) [@ursus](https://community.plotly.com/u/ursus)\
**Post date:** [March 11, 2019, 11:20am UTC](https://community.plotly.com/t/plot-of-a-discrete-distribution/20697/3 "2019-03-11T11:20:45Z")

</div>

Thank you!  
For posterity, here is the code:

```python
fig = go.FigureWidget()
xs = np.arange(51)
for el in zip([0.3, 0.5, 0.8], ['#1f77b4', '#2ca02c', '#9467bd']):
    ys = stats.binom.pmf(xs, n=50, p=el[0])
    fig.add_scatter(x=xs, y=ys, mode='markers')
    fig.add_bar(x=xs, y=ys, marker = dict(color=el[1], opacity=0.5))
fig.layout = dict(showlegend = False, bargap=0.75, barmode='overlay', width = 800)
fig

```

The plot:

 ![newplot%20(2)](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/2/2bdb57b72b83418e291f38edc41f004f14370106.png)

Is it possible instead of `color=el[1]` to have something like `color=scatter.marker.color` (`scatter` would be the name of the scatter plot)?

---

<div class="post-metadata">

**Author:** ![jmmease](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/jmmease/32/3706_2.png) [@jmmease](https://community.plotly.com/u/jmmease)\
**Post date:** [March 12, 2019, 9:26am UTC](https://community.plotly.com/t/plot-of-a-discrete-distribution/20697/4 "2019-03-12T09:26:06Z")

</div>

> [@ursus](#):
>
> Is it possible instead of `color=el[1]` to have something like `color=scatter.marker.color` ( `scatter` would be the name of the scatter plot)?

Yes, if I understand you correctly. You could do something like

```python
scatter = fig.add_scatter(x=xs, y=ys, mode='markers')
fig.add_bar(x=xs, y=ys, marker = dict(color=scatter.marker.color, opacity=0.5))

```

-Jon

---

<div class="post-metadata">

**Author:** ![ursus](https://avatars.discourse-cdn.com/v4/letter/u/e99b99/32.png) [@ursus](https://community.plotly.com/u/ursus)\
**Post date:** [March 12, 2019, 10:44am UTC](https://community.plotly.com/t/plot-of-a-discrete-distribution/20697/5 "2019-03-12T10:44:03Z")

</div>

That is allowed, but somehow the bars do not inherit the color of `scatter`.  
So, `color=scatter.marker.color` does not appear to have any effect:

```python
import numpy as np
from scipy import stats
import plotly.graph_objs as go

fig = go.FigureWidget()
xs = np.arange(51)
ys = stats.binom.pmf(xs, n=50, p=0.3)
scatter = fig.add_scatter(x=xs, y=ys, mode='markers')
fig.add_bar(x=xs, y=ys, marker = dict(color=scatter.marker.color, opacity=0.3))
fig.layout = dict(showlegend = False, bargap=0.75, barmode='overlay', width = 800)
fig

```

will produce:

 ![newplot%20(3)](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/7/7156622129d81c683e1be904c7e943f7840298d6.png)

If I then change the name to `dots` but keep `scatter.marker.color`:

```python
fig = go.FigureWidget()
xs = np.arange(51)
ys = stats.binom.pmf(xs, n=50, p=0.3)
dots = fig.add_scatter(x=xs, y=ys, mode='markers')
fig.add_bar(x=xs, y=ys, marker = dict(color=scatter.marker.color, opacity=0.3))
fig.layout = dict(showlegend = False, bargap=0.75, barmode='overlay', width = 800)
fig

```

I get what I want:

 ![newplot%20(4)](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/2/2d997aaf14c66310d6b2efd5e7a51d550f588341.png)

So, I think `scatter` gets initialized when `fig` is called… or something like that. In my case it remembered the `scatter` attributes from the previous call. Indeed when I place `fig.add_bar(x=xs, y=ys, marker = dict(color=scatter.marker.color, opacity=0.3))` in a different cell (after I’ve run `fig`) I get the right output. It is not very elegant though to have bits of code pertaining to the same plot in different cells, and I don’t know how (aside from using dictionaries) this can be done in a loop (as shown in the first piece of code above).

Maybe I’m missing something.

---

<div class="post-metadata">

**Author:** ![jmmease](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/jmmease/32/3706_2.png) [@jmmease](https://community.plotly.com/u/jmmease)\
**Post date:** [March 15, 2019, 9:51am UTC](https://community.plotly.com/t/plot-of-a-discrete-distribution/20697/6 "2019-03-15T09:51:10Z")

</div>

Hi @ursus,

I didn’t realize that you weren’t initializing the `scatter` trace with the color that you want. The reason this isn’t quite working is that the default value of `scatter.marker.color` that is chosen by plotly.js is synchronized back to the Python model asynchronously, so the color property is not available yet when you make the `add_bar` call.

So your choices are to either specify the `scatter.marker.color` property manually, or wait for the color to be updated before calling `add_bar`. One way to wait for this update to occur is to use an `on_edits_completed` callback. This lets you register a function to be called after all of the default properties have been synchronized back to the Python model. For example:

```auto
import numpy as np
from scipy import stats
import plotly.graph_objs as go

fig = go.FigureWidget()
xs = np.arange(51)
ys = stats.binom.pmf(xs, n=50, p=0.3)
scatter = fig.add_scatter(x=xs, y=ys, mode='markers')
fig.on_edits_completed(
    lambda: fig.add_bar(
        x=xs, y=ys,marker = dict(color=scatter.marker.color, opacity=0.3)))
fig.layout = dict(showlegend = False, bargap=0.75, barmode='overlay', width = 800)
fig

```

 ![newplot](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/f/fa8201072ab6279ca18d571c6930ac110f7a0192.png)

Hope that helps,  
-Jon
