-
Created by Unknown User (jonakaa), last modified on 13.06.2019
You are viewing an old version of this page. View the current version.
Compare with Current
View Page History
« Previous
Version 41
Next »
Simple plot
import numpy as np
import matplotlib.pyplot as plt
# Evenly sampled time from 0s to 10s at 200ms intervals
t = np.arange(0.0, 10.0, 0.2)
# Plotting t at x-axis and sin(t) at y-axis
plt.plot(t, np.sin(t))
# Naming the title and both axis
plt.title('Sinus function')
plt.ylabel('sin(t)')
plt.xlabel('t [s]')
# Need to call the show() function at the end to display my figure
plt.show()
![](/wiki/download/attachments/146605768/Simple%20plot.png?version=1&modificationDate=1560334640000&api=v2)
import numpy as np
import matplotlib.pyplot as plt
# Evenly sampled time at 200ms intervals
t = np.arange(0.0, 5.0, 0.2)
# plot() can plot several lines in the same figure. To seperate the different lines
# from eachother, we may change the line style and format strings.
# See the plot() documentation for a complete list of line styles and format strings.
# The following lines have red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', label='Linear line')
plt.plot(t, t**2, color='blue', linestyle='none', marker='s', label='Second degree polynom')
plt.plot(t, t**3, 'g^', label='Third degree polynom')
# To describe our plot even more detailed we can draw the labels we previously gave our lines using legend.
plt.legend(loc='upper left')
# The function axis() sets the axis sizes, and takes the argument [xmin, xmax, ymin, ymax]
plt.axis([0, 5, 0, 100])
plt.title('Mulitple polynoms')
plt.show()
![](/wiki/download/attachments/146605768/Several%20simple%20plots%20in%20same%20figure.png?version=2&modificationDate=1560337523000&api=v2)
import matplotlib.pyplot as plt
import numpy as np
# Some example data to display
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
A single plot
subplots() without arguments return a Figure and a single Axes. When dealing with multiple plots in the same figure, the different axes will seperate the different plots from eachother within the figure.
fig, ax = plt.subplots()
fig.suptitle('A single plot')
ax.plot(x, y)
Stacking subplots in one direction
The first two optional arguments of pyplot.subplots() define the number of rows and columns of the subplot grid.
When stacking in one direction only, the returned axs is a 1D numpy array containing the list of created Axes.
fig, axs = plt.subplots(2)
fig.suptitle('Vertically stacked subplots')
axs[0].plot(x, y)
axs[1].plot(x, -y)
If you are creating just a few Axes, it's handy to unpack them immediately to dedicated variables for each Axes. That way, we can use ax1 instead of the more verbose axs[0].
fig, (ax1, ax2) = plt.subplots(2)
fig.suptitle('Vertically stacked subplots')
ax1.plot(x, y)
ax2.plot(x, -y)
To obtain side-by-side subplots, pass parameters 1, 2 for one row and two columns.
fig, (ax1, ax2) = plt.subplots(1, 2)
fig.suptitle('Horizontally stacked subplots')
ax1.plot(x, y)
ax2.plot(x, -y)
![](/wiki/download/attachments/146605768/A%20single%20plot.png?version=2&modificationDate=1560425737000&api=v2)
![](/wiki/download/attachments/146605768/Vertically%20stacked%20subplots.png?version=1&modificationDate=1560424989000&api=v2)
![](/wiki/download/attachments/146605768/Horisontally%20stacked%20subplots.png?version=1&modificationDate=1560425426000&api=v2)
Quiver plot
import numpy as np
import matplotlib.pyplot as plt
# X and Y define the arrow locations
X, Y = np.meshgrid(np.arange(0, 2 * np.pi, .2), np.arange(0, 2 * np.pi, .2))
# U and V define the arrow directions, respectively in x- and y-direction
U = np.cos(X)
V = np.sin(Y)
# Call signature: quiver([X, Y], U, V, [C]), where C optionally sets the color
plt.quiver(X, Y, U, V)
plt.title('Simple quiver plot')
plt.show()
![](/wiki/download/attachments/146605768/Simple%20quiver%20plot.png?version=2&modificationDate=1560344802000&api=v2)
import numpy as np
import matplotlib.pyplot as plt
# X and Y define the arrow locations
# This setup gives us 10 arrows in width and height, as our interval is from -5 to 5 with step 1
X = np.arange(-5, 5, 1)
Y = np.arange(-5, 5, 1)
# U and V define the arrow directions, respectively in x- and y-direction
U, V = np.meshgrid(3*X, 3*Y)
plt.figure()
plt.subplot(121)
plt.quiver(X, Y, U, V)
plt.title('Only autoscaling')
plt.subplot(122)
plt.quiver(X, Y, U, V)
# Here we specify the axes. How much extra space you need depends on the arrow size and direction,
# and must therefore be adapted each time
plt.axis([-6.5, 5.5, -6.5, 5.5])
plt.title('Manually set axes')
plt.show()
![](/wiki/download/attachments/146605768/Quiver%20autscaling%20vs%20manually%20axes.png?version=1&modificationDate=1560345986000&api=v2)