Skip to Content

How do I change the line style in pyplot?

Pyplot is a Matplotlib module that provides a plotting interface similar to MATLAB. It allows you to create simple plots, histograms, scatter plots, bar charts, errorbars, etc. with just a few lines of code.

One common task when creating plots is customizing the style of the lines, including changing the line width, line type, dash patterns, and colors. Pyplot provides a few different ways to customize the line style.

Using Format Strings

The simplest way to change the line style is to use a format string. This is a string that describes the line style to use. For example:

'-'' solid line
'--' dashed line  
'-.' dash-dot line
':' dotted line

You can pass the format string to the ‘linestyle’ parameter when plotting:

import matplotlib.pyplot as plt

plt.plot([1, 2, 3], linestyle='--')

This will plot a dashed line. You can also abbreviate ‘linestyle’ to ‘ls’.

Some common format strings you can use are:

Format String Description
‘-‘ Solid line
‘–‘ Dashed line
‘-.’ Dash-dot line
‘:’ Dotted line

You can combine line styles by concatenating the format strings. For example, ‘-.’ will give a dash-dot line.

Using Linestyle Objects

For more flexibility in controlling the line style, you can use Linestyle objects. These allow you to precisely specify the on/off pattern for dashes.

To create a Linestyle, import matplotlib.lines and instantiate a Linestyle object:

from matplotlib import lines

solid = lines.Line2D.solid
dashed = lines.Line2D.dashed

Then pass this object to the ‘linestyle’ parameter:

plt.plot([1,2,3], linestyle=dashed)

Some common Linestyle objects available are:

Object Description
solid Solid line (default)
dashed Dashed line
dashdot Dash-dot line
dotted Dotted line

You can also define custom dash patterns by providing a sequence of on/off lengths in points:

import matplotlib.lines as mlines

custom = mlines.Line2D([10,5,2,5], linewidth=2) 

This would create a line style with 10pt dash, 5pt gap, 2pt dash, 5pt gap. Pass this object to ‘linestyle’ to use it.

Changing Line Width

In addition to the line style, you can also change the width of the line. This is done using the ‘linewidth’ parameter:

plt.plot([1,2,3], linewidth=5)

The width is specified in points. The default linewidth is 1.0.

Plot Line Colors

You can change the color of lines using the ‘color’ parameter:

plt.plot([1,2,3], color='red')

This will plot a red line. You can use any matplotlib color spec here, including RGB tuples, hex codes, or HTML color names.

Some examples:

# Red 
plt.plot([1,2,3], color='red')

# Hex code
plt.plot([1,2,3], color='#FF0000') 

# RGB tuple
plt.plot([1,2,3], color=(1.0, 0, 0))

# Cyan  
plt.plot([1,2,3], color='cyan')

Shorthand Syntax

You can also specify the line style, width, and color using a shorthand syntax by concatenating them together with periods:

fmt = '[linestyle].[linewidth].[color]'

plt.plot([1,2,3], 'r--')

This sets a red dashed line with default width. The style, width, and color parts are all optional, so you can mix and match elements.

Setting Default Line Styles

If you want to set line styles for all plots, you can customize the matplotlib rcParams. For example:

plt.rcParams['lines.linestyle'] = '--'
plt.rcParams['lines.linewidth'] = 2
plt.rcParams['lines.color'] = 'blue'

This will set dashed blue lines with width 2 as the default for all plots.

Per-Dataset Line Styles

When plotting multiple sets of data on the same axes, you can specify per-dataset line styles to distinguish them. Do this by passing line style arguments to each call to plt.plot():

import matplotlib.pyplot as plt

xs = [1, 2, 3, 4]
ys1 = [1, 2, 3, 4] 
ys2 = [2, 4, 6, 8]

plt.plot(xs, ys1, linestyle='--', color='b')
plt.plot(xs, ys2, linestyle=':', color='r')

plt.show()

This will plot ys1 as a blue dashed line and ys2 as a red dotted line.

Line Styles in Matplotlib

In summary, to change line styles in matplotlib plots, you can use:

  • Format strings like ‘-‘, ‘–‘, ‘-.’, ‘:’
  • Linestyle objects like lines.Line2D.solid
  • The linewidth parameter to set width
  • The color parameter to set color
  • The shorthand fmt syntax for style, width, and color
  • rcParams settings for default styles
  • Per-dataset parameters in plt.plot() for multi-line plots

This provides a lot of flexibility for customizing plot lines to suit your needs. Proper use of line styles can make your plots clearer and more visually effective.

Example Plots with Different Line Styles

Here are some examples of plots using the various line style options:

Solid and Dashed Lines

import matplotlib.pyplot as plt
import matplotlib.lines as mlines

plt.plot([1, 2, 3], linestyle='-') 
plt.plot([1, 2, 3], linestyle='--')

plt.show() 

This plots a solid line and dashed line using format strings.

Custom Dash Pattern

custom = mlines.Line2D([10,5,2,5], linewidth=2)
plt.plot([1,2,3], linestyle=custom)

plt.show()

Here a custom linestyle object defines the on/off dash pattern.

Varying Line Width

import matplotlib.pyplot as plt

plt.plot([1,2,3], linewidth=1)
plt.plot([1,2,3], linewidth=5)

plt.show()

This sets different linewidths for each line.

Different Colors

import matplotlib.pyplot as plt

plt.plot([1,2,3], color='red') 
plt.plot([1,2,3], color='#00FF00')

plt.show()

Here red and green colors are used.

Shorthand Syntax

import matplotlib.pyplot as plt

plt.plot([1,2,3], 'r-.')
plt.plot([1,2,3], 'b--') 

plt.show()

The shorthand syntax defines both style and color concisely.

Multi-Dataset Plot

import matplotlib.pyplot as plt

xs = [1, 2, 3, 4]
ys1 = [1, 2, 3, 4]
ys2 = [2, 4, 6, 8]

plt.plot(xs, ys1, 'r--')
plt.plot(xs, ys2, 'b:')

plt.show()

Here each line has distinct styles since they represent different data sets.

Conclusion

In matplotlib, lines are very customizable through format strings, linestyle objects, linewidth, color, shorthand syntax, and per-dataset parameters. This allows full control over the visual representation of lines in your plots. Proper use of line styles is key for creating publication-quality plots that effectively showcase your data.