Matplotlib Plot

From NovaOrdis Knowledge Base
Revision as of 22:51, 7 October 2023 by Ovidiu (talk | contribs) (→‎Style)
Jump to navigation Jump to search

External

Internal

Overview

import matplotlib.pyplot as plt

plt.style.use('seaborn-v0_8-whitegrid')

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

fig, ax = plt.subplots()

ax.plot(x, y)

plt.show() # not needed in Jupyter

Difference between Plot and Scatter

Difference between Plot and Scatter

Input Data

plot([x], y, [fmt], ...)

where fmt is a format string. The coordinates of the points or line nodes are given by x, y.

Multiple Series

If multiple pairs of x/y arguments are provided, multiple lines are plotted:

def plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)

Style

To set globally, see:

Visualization Style

Format String

Color "r" for red.

Line Width, Color, Style, Marker

ax.plot(x, y, linewidth=0.5)
ax.plot(x, y, lw=0.5)
ax.plot(x, y, color='green', marker='o', linestyle='dashed', linewidth=2, markersize=12)

Multiple Series Formatting

Axes

Labels

ax.set_xlabel('length')
ax.set_ylabel('height')

Grid Lines

Title

fig, ax = plt.subplots()
fig.suptitle('Something')