Skip to main content
v2026.11,858 entries · CC-BY 4.0

Linewidth in Matplotlib: Set Line Thickness in plot()

How to set linewidth in Matplotlib: the keyword, the lw alias, set_linewidth(), rcParams and style sheets, the unit (points) and which setting wins.

Written and maintained by CASRAI Editorial Board

Last updated

In Matplotlib, linewidth (shortened to lw) sets how thick a line is drawn, and it is measured in points, not in data units. Pass it as a number to plt.plot(), for example plt.plot(x, y, linewidth=2), or set it for a whole session or style with the lines.linewidth rc parameter. This guide covers every documented way to set it, which one wins when several are set, and how to read the result.

What linewidth is used for in research figures

Line thickness is one of the few visual encodings you control directly in a figure: a heavier line for the fitted model, a lighter one for individual observations, thinner lines for reference or confidence curves. In Matplotlib, linewidth is a property of the Line2D object that plot() creates. The Line2D documentation lists it as linewidth or lw, type float, and the constructor signature carries linewidth=None, meaning the value falls back to the configured default unless you supply one.

Seaborn passes widths through its own parameters; see the table of related interfaces below, and the guides on seaborn line plots and scatter plots in Python for those plot types.

Install or access

Matplotlib is open-source and installed from the Python package index or conda. The official installation page gives the commands for pip, conda and other environments, and the quick-start imports the plotting interface as import matplotlib.pyplot as plt (see import matplotlib.pyplot as plt). The commands below follow the Matplotlib 3.11.2 documentation.

Set linewidth step by step

1. Set it for one line with the linewidth keyword

Pass linewidth to plot(). The keyword arguments of plot(x, y, fmt, *, data=None, **kwargs) are Line2D properties, so any property in the Line2D table is accepted:

import matplotlib.pyplot as plt

x = [0, 1, 2, 3]
y = [0, 1, 4, 9]
plt.plot(x, y, linewidth=2.5)
plt.show()

2. Use the lw shorthand

lw is the documented short name for the same property, so plt.plot(x, y, lw=2.5) gives an identical line.

3. Change an existing line with set_linewidth()

plot() returns a list of Line2D objects. Call set_linewidth(w) on one of them; the docs describe it as “Set the line width in points.” set_lw(w) is its alias.

(line,) = plt.plot(x, y)
line.set_linewidth(4)

4. Change several lines at once with plt.setp()

plt.setp(obj, **kwargs) sets properties on an artist or a list of artists, and the documentation uses it to make several lines thicker in one call:

lines = plt.plot(x, y, x, y[::-1])
plt.setp(lines, linewidth=3)

5. Change the default with rcParams

All rc settings live in matplotlib.rcParams, a dictionary-like variable that is global to the package. Set the default width for every line created afterwards:

import matplotlib as mpl
mpl.rcParams['lines.linewidth'] = 2

# several settings at once
mpl.rc('lines', linewidth=4, linestyle='-.')

matplotlib.rcdefaults() restores the defaults.

6. Change it temporarily with rc_context

matplotlib.rc_context modifies the defaults only inside a with block (it can also be used as a decorator), which is the safe choice when one figure needs a different width but the rest of a script should not change:

with mpl.rc_context({'lines.linewidth': 2, 'lines.linestyle': ':'}):
    plt.plot(x, y)

7. Set it in a style sheet

A style sheet or matplotlibrc file holds the same setting as a line such as lines.linewidth : 3. Load a style file with plt.style.use('./images/presentation.mplstyle'), or apply one temporarily with with plt.style.context('dark_background'):. To see which matplotlibrc file is active on your machine, call matplotlib.matplotlib_fname().

Which setting wins: precedence

An explicit linewidth= argument on a call sets that line directly, so it is the most specific setting. For the defaults, the customizing documentation states the order: “Setting rcParams at runtime takes precedence over style sheets, style sheets take precedence over matplotlibrc files.” In practice:

Scope How Lasts for
One line plot(..., linewidth=) or set_linewidth() That line
Block of code rc_context or plt.style.context The with block
Rest of the session rcParams['lines.linewidth'] or rc('lines', linewidth=) Until changed or rcdefaults()
Every session Style sheet or matplotlibrc file Whenever that file is loaded

Output and how to read it

The width is a float in points (“Line width, in points”, per set_linewidth). Because it is a physical unit rather than a data unit, a line keeps the same thickness when you change the axis limits or the data range. Two consequences: the same width in points can look different relative to the figure once you change the figure size, so check the exported figure at its final size; and a fractional value such as 1.5 is valid because the type is float.

When several series share a plot, the legend guide for Matplotlib shows how to label each series once its width and style are set.

Troubleshooting

The width does not change

Check whether a later call overrides it, for example set_linewidth() or an rc_context block that ended before the line was drawn.

Widths changed for the whole script unexpectedly

A runtime rcParams['lines.linewidth'] = ... assignment stays in effect until changed. Use rc_context for one-off changes, or call matplotlib.rcdefaults() to restore the defaults.

Seaborn ignores my linewidth

See the table below: seaborn’s lineplot maps width through size and sizes, and passes other keyword arguments down to Axes.plot().

Free alternatives and related interfaces

Tool How line width is set Source
Matplotlib pyplot.plot linewidth or lw, in points matplotlib.org, Line2D
seaborn lineplot size groups lines by width; sizes=(smallest, largest) sets the range of widths; other keyword arguments, such as lw=1 in its examples, are passed down to matplotlib.axes.Axes.plot() seaborn.pydata.org
pandas DataFrame.plot The pandas visualization guide describes plot() as a wrapper around plt.plot() pandas.pydata.org

Seaborn builds on Matplotlib, so the units and rc-parameter behavior above carry over; see seaborn line plots for the plot type itself.

FAQ

What is the difference between linewidth and lw in Matplotlib?

None. lw is the documented alias of linewidth, and set_lw() is the alias of set_linewidth().

What unit is matplotlib linewidth in?

Points. The set_linewidth documentation says “Line width, in points,” and the value is a float.

How do I set the default line width for all plots?

Set mpl.rcParams['lines.linewidth'] at runtime, call mpl.rc('lines', linewidth=...), or put lines.linewidth in a style sheet or matplotlibrc file. Runtime settings take precedence over style sheets, and style sheets over matplotlibrc.

How do I change the line width temporarily?

Use with mpl.rc_context({'lines.linewidth': 2}): around the plotting code, or with plt.style.context(...) for a style sheet.

Sources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Linewidth in Matplotlib: Set Line Thickness in plot()

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →

Ask CASRAI · Regulatory Radar

Research-admin question? Get an answer that links its sources.

An AI assistant specialized in research administration. Every answer links its sources to check before you act. 2 questions free, no account. $29/month after.

  • Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.
  • Every answer numbers its sources and links each one, so you can check the source yourself.