# Scatterplot with concentration hue

**URL:** <https://community.plotly.com/t/scatterplot-with-concentration-hue/26188>\
**Category:** 📊 Plotly Python\
**Created:** [July 22, 2019, 8:01am UTC](https://community.plotly.com/t/scatterplot-with-concentration-hue/26188 "2019-07-22T08:01:57Z")\
**Posts on this page:** 7\
**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:** [July 22, 2019, 8:01am UTC](https://community.plotly.com/t/scatterplot-with-concentration-hue/26188/1 "2019-07-22T08:01:57Z")

</div>

A simple scatterplot can be created as follows:

```python
a, b = np.random.lognormal(size=(2,500))

```

```python
fig = go.FigureWidget()
trace = fig.add_scatter(x=a, y=b, mode='markers', marker=dict(size=10), opacity=0.2)
fig

```

 ![newplot](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/a/a6456c7c67cac768216dc7796133137ddaff2ef1.png)  
I would like there to be a darker hue for the regions where the concentration is higher.

I can achieve that if I plot each point separately:

```python
X = zip(a, b)

```

```python
fig = go.FigureWidget()
for el in X:
    fig.add_scatter(x=[el[0]],y=[el[1]], marker=dict(size=10, color=trace.marker.color), opacity=0.2)
fig.layout.update(showlegend=False)
fig

```

 ![newplot%20(1)](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/3/31c0e48846b6a5623dc23f20e8accf0a6ee7a908.png)  
Plotting each point separately seems to be somewhat inefficient.

**Question** : How can I achieve the same result without having to plot each point separately.

* * *

UPDATE:

I’ve just upgraded to 4.0.0 and it seems that I can’t use `trace.marker.color`.  
How can I get the color of `trace` now?

---

<div class="post-metadata">

**Author:** ![michaelbabyn](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/michaelbabyn/32/3569_2.png) [@michaelbabyn](https://community.plotly.com/u/michaelbabyn)\
**Post date:** [July 22, 2019, 3:55pm UTC](https://community.plotly.com/t/scatterplot-with-concentration-hue/26188/2 "2019-07-22T15:55:56Z")

</div>

@ursus, You can get the same result by using the marker’s `opacity` attribute instead of the trace’s. `fig.add_scatter(x=a, y=b, mode='markers', marker=dict(size=10, opacity=0.2))`.

Also, [starting in version 4](https://plot.ly/python/v4-migration/#add-trace-return-value) of plotly.py `fig.add_scatter()` returns the entire fig object and not the newly added trace as it did before. This makes it possible to easily chain method calls but it means that you will need to use something like `fig.add_scatter(x=a, y=b, mode='markers', marker=dict(size=10, opacity=0.2)).data[-1]` if you want to get just the newly added trace.

---

<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:** [July 22, 2019, 9:35pm UTC](https://community.plotly.com/t/scatterplot-with-concentration-hue/26188/3 "2019-07-22T21:35:06Z")

</div>

Thank you, @michaelbabyn!

But, I still cannot get the marker color. Is that something that can be done in version 4?

---

<div class="post-metadata">

**Author:** ![michaelbabyn](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/michaelbabyn/32/3569_2.png) [@michaelbabyn](https://community.plotly.com/u/michaelbabyn)\
**Post date:** [July 23, 2019, 7:18pm UTC](https://community.plotly.com/t/scatterplot-with-concentration-hue/26188/4 "2019-07-23T19:18:31Z")

</div>

@ursus, That works for me with Plotly.py 4.0 in jupyter notebook and jupyterlab ~~but I can’t get it to work in jupyterlab~~. Where are you running your code and which versions of jupyter/notebook/ipywidgets are you using?

---

<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:** [July 23, 2019, 8:49pm UTC](https://community.plotly.com/t/scatterplot-with-concentration-hue/26188/5 "2019-07-23T20:49:19Z")

</div>

`trace.data[-1].marker.color` seems to work in `jupyter notebook`. I might have tried something different.

My version of `notebook` is 6.0.0.

My version of `JupyterLab` is 1.0.2 and unfortunately I cannot use it now. There appears to be an [issue](https://github.com/jupyterlab/jupyterlab/issues/6858#issuecomment-513765979) with it.

---

<div class="post-metadata">

**Author:** ![michaelbabyn](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/michaelbabyn/32/3569_2.png) [@michaelbabyn](https://community.plotly.com/u/michaelbabyn)\
**Post date:** [August 9, 2019, 3:01am UTC](https://community.plotly.com/t/scatterplot-with-concentration-hue/26188/6 "2019-08-09T03:01:41Z")

</div>

@ursus, did you ever get jupyterlab to load properly? I ran into a similar issue recently and I noticed the below error in my browser console regarding duplicate plugins being registered.

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

I was able to fix it by getting rid of the offending plugins from `~/.conda/envs/plotly-env/share/jupyter/lab/extensions/` and then reinstalling them.

---

<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:** [August 9, 2019, 10:04am UTC](https://community.plotly.com/t/scatterplot-with-concentration-hue/26188/7 "2019-08-09T10:04:36Z")

</div>

Yes:

> <https://github.com/plotly/plotly.py/issues/1686>

The question now is: why does that happen?
