Show Sidebar Hide Sidebar

Setting the Title, Legend Entries, and Axis Titles in matplotlib

How to set the title, axis-titles, and axis limits in matplotlib and plotly.

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.array(range(1,13))
y = np.random.randint(100, size=x.shape)

plt.plot(x,y)
plt.title('The title with font size: 20, and font:monospace')
plt.tight_layout()

fig = plt.gcf()
plotly_fig = tls.mpl_to_plotly( fig )

plotly_fig['layout']['titlefont'].update({'color':'blue', 'size':20, 'family':'monospace'})

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

import plotly.plotly as py

# 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)

# Generating Data..
t = np.arange(-5.0, 5.0, 0.01)
s = np.sin(4 * np.pi * np.absolute(t)) * np.exp(-5 * np.absolute(t))/2

line, = ax.plot(t, s, lw=2)

plotly_fig = tls.mpl_to_plotly( mpl_fig )

plotly_fig['layout']['annotations'] = []
plotly_fig['layout']['annotations'].append({ 'text': 'Setting the Custom Title Position', 
                                             'ax':-42, 'ay': 148,  'showarrow':False, 
                                             'x': 0, 'y': -0.2, 'font': {'size':16 }
                                            })

plotly_fig['layout']['yaxis1'].update({'range': [-0.3,0.3]})
plotly_url = py.plot(plotly_fig, file_name='mpl-custom-title-position')
import matplotlib.pyplot as plt
import numpy as np

import plotly.plotly as py

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


fig = plt.figure()
x = np.linspace(np.pi, 2*np.pi, 1000)

ax1 = fig.add_subplot(221)
y = np.sin(x)
ax1.plot(x,y)
ax1.set_title('Sine Curve')


ax2 = fig.add_subplot(222)
y = np.cos(x)
ax2.plot(x,y)
ax2.set_title('Cosine Curve')


ax3 = fig.add_subplot(223)
y = np.sin(x)
ax3.plot(x,y)
ax3.set_title('Tangent Curve')


ax4 = fig.add_subplot(224)
y = np.log(x)
ax4.plot(x,y)
ax4.set_title('Logarithmic Curve')

fig.tight_layout()

plotly_fig = tls.mpl_to_plotly( fig )
plotly_url = 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 = plt.subplot(111)

x = np.arange(10)

for i in range(1, 4):
    ax.plot(x, i * x**2, label='Group %d' % i)

ax.set_title('Plot with custom legend style')
plotly_fig = tls.mpl_to_plotly( fig )

# Adding custom attributes to legend
plotly_fig['layout']['showlegend'] = True
plotly_fig['layout']['legend'] = {}
plotly_fig['layout']['legend'].update({'x':1.5, 'y':0.5, 'borderwidth':1, 'bgcolor':'rgb(217,217,217)'})

plot_url = py.plot(plotly_fig, filename='mpl-custom-legend')
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(111)
ax.yaxis.tick_right()
plt.plot([2,3,4,5])


plotly_fig = tls.mpl_to_plotly(fig)
plotly_url = py.plot(plotly_fig)
Inspired from Stack Overflows
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


plt.title('A Vertical Line')
plt.xlim([-5,5])
plt.ylim([-5,5])

fig = plt.gcf()
plotly_fig = tls.mpl_to_plotly( fig )

plotly_fig['layout']['shapes'] = list()
plotly_fig['layout']['shapes'].append( dict(type='line', x0=1, y0=0, x1=1, y1=2))

plotly_fig['data'] = []
plotly_fig['data'].append({'x':2,'y':1,'type':'scatter','mode':'text', 'text':'A Vertical Line'})


plotly_url = py.plot(plotly_fig, filename='mpl-vertical-line')
Still need help?
Contact Us

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