Show Sidebar Hide Sidebar

Lines and Markers in matplotlib

Lines and markers options in matplotlib. Examples of different types of lines and markers available 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



color = 'cornflowerblue'
points = np.ones(5)  # Draw 5 points for each line
text_style = dict(horizontalalignment='right', verticalalignment='center',
                  fontsize=12, fontdict={'family': 'monospace'})


def format_axes(ax):
    ax.margins(0.2)
    ax.set_axis_off()


def nice_repr(text):
    return repr(text).lstrip('u')


# Plot all line styles.
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)

linestyles = ['-', '--', '-.', ':']
y1 = 1
ax1.plot(y1 * points, linestyle='-', color=color, linewidth=3)
ax1.set_title('Line Type -')

y2=2
ax2.plot(y2 * points, linestyle='--', color=color, linewidth=3)
ax2.set_title('Line Type --')

y3=3
ax3.plot(y3* points, linestyle='-.', color=color, linewidth=3)
ax3.set_xlabel('Line Type -.')

y4=4
ax4.plot(y4 * points, linestyle=':', color=color, linewidth=3)
ax4.set_xlabel('Line Type :')

plotly_fig = tls.mpl_to_plotly( fig )
plot_url = py.plot(plotly_fig, filename='mpl-line-styles')
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

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.linspace(-5,5,1000)
sinx = np.sin(x)
cosx = np.cos(x)
logx = np.log(x)

df = pd.DataFrame({'x':x, 'sinx':sinx, 'cosx':cosx, 'logx':logx})

ax = df.plot(kind='line', style=['-', '--', '-.'], legend=False)
ax.set_title('Setting Line Styles Per column in Matplotlib and pandas')
fig = ax.get_figure()

plotly_fig = tls.mpl_to_plotly( fig )
plotly_fig['layout']['showlegend'] = True
plotly_url = py.plot(plotly_fig, filename='mpl-linestyles-per-column')
Inspired from Pandas 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

from itertools import cycle

lines = ["-","--","-.",":"]
linecycler = cycle(lines)
fig = plt.figure()
ax = fig.add_subplot(111)
for i in range(10):
    x = range(i,i+10)
    ax.plot(range(10),x,next(linecycler))

ax.set_title('Cycling through line styles in matplotlib')

plotly_fig = tls.mpl_to_plotly( fig )
plot_url = py.plot(plotly_fig, filename='mpl-linestyles-cycle')
Inspired from 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

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

x = [0,2,4,6,8,10]
y = [0]*len(x)
s = [100,  400, 490, 600, 240, 160] # Specifies marker size

ax.scatter(x,y,s=s)
ax.set_title('Plot with Different Marker size, matplotlib and plotly')

plotly_fig = tls.mpl_to_plotly( fig )
plotly_fig['layout']['showlegend'] = True
plotly_url = py.plot(plotly_fig, filename='mpl-marker-size')
Inspired from 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

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

x = [0,2,4,6,8,10]
y = [0]*len(x)
c = 'gbrcyk'

ax.scatter(x,y,s=400,c=c)
ax.set_title('Plot with Different Marker Color, matplotlib and plotly')

plotly_fig = tls.mpl_to_plotly( fig )
plotly_url = py.plot(plotly_fig, filename='mpl-marker-color')
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)

x = [0,2,4,6,8,10]
y = [1]*len(x)
marker_edge_color = 'rgbcyk'

for i in range(6):
    ax = fig.add_subplot(2,3,i+1)
    ax.plot(x,y,marker='o', markersize=20, markerfacecolor='lightgrey',markeredgecolor=marker_edge_color[i])
    ax.set_title('Edge Color: %s'%marker_edge_color[i])

fig.tight_layout()
plotly_fig = tls.mpl_to_plotly( fig )
plotly_url = py.plot(plotly_fig, filename='mpl-marker-color')
Still need help?
Contact Us

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