Matplotlib Plot: Difference between revisions
(12 intermediate revisions by the same user not shown) | |||
Line 9: | Line 9: | ||
import matplotlib.pyplot as plt | import matplotlib.pyplot as plt | ||
plt.style.use('seaborn-v0_8-whitegrid') | plt.style.use('seaborn-v0_8-whitegrid') | ||
fig, ax = plt.subplots() # this returns a Figure instance and one or an array of matplotlib.axes.Axes instances | |||
fig.suptitle('Something') | |||
ax.set_xlabel('length') | |||
ax.set_ylabel('height') | |||
x = [1, 2, 3] | x = [1, 2, 3] | ||
y = [10, 23, 4] | y = [10, 23, 4] | ||
x2 = [1, 2, 3] | x2 = [1, 2, 3] | ||
y2 = [11, 25, 6] | y2 = [11, 25, 6] | ||
ax.plot(x, y, 'g', x2, y2, 'b', lw=0.5, color='indigo') | |||
ax.plot(x, y, 'g', x2, y2, 'b', lw=0.5) | |||
plt.show() # not needed in Jupyter | plt.show() # not needed in Jupyter | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 24: | Line 26: | ||
{{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> | ||
<code>fmt</code> is a [[#Format_String|format string]]. | |||
==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: | ||
Line 50: | Line 52: | ||
ax.plot(x, y, 'g', marker='o', linestyle='dashed', linewidth=2, markersize=12) | ax.plot(x, y, 'g', marker='o', linestyle='dashed', linewidth=2, markersize=12) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
===Marker=== | |||
'o' (dots), '*' (stars), 's' (square). | |||
===Multiple Series Formatting=== | ===Multiple Series Formatting=== | ||
The series are specified as array pairs: | |||
<syntaxhighlight lang='py'> | <syntaxhighlight lang='py'> | ||
# first line green, second blue | # first line green, second blue | ||
ax.plot(x, y, 'g', x2, y2, 'b', lw=0.5) | ax.plot(x, y, 'g', x2, y2, 'b', lw=0.5) | ||
</syntaxhighlight> | |||
The series are Pandas [[Pandas_Series|Series]]: | |||
<syntaxhighlight lang='py'> | |||
s = ... | |||
s2 = ... | |||
ax.plot(s) | |||
ax.plot(s2) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
==Axes== | ==Axes== | ||
If some axis attribute needs to be set, after it was created, the x axis and the y axis of the <code>matplotlib.axes.Axes</code> object can be accessed with the <code>ax.xaxis</code> and <code>ax.yaxis</code>: | |||
<syntaxhighlight lang='py'> | |||
import matplotlib as mp | |||
ax.xaxis.set_minor_locator(mp.ticker.AutoMinorLocator()) | |||
</syntaxhighlight> | |||
===Labels=== | ===Labels=== | ||
<syntaxhighlight lang='py'> | <syntaxhighlight lang='py'> | ||
ax.set_xlabel('length') | ax.set_xlabel('length') | ||
ax.set_ylabel('height') | ax.set_ylabel('height') | ||
</syntaxhighlight> | |||
When the date labels overlap on the horizontal axis, use this: | |||
<syntaxhighlight lang='py'> | |||
fig.autofmt_xdate() | |||
</syntaxhighlight> | |||
To format individual values corresponding to minor and major tickers: | |||
<syntaxhighlight lang='py'> | |||
import matplotlib.ticker as pt | |||
... | |||
ax.xaxis.set_major_formatter(pt.FormatStrFormatter('% 1.2f')) | |||
ax.xaxis.set_minor_formatter(pt.FormatStrFormatter('% 1.2f')) | |||
ax.yaxis.set_major_formatter(pt.FormatStrFormatter('% 1.2f')) | |||
ax.yaxis.set_minor_formatter(pt.FormatStrFormatter('% 1.2f')) | |||
</syntaxhighlight> | |||
To format percentages: | |||
<syntaxhighlight lang='py'> | |||
ax.xaxis.set_major_formatter(pt.PercentFormatter()) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 69: | Line 106: | ||
fig.suptitle('Something') | fig.suptitle('Something') | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=Using a Plot with Pandas Series= | |||
{{Internal|Using a Matplotlib Plot with Pandas Series|Using a Plot with Pandas Series}} |
Latest revision as of 03:52, 21 October 2023
External
- Example https://matplotlib.org/stable/plot_types/basic/plot.html#sphx-glr-plot-types-basic-plot-py
- Documentation https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.plot.html#matplotlib.axes.Axes.plot
Internal
Overview
import matplotlib.pyplot as plt
plt.style.use('seaborn-v0_8-whitegrid')
fig, ax = plt.subplots() # this returns a Figure instance and one or an array of matplotlib.axes.Axes instances
fig.suptitle('Something')
ax.set_xlabel('length')
ax.set_ylabel('height')
x = [1, 2, 3]
y = [10, 23, 4]
x2 = [1, 2, 3]
y2 = [11, 25, 6]
ax.plot(x, y, 'g', x2, y2, 'b', lw=0.5, color='indigo')
plt.show() # not needed in Jupyter
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:
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)
Marker
'o' (dots), '*' (stars), 's' (square).
Multiple Series Formatting
The series are specified as array pairs:
# first line green, second blue
ax.plot(x, y, 'g', x2, y2, 'b', lw=0.5)
The series are Pandas Series:
s = ...
s2 = ...
ax.plot(s)
ax.plot(s2)
Axes
If some axis attribute needs to be set, after it was created, the x axis and the y axis of the matplotlib.axes.Axes
object can be accessed with the ax.xaxis
and ax.yaxis
:
import matplotlib as mp
ax.xaxis.set_minor_locator(mp.ticker.AutoMinorLocator())
Labels
ax.set_xlabel('length')
ax.set_ylabel('height')
When the date labels overlap on the horizontal axis, use this:
fig.autofmt_xdate()
To format individual values corresponding to minor and major tickers:
import matplotlib.ticker as pt
...
ax.xaxis.set_major_formatter(pt.FormatStrFormatter('% 1.2f'))
ax.xaxis.set_minor_formatter(pt.FormatStrFormatter('% 1.2f'))
ax.yaxis.set_major_formatter(pt.FormatStrFormatter('% 1.2f'))
ax.yaxis.set_minor_formatter(pt.FormatStrFormatter('% 1.2f'))
To format percentages:
ax.xaxis.set_major_formatter(pt.PercentFormatter())
Grid Lines
Title
fig, ax = plt.subplots()
fig.suptitle('Something')