Show Sidebar Hide Sidebar

Axes in matplotlib

How to customize axes to plots in matplotlib.

import matplotlib.pyplot as plt
import numpy as np

import plotly.plotly as py
import plotly.tools as tls
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api


mpl_fig = plt.figure()
ax = mpl_fig.add_subplot(111)

x=[0, 1, 2, 3, 4, 5, 6, 7, 8]
y=[0, 4, 5, 1, 8, 5, 3, 2, 9]

line, = ax.plot(x, y, lw=2)

ax.set_title("My Plot Title")
ax.set_xlabel("This is the X Axis")
ax.set_ylabel("This is the Y Axis")

plotly_fig = tls.mpl_to_plotly( mpl_fig )
plot_url = py.plot_mpl(plotly_fig, filename='mpl-axes-labels')

plot_url = py.plot_mpl(plotly_fig, filename='mpl-annotation-with-custom-font-size')
import matplotlib.pyplot as plt
import numpy as np

import plotly.plotly as py
import plotly.tools as tls
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

mpl_fig = plt.figure()
ax = mpl_fig.add_subplot(111)

t = np.arange(-5.0, 5.0, 0.01)
s = np.sin(4 * np.pi * np.absolute(t)) * np.exp(-5 * np.absolute(t))
line, = ax.plot(t, s, lw=2)

ax.set_title("My Plot Title")
ax.set_xlabel("This is X Axis label with fontsize=16", fontsize=18)
ax.set_ylabel("This is Y Axis with fontsize=16", fontsize=16)

plotly_fig = tls.mpl_to_plotly( mpl_fig )

plot_url = py.plot(plotly_fig, filename='mpl-axes-font-size')
import matplotlib.pyplot as plt
import numpy as np

import plotly.plotly as py
import plotly.tools as tls
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api


x = np.arange(1, 7, 0.4)
y0 = np.sin(x)
y = y0.copy() + 2.5


plt.title('Setting Axis Limits')
plt.step(x, y)

y -= 0.5
plt.step(x, y)

y -= 0.5
plt.step(x, y)

plt.xlim(0, 7) 
plt.ylim(-0.5, 4)

plt.xlabel('X Axis limit is (0,7)')
plt.ylabel('Y Axis limit is (-0.5,4)')

mpl_fig = plt.gcf()
plotly_fig = tls.mpl_to_plotly( mpl_fig )
plot_url = py.plot(plotly_fig)
Inspired by matplotlib tutorials
import matplotlib.pyplot as plt
import numpy as np

import plotly.plotly as py
import plotly.tools as tls
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api


x=[1, 2, 3, 4, 5]
y=[2, 4, 7, 9, 5]

y1=[20, 45, 79, 90, 190]
plt.plot(x,y, color="r")
plt.plot(x,y1, color="b")
fig = plt.gcf()


plotly_fig = tls.mpl_to_plotly(fig)
plotly_fig['data'][0].update({'name':'YAxis1 Data'})
plotly_fig['data'][1].update({'yaxis':'y2', 'name':'YAxis2 Data'})

plotly_fig['layout'].update(yaxis=dict(title='Y Axis1', 
                                           titlefont=dict(color='rgb(255, 0, 0)'),
                                           tickfont=dict(color='rgb(255, 0, 0)'),
                                           side='left')
                                )

plotly_fig['layout'].update(yaxis2=dict(title='Y Axis2', 
                                           titlefont=dict(color='rgb(0, 0, 255)'),
                                           tickfont=dict(color='rgb(0, 0, 255)'), 
                                           overlaying='y',side='right')
                                )

plotly_fig['layout']['showlegend'] = True
plot_url = py.plot(plotly_fig, filename='mpl-two-y-axis')
import matplotlib.pyplot as plt
import numpy as np

import plotly.plotly as py
import plotly.tools as tls
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

mpl_fig = plt.figure()
ax = mpl_fig.add_subplot(111)

ax.plot(range(10))
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')

ax.xaxis.label.set_color('red')
ax.yaxis.label.set_color('green')

plotly_fig = tls.mpl_to_plotly( mpl_fig )
plot_url = py.plot(plotly_fig, filename='mpl-axes-color')
Inspired by Stack Overflow
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.tools as tls
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api


import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
x = np.linspace(0, 14, 100)
flip = -1
for i in range(1, 7):
    plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)

plotly_fig = tls.mpl_to_plotly(fig)
plotly_fig['layout']['xaxis']['showgrid'] = True
plotly_fig['layout']['yaxis']['showgrid'] = True

plotly_url = py.plot(plotly_fig, filename='background-grid')
import matplotlib.pyplot as plt
import numpy as np

import plotly.plotly as py
import plotly.tools as tls
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api


mpl_fig = plt.figure()
ax = mpl_fig.add_subplot(111)

