Show Sidebar Hide Sidebar

Axes in Python

How to adjust axes properties in python. Seven examples of linear and logarithmic axes, axes titles, and styling and coloring axes and grid lines.

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!

Learn about API authentication here: https://plot.ly/python/getting-started Find your api_key here: https://plot.ly/settings/api

Toggling Axes Lines, Ticks, Labels, and Autorange

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

trace1 = go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[8, 7, 6, 5, 4, 3, 2, 1, 0]
)
trace2 = go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[0, 1, 2, 3, 4, 5, 6, 7, 8]
)
data = [trace1, trace2]
layout = go.Layout(
    xaxis=dict(
        autorange=True,
        showgrid=False,
        zeroline=False,
        showline=False,
        autotick=True,
        ticks='',
        showticklabels=False
    ),
    yaxis=dict(
        autorange=True,
        showgrid=False,
        zeroline=False,
        showline=False,
        autotick=True,
        ticks='',
        showticklabels=False
    )
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='axes-booleans')
Out[3]:

Tick Placement, Color, and Style

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

trace1 = go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[8, 7, 6, 5, 4, 3, 2, 1, 0]
)
trace2 = go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[0, 1, 2, 3, 4, 5, 6, 7, 8]
)
data = [trace1, trace2]
layout = go.Layout(
    xaxis=dict(
        autotick=False,
        ticks='outside',
        tick0=0,
        dtick=0.25,
        ticklen=8,
        tickwidth=4,
        tickcolor='#000'
    ),
    yaxis=dict(
        autotick=False,
        ticks='outside',
        tick0=0,
        dtick=0.25,
        ticklen=8,
        tickwidth=4,
        tickcolor='#000'
    )
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='axes-ticks')
Out[4]:

Set and Style Axes Title Labels and Ticks

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

trace1 = go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[8, 7, 6, 5, 4, 3, 2, 1, 0]
)
trace2 = go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[0, 1, 2, 3, 4, 5, 6, 7, 8]
)
data = [trace1, trace2]
layout = go.Layout(
    xaxis=dict(
        title='AXIS TITLE',
        titlefont=dict(
            family='Arial, sans-serif',
            size=18,
            color='lightgrey'
        ),
        showticklabels=True,
        tickangle=45,
        tickfont=dict(
            family='Old Standard TT, serif',
            size=14,
            color='black'
        ),
        exponentformat='e',
        showexponent='All'
    ),
    yaxis=dict(
        title='AXIS TITLE',
        titlefont=dict(
            family='Arial, sans-serif',
            size=18,
            color='lightgrey'
        ),
        showticklabels=True,
        tickangle=45,
        tickfont=dict(
            family='Old Standard TT, serif',
            size=14,
            color='black'
        ),
        exponentformat='e',
        showexponent='All'
    )
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='axes-labels')
Out[5]:

Styling and Coloring Axes and the Zero-Line

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

trace1 = go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[8, 7, 6, 5, 4, 3, 2, 1, 0]
)
trace2 = go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[0, 1, 2, 3, 4, 5, 6, 7, 8]
)
data = [trace1, trace2]
layout = go.Layout(
    xaxis=dict(
        showgrid=True,
        zeroline=True,
        showline=True,
        mirror='ticks',
        gridcolor='#bdbdbd',
        gridwidth=2,
        zerolinecolor='#969696',
        zerolinewidth=4,
        linecolor='#636363',
        linewidth=6
    ),
    yaxis=dict(
        showgrid=True,
        zeroline=True,
        showline=True,
        mirror='ticks',
        gridcolor='#bdbdbd',
        gridwidth=2,
        zerolinecolor='#969696',
        zerolinewidth=4,
        linecolor='#636363',
        linewidth=6
    )
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='axes-lines')
Out[6]:

Setting the Range of Axes Manually

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

trace1 = go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[8, 7, 6, 5, 4, 3, 2, 1, 0]
)
trace2 = go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[0, 1, 2, 3, 4, 5, 6, 7, 8]
)
data = [trace1, trace2]
layout = go.Layout(
    xaxis=dict(
        range=[2, 5]
    ),
    yaxis=dict(
        range=[2, 5]
    )
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='axes-range-manual')
Out[7]:

Logarithmic Axes

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

trace1 = go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[8, 7, 6, 5, 4, 3, 2, 1, 0]
)
trace2 = go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[0, 1, 2, 3, 4, 5, 6, 7, 8]
)
data = [trace1, trace2]
layout = go.Layout(
    xaxis=dict(
        type='log',
        autorange=True
    ),
    yaxis=dict(
        type='log',
        autorange=True
    )
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='axes-range-type')
Out[8]:

Fixed Ratio Axes

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

trace0 = go.Scatter(
  x = [0,1,1,0,0,1,1,2,2,3,3,2,2,3],
  y = [0,0,1,1,3,3,2,2,3,3,1,1,0,0]
)

trace1 = go.Scatter(
  x = [0,1,2,3],
  y = [1,2,4,8],
  yaxis = "y2"
)

trace2 = go.Scatter(
  x = [1,10,100,10,1],
  y = [0,1,2,3,4],
  xaxis = "x2",
  yaxis ="y3",
)

