matplotlib code snippets

back

Classic two axis graph with origin at 0,0

import matplotlib.pyplot as plt
import numpy as np

N = 8
k = 1
n = np.arange(0, N)
s = np.exp(1j * 2 * np.pi * k * n / N)

fig, ax = plt.subplots()

# Move the left and bottom spines to x = 0 and y = 0, respectively.
ax.spines["left"].set_position(("data", 0))
ax.spines["bottom"].set_position(("data", 0))
# Hide the top and right spines.
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)

ax.plot(1, 0, ">k", transform=ax.get_yaxis_transform(), clip_on=False)
ax.plot(0, 1, "^k", transform=ax.get_xaxis_transform(), clip_on=False)
ax.set_xlabel('real', x=1)
ax.set_ylabel('imag', y=1, rotation=0)

ax.plot(np.real(s), np.imag(s), '-o')
margin = 0.25
ax.axis([-1-margin, 1+margin, -1-margin, 1+margin])

# plt.grid(True)
plt.show()