x=[0, 1, 2, 3, 4, 5, 6, 7, 8]
y=[0, 4, 5, 1, 8, 5, 3, 2, 9]

line, = ax.plot(x, y, lw=2)

plotly_fig = tls.mpl_to_plotly( mpl_fig )



plotly_fig['layout']['xaxis1'].update({'ticktext': ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight'],
                                       'tickvals': [0, 1, 2, 3, 4, 5, 6, 7, 8],
                                       'tickfont': {'size': 14, 'family':'Courier New, monospace'},
                                       'tickangle': 60
                                       })

plotly_fig['layout']['yaxis1'].update({'ticktext': ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'], 
	                                   'tickvals': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
                                       'tickfont': {'size': 14, 'family':'Courier New, monospace'},
                                       'tickangle': 60
	                                   })

plot_url = py.plot(plotly_fig, filename='mpl-custom-xticks-yticks')
import matplotlib.pyplot as plt
import numpy as np

import plotly.plotly as py
import plotly.tools as tls
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

# Plot circle or radius 3
an = np.linspace(0, 2*np.pi, 100)

mpl_fig=plt.figure()
plt.subplot(221)
plt.plot(3*np.cos(an), 3*np.sin(an))
plt.title('not equal, looks like ellipse', fontsize=10)

plt.subplot(222)
plt.plot(3*np.cos(an), 3*np.sin(an))
plt.axis('equal')
plt.title('equal, looks like circle', fontsize=10)

plt.subplot(223)
plt.plot(3*np.cos(an), 3*np.sin(an))
plt.axis('equal')
plt.axis([-3, 3, -3, 3])
plt.title('looks like circle, even after changing limits', fontsize=10)

plt.subplot(224)
plt.plot(3*np.cos(an), 3*np.sin(an))
plt.axis('equal')
plt.axis([-3, 3, -3, 3])
plt.plot([0, 4], [0, 4])
plt.title('still equal after adding line', fontsize=10)
plt.tight_layout()


plotly_fig = tls.mpl_to_plotly( mpl_fig )
plot_url = py.plot(plotly_fig)
Inspired by Matplotlib Docs
import matplotlib.pyplot as plt
import numpy as np

import plotly.plotly as py
import plotly.tools as tls
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

mpl_fig = plt.figure()
ax = mpl_fig.add_subplot(111)

t = np.arange(-5.0, 5.0, 0.01)
s = np.sin(4 * np.pi * np.absolute(t)) * np.exp(-5 * np.absolute(t))
line, = ax.plot(t, s, lw=2)

ax.set_ylabel(r'$\ln\left(\frac{x_a-x_b}{x_a-x_c}\right)$')
ax.set_xlabel(r'$\ln\left(\frac{x_a-x_d}{x_a-x_e}\right)$')

plotly_fig = tls.mpl_to_plotly( mpl_fig )
plot_url = py.plot_mpl(plotly_fig, filename='mpl-axes-labels-latex')
Inspired by Stack Overflow
import matplotlib.pyplot as plt
import numpy as np

import plotly.plotly as py
import plotly.tools as tls
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api


mpl_fig = plt.figure()
ax = mpl_fig.add_subplot(111)

x = ['01-02', '02-02', '03-02', '04-02', '05-02']
y = [2, 2, 3, 7, 2]

ax.bar(range(len(y)), y, width=0.3,align='center',color='skyblue')
plt.xticks(range(len(y)), x, size='small')
ax.set_title('Bar chart with custom background grid')
ax.grid()

plotly_fig = tls.mpl_to_plotly( mpl_fig )
plotly_fig['layout']['xaxis']['showgrid'] = True
plotly_fig['layout']['xaxis']['gridcolor'] = "rgb(31, 119, 180)"
plotly_fig['layout']['xaxis']['zeroline'] = True

plotly_fig['layout']['yaxis']['showgrid'] = True
plotly_fig['layout']['yaxis']['gridcolor'] = "rgb(31, 119, 180)"

py.plot(plotly_fig)
import matplotlib.pyplot as plt
import numpy as np

import plotly.plotly as py
import plotly.tools as tls
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

fig = plt.figure()                                                               
ax = fig.add_subplot(1,1,1)                                                      
x = np.linspace(1,100,100)
y = np.random.randint(1,100,100)
ax.plot(x, y)

# Ticks after every 20 .. 
major_ticks = np.arange(0, 101, 20)                                              
ax.set_xticks(major_ticks)                                                       
ax.set_yticks(major_ticks)                                                       

# and a corresponding grid                                                       
ax.grid(which='both') 

plotly_fig = tls.mpl_to_plotly(fig)
plotly_url = py.plotp(plotly_fig, filename='mpl-grid-spacing')
Still need help?
Contact Us

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