Styling Markers in Python/v3

How to style markers in Python with Plotly.


Note: this page is part of the documentation for version 3 of Plotly.py, which is not the most recent version.
See our Version 4 Migration Guide for information about how to upgrade.
The version 4 version of this page is here.

New to Plotly?

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check

Plotly's python package is updated frequently. Run pip install plotly --upgrade to use the latest version.

In [3]:
import plotly
plotly.__version__
Out[3]:
'2.4.1'

Add Marker Border

In order to make markers distinct, you can add a border to the markers. This can be achieved by adding the line dict to the marker dict. For example, marker:{..., line: {...}}.

In [4]:
import plotly.plotly as py
import plotly.graph_objs as go

import numpy as np

x = np.random.uniform(low=3, high=6, size=(500,))
y = np.random.uniform(low=3, high=6, size=(500,))

data = [
    go.Scatter(
        mode = 'markers',
        x = x,
        y = y,
        marker = dict(
          color = 'rgb(17, 157, 255)',
          size = 20,
          line = dict(
            color = 'rgb(231, 99, 250)',
            width = 2
          )
        ),
        showlegend = False
  ),
  go.Scatter(
      mode = 'markers',
      x = [2],
      y = [4.5],
      marker = dict(
        color = 'rgb(17, 157, 255)',
        size = 120,
        line = dict(
          color = 'rgb(231, 99, 250)',
          width = 12
        )
      ),
      showlegend = False
  )]

py.iplot(data, filename = "style-add-border")
Out[4]:

Fully Opaque

Fully opaque, the default setting, is useful for non-overlapping markers. When many points overlap it can be hard to observe density.

In [5]:
import plotly.plotly as py
import plotly.graph_objs as go

import numpy as np

x = np.random.uniform(low=3, high=6, size=(500,))
y = np.random.uniform(low=3, high=6, size=(500,))

data = [
    go.Scatter(
        mode = 'markers',
        x = x,
        y = y,
        marker = dict(
          color = 'rgb(17, 157, 255)',
          size = 20,
          line = dict(
            color = 'rgb(231, 99, 250)',
            width = 2
          )
        ),
        showlegend = False
      ),
      go.Scatter(
          mode = 'markers',
          x = [2,2],
          y = [4.25,4.75],
          marker = dict(
            color = 'rgb(17, 157, 255)',
            size = 80,
            line = dict(
              color = 'rgb(231, 99, 250)',
              width = 8
            )
          ),
          showlegend = False
    )]

py.iplot(data, filename = "style-full-opaque")
Out[5]:

Opacity

Setting opacity outside the marker will set the opacity of the trace. Thus, it will allow greater visbility of additional traces but like fully opaque it is hard to distinguish density.

In [6]:
import plotly.plotly as py
import plotly.graph_objs as go

import numpy as np

x = np.random.uniform(low=3, high=6, size=(500,))
y = np.random.uniform(low=3, high=4.5, size=(500,))
x2 = np.random.uniform(low=3, high=6, size=(500,))
y2 = np.random.uniform(low=4.5, high=6, size=(500,))

data = [
    go.Scatter(
        mode = 'markers',
        x = x,
        y = y,
        opacity = 0.5,
        marker = dict(
          color = 'rgb(17, 157, 255)',
          size = 20,
          line = dict(
            color = 'rgb(231, 99, 250)',
            width = 2
          )
        ),
        name = 'Opacity 0.5'
      ),
     go.Scatter(
       mode = 'markers',
       x = x2,
       y = y2,
       marker = dict(
         color = 'rgb(17, 157, 255)',
         size = 20,
         line = dict(
           color = 'rgb(231, 99, 250)',
           width = 2
         )
       ),
       name = 'Opacity 1.0'
     ),
     go.Scatter(
        mode = 'markers',
        x = [2,2],
        y = [4.25,4.75],
        opacity = 0.5,
        marker = dict(
           color = 'rgb(17, 157, 255)',
           size = 80,
           line = dict(
              color = 'rgb(231, 99, 250)',
              width = 8
           )
        ),
        showlegend = False
    )]

py.iplot(data, filename = "style-opacity")
Out[6]:

Marker Opacity

To maximise visibility of density, it is recommended to set the opacity inside the marker marker:{opacity:0.5}. If multiple traces exist with high density, consider using marker opacity in conjunction with trace opacity.

In [7]:
import plotly.plotly as py
import plotly.graph_objs as go

import numpy as np

x = np.random.uniform(low=3, high=6, size=(500,))
y = np.random.uniform(low=3, high=6, size=(500,))

data = [
    go.Scatter(
        mode = 'markers',
        x = x,
        y = y,
        marker = dict(
          color = 'rgb(17, 157, 255)',
          size = 20,
          opacity = 0.5,
          line = dict(
            color = 'rgb(231, 99, 250)',
            width = 2
          )
        ),
        showlegend = False
      ),
      go.Scatter(
          mode = 'markers',
          x = [2,2],
          y = [4.25,4.75],
          marker = dict(
            color = 'rgb(17, 157, 255)',
            size = 80,
            opacity = 0.5,
            line = dict(
              color = 'rgb(231, 99, 250)',
              width = 8
            )
          ),
          showlegend = False
    )]

py.iplot(data, filename = "style-marker-opacity")
Out[7]:

Color Opacity

To maximise visibility of each point, set the color opacity by using alpha: marker:{color: 'rgba(0,0,0,0.5)'}. Here, the marker line will remain opaque.

In [8]:
import plotly.plotly as py
import plotly.graph_objs as go

import numpy as np

x = np.random.uniform(low=3, high=6, size=(500,))
y = np.random.uniform(low=3, high=6, size=(500,))

data = [
    go.Scatter(
        mode = 'markers',
        x = x,
        y = y,
        marker = dict(
          color = 'rgba(17, 157, 255, 0.5)',
          size = 20,
          line = dict(
            color = 'rgb(231, 99, 250)',
            width = 2
          )
        ),
        showlegend = False
      ),
      go.Scatter(
          mode = 'markers',
          x = [2,2],
          y = [4.25,4.75],
          marker = dict(
            color = 'rgba(17, 157, 255, 0.5)',
            size = 80,
            line = dict(
              color = 'rgb(231, 99, 250)',
              width = 8
            )
          ),
          showlegend = False
    )]

py.iplot(data, filename = "style-color-opacity")
Out[8]:

Reference

See https://plotly.com/python/reference/ for more information and chart attribute options!