Matplotlib Plot: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 24: Line 24:
{{Internal|Matplotlib#Difference_between_Plot_and_Scatter|Difference between Plot and Scatter}}
{{Internal|Matplotlib#Difference_between_Plot_and_Scatter|Difference between Plot and Scatter}}
=Input Data=
=Input Data=
The coordinates of the points or line nodes are given by the <code>x</code> and <code>y</code> lists, which must have the same size, otherwise a runtime error occurs.
<syntaxhighlight lang='py'>
<syntaxhighlight lang='py'>
plot([x], y, [fmt], ...)
plot([x], y, [fmt], ...)
</syntaxhighlight>
</syntaxhighlight>
where <code>fmt</code> is a [[#Format_String|format string]].
<code>fmt</code> is a [[#Format_String|format string]].  
The coordinates of the points or line nodes are given by <code>x</code>, <code>y</code>.
==Multiple Series==
==Multiple Series==
If multiple pairs of <code>x</code>/<code>y</code> arguments are provided, multiple lines are plotted:
If multiple pairs of <code>x</code>/<code>y</code> arguments are provided, multiple lines are plotted:

Revision as of 23:04, 7 October 2023

External

Internal

Overview

import matplotlib.pyplot as plt
plt.style.use('seaborn-v0_8-whitegrid')
x = [1, 2, 3]
y = [10, 23, 4]
x2 = [1, 2, 3]
y2 = [11, 25, 6]
fig, ax = plt.subplots()
fig.suptitle('Something')
ax.set_xlabel('length')
ax.set_ylabel('height')
ax.plot(x, y, 'g', x2, y2, 'b', lw=0.5)
plt.show() # not needed in Jupyter

Difference between Plot and Scatter

Difference between Plot and Scatter

Input Data

The coordinates of the points or line nodes are given by the x and y lists, which must have the same size, otherwise a runtime error occurs.

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

fmt is a format string.

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

Use style properties:

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)

The fmt argument, which is a convenient way for defining basic formatting like color, marker and lifestyle, can be mixed with style properties.

ax.plot(x, y, 'g', marker='o', linestyle='dashed', linewidth=2, markersize=12)

Multiple Series Formatting

# first line green, second blue
ax.plot(x, y, 'g', x2, y2, 'b', lw=0.5)

Axes

Labels

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

Grid Lines

Title

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