Styling Matplotlib Plots

matplotlib_plot_styling

Skill - Basic styling of Matplotlib plots

Table of Contents

Skills Required

Please make sure to have all the skills mentioned above to understand and execute the code mentioned below. Go through the above skills if necessary for reference or revision


Matplotlib is a plotting library in the scipy ecosystem of libraries.

In this post we will try to understand how to control the styling of a Matplotlib plot

Please make sure that you covered the post on basics

Color

use color parameter in plot function. color can be a known color like 'red' string or a hex color like '#0F0F0F'. You can use online color pickers like this to generate hex codes from colors.

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [1, 4, 2, 3]

# create a new figure and get figure, axes handle in return
fig, ax = plt.subplots()

# using color keyword in plot function to control color
ax.plot(x,y,color='magenta')

# print figure
plt.show()

plot color demo

Line style

linestyle can be 'solid', 'dashed', 'dashdot', 'dotted', 'None'

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [1, 4, 2, 3]

# create a new figure and get figure, axes handle in return
fig, ax = plt.subplots()

# using linestyle keyword in plot function
ax.plot(x,y,linestyle='dashed')

# print figure
plt.show()

plot line style demo

Markers

marker parameter can be used to control the marker style. For example, here we are using marker='p' to use pentagon marker.
Marker strings like the one used above can be found here
Other marker styling options are

markeredgecolor / mec - control the color of marker edge / outline

markeredgewidth / mew - a number that can control the marker outline/edge width

markerfacecolor / mfc - control the color of marker filling inside the outline

markersize / ms - a number that can control the size of the marker
import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [1, 4, 2, 3]

# create a new figure and get figure, axes handle in return
fig, ax = plt.subplots()

# using marker, ms, mfc, mec, mew keywords in plot function to control marker appearance
ax.plot(x,y,marker='p', ms=15, mfc='magenta', mec='yellow', mew=3)

# print figure
plt.show()

plot marker demo

Styling plots using format string

the plot function accepts a format string as the 3rd input for specifying the line style in a single string.
ax.plot([<x_vals>], [<y_vals>], <fmt>)

fmt string should in the format '[marker][line][color]'
matplotlib line style formats
Example formats are shown below

'b'    # blue markers with default shape
'or'   # red circles
'-g'   # green solid line
'--'   # dashed line with default color
'^k:'  # black triangle_up markers connected by a dotted line

An example of styling a plot using format string can be seen below

import matplotlib.pyplot as plt

# the lists of x and y coordinates
x = [1, 2, 3, 4]
y = [1, 4, 2, 3]

# create a figure and axes handle using 'subplots' function
fig, ax = plt.subplots()

# use axes handle to plot xy data and get the plot artist in return
la, = ax.plot(x, y, '*-r')
# In the format string, * means marker style, - means normal line, r means red color

# print the plot
plt.show()

plot with format string
For more styling options an in depth understanding of styling please check out the api documentation page

Video

You can the video on this post here


Online Interpreter

Although we recommend to practice the above examples in Visual Studio Code, you can run these examples online at https://pynative.com/online-python-code-editor-to-execute-python-code/

Color example

Line style example

Markers example

Format string example

References


Table of Contents

Comments