thumbnail of a wide line plot of a sinc function
ax.plot(x, y, linewidth=5)
thumbnail of a pink line plot of a sinc function
ax.plot(x, y, color="pink")
thumbnail of a dashed line plot of a sinc function
ax.plot(x, y, linestyle="--")
thumbnail of multiple line plots of a sinc function
ax.plot(x1, y1)
ax.plot(x2, y2)
thumbnail of various cap styles and join styles
ax.plot(
    x, y,
    solid_capstyle="round",
    solid_joinstyle="bevel")

Make your lines your own

Occasionally you'll want a plot to show something other than a single solid thin blue line. Here are some ways you can tweak it.

First we need to set up.

import matplotlib
matplotlib.use("agg")
import matplotlib.pyplot as plt
import numpy as np

Select the "agg" backend and make our imports, as described here.

x = np.linspace(-6, 6, 500)
y = np.sinc(x)

Create a curve to work with.

fig = plt.figure()
ax = fig.gca()

Create a new Figure and get the Axes, like we did in this example. Now we're ready to go to work.

Change the width

ax.plot(x, y, linewidth=15)

Using the linewidth argument, you can set the width of your line, in points. For comparison, the default is 1.

wide curve plot of a sinc function

Change the color

ax.plot(x, y, color="pink", linewidth=5)

You can also specify the color using the color argument.

pink curve plot of a sinc function

There are several ways to specify colors, and covering all of them deserves its own tutorial, but the simplest way is to call out the (English) name of a color. Matplotlib can recognize a shocking variety of them. Here are some samples.

named CSS colors named colors from xkcd survey

Change the style

Another common manipulation is to change the style of a line from solid to dashed or dotted.

ax.plot(x, y, linewidth=2, linestyle="--")

The linestyle argument controls this.

dashed curve plot of a sinc function

You can choose from

or you can create your own if you have strong opinions about what it should look like.

Add more lines

Another common trick is to put more than one line on a plot.

ax.plot(x, y, linewidth=4)
ax.plot(x, y + .1, linewidth=2)
ax.plot(x, y + .2, linewidth=1)
ax.plot(x, y + .3, linewidth=.5)
ax.plot(x, y + .4, linewidth=.2)

Luckily, this is as easy as repeatedly calling plot(). You can do this as many times as you want. I've had thousands of lines on a single plot before. You have to wait a little while for everything to draw, but it gets there eventually.

multiple curve plots of a sinc function

Style line ends and joins

Occasionally you want to really get in and control exactly how your lines look. The styling of the tips of the lines (capstyle) and the bends in the lines (joinstyle) let you do this.

ax.plot(x, y, solid_capstyle="butt", solid_joinstyle="miter")
ax.plot(x, y, solid_capstyle="round", solid_joinstyle="round")
ax.plot(x, y, solid_capstyle="projecting", solid_joinstyle="bevel")

The solid_capstyle and solid_joinstyle options in the snippet above give the varying effects seen in this figure.

thumbnail of various cap styles and join styles

You can also make use of the dash_capstyle and dash_joinstyle with broken lines. They take the same sets of arguments.

and more!

There are lots of other fine details of lines you can control. If you're curious, check out the API.

Want even more control of your plot? Come take a look at the full set of tutorials.