Show Sidebar Hide Sidebar

Dot Plots in Python

How to make dot plots in Python with Plotly.

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 [1]:
import plotly
plotly.__version__
Out[1]:
'2.0.8'

Basic Dot Plot

Dot plots show changes between two points in time or between two conditions.

In [5]:
import plotly.plotly as py
from plotly.graph_objs import *

trace1 = {"x": [72, 67, 73, 80, 76, 79, 84, 78, 86, 93, 94, 90, 92, 96, 94, 112], 
          "y": ["Brown", "NYU", "Notre Dame", "Cornell", "Tufts", "Yale",
                "Dartmouth", "Chicago", "Columbia", "Duke", "Georgetown",
                "Princeton", "U.Penn", "Stanford", "MIT", "Harvard"], 
          "marker": {"color": "pink", "size": 12}, 
          "mode": "markers", 
          "name": "Women", 
          "type": "scatter"
}

trace2 = {"x": [92, 94, 100, 107, 112, 114, 114, 118, 119, 124, 131, 137, 141, 151, 152, 165], 
          "y": ["Brown", "NYU", "Notre Dame", "Cornell", "Tufts", "Yale",
                "Dartmouth", "Chicago", "Columbia", "Duke", "Georgetown",
                "Princeton", "U.Penn", "Stanford", "MIT", "Harvard"], 
          "marker": {"color": "blue", "size": 12}, 
          "mode": "markers", 
          "name": "Men", 
          "type": "scatter", 
}

data = Data([trace1, trace2])
layout = {"title": "Gender Earnings Disparity", 
          "xaxis": {"title": "Annual Salary (in thousands)", }, 
          "yaxis": {"title": "School"}}

fig = Figure(data=data, layout=layout)
py.iplot(fig, filenmae='basic_dot-plot')
Out[5]:

Styled Categorical Dot Plot

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

country = ['Switzerland (2011)', 'Chile (2013)', 'Japan (2014)',
           'United States (2012)', 'Slovenia (2014)', 'Canada (2011)',
           'Poland (2010)', 'Estonia (2015)', 'Luxembourg (2013)', 'Portugal (2011)']
voting_pop = [40, 45.7, 52, 53.6, 54.1, 54.2, 54.5, 54.7, 55.1, 56.6]
reg_voters = [49.1, 42, 52.7, 84.3, 51.7, 61.1, 55.3, 64.2, 91.1, 58.9]

trace0 = go.Scatter(
    x=voting_pop,
    y=country,
    mode='markers',
    name='Percent of estimated voting age population',
    marker=dict(
        color='rgba(156, 165, 196, 0.95)',
        line=dict(
            color='rgba(156, 165, 196, 1.0)',
            width=1,
        ),
        symbol='circle',
        size=16,
    )
)
trace1 = go.Scatter(
    x=reg_voters,
    y=country,
    mode='markers',
    name='Percent of estimated registered voters',
    marker=dict(
        color='rgba(204, 204, 204, 0.95)',
        line=dict(
            color='rgba(217, 217, 217, 1.0)',
            width=1,
        ),
        symbol='circle',
        size=16,
    )
)

data = [trace0, trace1]
layout = go.Layout(
    title="Votes cast for ten lowest voting age population in OECD countries",
    xaxis=dict(
        showgrid=False,
        showline=True,
        linecolor='rgb(102, 102, 102)',
        titlefont=dict(
            color='rgb(204, 204, 204)'
        ),
        tickfont=dict(
            color='rgb(102, 102, 102)',
        ),
        autotick=False,
        dtick=10,
        ticks='outside',
        tickcolor='rgb(102, 102, 102)',
    ),
    margin=dict(
        l=140,
        r=40,
        b=50,
        t=80
    ),
    legend=dict(
        font=dict(
            size=10,
        ),
        yanchor='middle',
        xanchor='right',
    ),
    width=800,
    height=600,
    paper_bgcolor='rgb(254, 247, 234)',
    plot_bgcolor='rgb(254, 247, 234)',
    hovermode='closest',
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='lowest-oecd-votes-cast')
Out[2]:

Reference

See https://plot.ly/python/reference/#scatter for more information and chart attribute options!

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.