Chart styling
Matplotlib Basics
3 min read
Published Oct 5 2025
Guide Sections
Guide Comments
Overall concept
Matplotlib provides two main layers of styling control:
- Global style : Default look for all plots using
plt.style.use()orrcParams. - Figure/Axes-level style : Customisation for one figure or axes using methods like
plt.grid(),ax.set_facecolor(), etc.
You can think of it like CSS for charts — global themes + per-element overrides.
Global styles (plt.style)
Matplotlib comes with predefined style sheets that change colours, fonts, gridlines, backgrounds, etc.
List all available styles:
Temporarily apply a style:
Exiting the with block restores your default style.
Global customisation via rcParams
Matplotlib’s runtime configuration parameters (rcParams) let you set defaults globally:
You can also reset them:
Figure-level styling
The figure is the overall canvas, so you can control global visual aspects:
Example figure-level attributes:
figsize= Width × height in inches eg. (8, 6)facecolor= Background colour of canvas eg. 'white'edgecolor= Outline colour of canvas eg. 'black'dpi= Resolution (dots per inch) eg. dpi=120
Axes-level styling
Axes are the “plot area” inside the figure — you can adjust their background, borders, ticks, and gridlines:
Gridlines
You can add and style gridlines easily:
or at the axes level:
Styling options:
axis= 'x', 'y', 'both' eg. axis='x'color= Grid colour eg. 'lightgray'linestyle= Line pattern wg. '--', ':', '-.'linewidth= Thickness eg. 0.5alpha= Transparency eg. 0.7
Fonts, titles, and text
Global font settings:
Custom per-plot:
Add text annotation
plt.text() - Places text at a specific (x, y) coordinate in data space.
Parameters:
x,y: Position in the same coordinate system as your plot.'Your text': The string to display.fontsize: Font size.color(orc): Text colour.ha,va: Horizontal and vertical alignment ('left','center','right','top','bottom').
Example:

Add a horizontal line
plt.axhline() - Draws a horizontal line across the entire plot (at a specific y-value).
Parameters:
y: The y-coordinate where the line should appear.color(orc): Line colour (e.g.'red','k'for black).linestyle: Line pattern ('-','--',':','-.').linewidth(orlw): Thickness of the line.xmin,xmax: Optional range (0 to 1, fraction of x-axis range).
Example:

Add a vertical line
plt.axvline() - Draws a vertical line at a specific x-value.
Parameters:
x: The x-coordinate for the line.- Other options (
color,linestyle,linewidth,ymin,ymax) work just like inaxhline().
Example:

Tick styling
Tweak tick marks and labels:
Hide or customise tick labels:
Legends
Common legend options:
loc= 'upper left', 'lower right', etc.frameon= Show/hide borderfontsize= Legend text sizetitle= Add a title to the legendncol= Columns in legend layout
Creating your own style
For a consistent professional look across projects:
- Pick a base style (
plt.style.use('seaborn-v0_8')) - Customise with a few
rcParams - Save it as your own style file
Create your own style:
You can store .mplstyle files in your Matplotlib config folder.