trace3 = go.Scatter(
  x = [1,100,30,80,1],
  y = [1,1.5,2,2.5,3],
  xaxis = "x2",
  yaxis = "y4"
)

data = [trace0,trace1,trace2,trace3]

layout = go.Layout(
    width = 800,
    height = 500,
    title = "fixed-ratio axes",
    xaxis = dict(
      nticks = 10,
      domain = [0, 0.45],
      title = "shared X axis"
    ),
    yaxis = dict(
      scaleanchor = "x",
      domain = [0, 0.45],
      title = "1:1"
    ),
    yaxis2 = dict(
      scaleanchor = "x",
      scaleratio = 0.2,
      domain = [0.55,1],
      title = "1:5"
    ),
    xaxis2 = dict(
      type = "log",
      domain = [0.55, 1],
      anchor = "y3",
      title = "unconstrained log X"
    ),
    yaxis3 = dict(
      domain = [0, 0.45],
      anchor = "x2",
      title = "Scale matches ->"
    ),
    yaxis4 = dict(
      scaleanchor = "y3",
      domain = [0.55, 1],
      anchor = "x2",
      title = "Scale matches <-"
    ),
    showlegend= False
)

fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename = "aspect-ratio")
Out[9]:

Reversed Axes

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

data = [
    go.Scatter(
        x=[1, 2],
        y=[1, 2]
    )
]
layout = go.Layout(
    xaxis=dict(
        autorange='reversed'
    )
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='axes-reversed')
Out[10]:

Reversed Axes with Range ( Min/Max ) Specified

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

import numpy as np

x = np.linspace(0, 10, 100)
y = np.random.randint(1, 100, 100)

trace = go.Scatter(x=x, y=y, mode='markers')
data = [trace]
layout = go.Layout(title='Reversed Axis with Min/Max', xaxis=dict(autorange='reversed', range=[0, 10]))

fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='example-reversed-axis-with-min-max')
Out[11]:

nonnegative, tozero, and normal Rangemode

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

data = [
    go.Scatter(
        x=[2, 4, 6],
        y=[-3, 0, 3]
    )
]
layout = go.Layout(
    showlegend=False,
    xaxis=dict(
        rangemode='tozero',
        autorange=True
    ),
    yaxis=dict(
        rangemode='nonnegative',
        autorange=True
    )
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='axes-range-mode')
Out[12]:

Enumerated Ticks with Tickvals and Ticktext

In [13]:
import plotly.plotly as py
import plotly.tools as tls    
import plotly.graph_objs as go 

import numpy as np
from pymatgen.io.vasp import Vasprun
from pymatgen.electronic_structure.core import Spin

dosrun = Vasprun("Si_bands/DOS/vasprun.xml")
spd_dos = dosrun.complete_dos.get_spd_dos()
run = Vasprun("Si_bands/Bandes/vasprun.xml", parse_projected_eigen = True)
bands = run.get_band_structure("Si_bands/Bandes/KPOINTS", line_mode=True, efermi=dosrun.efermi)

emin = 1e100
emax = -1e100
for spin in bands.bands.keys():
    for band in range(bands.nb_bands):
        emin = min(emin, min(bands.bands[spin][band]))
        emax = max(emax, max(bands.bands[spin][band]))
emin = emin - bands.efermi - 1
emax = emax - bands.efermi + 1

kptslist = range(len(bands.kpoints))
bandTraces = list()
for band in range(bands.nb_bands):
    bandTraces.append(
        go.Scatter(
            x=kptslist,
            y=[e - bands.efermi for e in bands.bands[Spin.up][band]],
            mode="lines",
            line=go.Line(color="#666666"),
            showlegend=False
        )
    )

labels = [r"$L$", r"$\Gamma$", r"$X$", r"$U,K$", r"$\Gamma$"]
step = len(bands.kpoints) / (len(labels) - 1)
# vertical lines
vlines = list()
for i, label in enumerate(labels):
    vlines.append(
        go.Scatter(
            x=[i * step, i * step],
            y=[emin, emax],
            mode="lines",
            line=go.Line(color="#111111", width=1),
            showlegend=False
        )
    )

bandxaxis = go.XAxis(
    title="k-points",
    range=[0, len(bands.kpoints)],
    showgrid=True,
    showline=True,
    ticks="", 
    showticklabels=True,
    mirror=True,
    linewidth=2,
    ticktext=labels,
    tickvals=[i * step for i in range(len(labels))]
)
bandyaxis = go.YAxis(
    title="$E - E_f \quad / \quad \\text{eV}$",
    range=[emin, emax],
    showgrid=True,
    showline=True,
    zeroline=True,
    mirror="ticks",
    ticks="inside",
    linewidth=2,
    tickwidth=2,
    zerolinewidth=2
)
bandlayout = go.Layout(
    title="Bands diagram of Silicon",
    xaxis=bandxaxis,
    yaxis=bandyaxis,
)

bandfig = go.Figure(data=bandTraces + vlines, layout=bandlayout)
py.iplot(bandfig, filename="Bands_Si", auto_open=False)
Out[13]:

Reference

See https://plot.ly/python/reference/#layout-xaxis and https://plot.ly/python/reference/#layout-yaxis for more information and chart attribute options!

Still need help?
Contact Us

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