Skip to content

Instantly share code, notes, and snippets.

@chuangzhu
Last active April 8, 2020 03:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuangzhu/2f2b66a6b6d7cd6b9268289a1d6d5840 to your computer and use it in GitHub Desktop.
Save chuangzhu/2f2b66a6b6d7cd6b9268289a1d6d5840 to your computer and use it in GitHub Desktop.
Draw a __NEGATIVE BASED__ exponential function diagram using `matplotlib.mplot3d`.
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
SIZE = 10 # How big you'd like to draw
fig = plt.figure()
# OR USING `ax = fig.add_subplot(1, 1, 1, projection='3d')`
ax = Axes3D(fig)
# Draw coordinates
rge = np.arange(-SIZE/2, SIZE/2)
ax.plot([0]*SIZE, rge, [0]*SIZE, color='black', linestyle='--') # x
ax.plot(rge, [0]*SIZE, [0]*SIZE, color='black', linestyle='--') # yR
ax.plot([0]*SIZE, [0]*SIZE, rge, color='black', linestyle='--') # yI
# Numpy do not support dtype auto transform in complex power operate
X = np.arange(-SIZE/2, SIZE/2, 0.01, dtype='complex')
R = (-1)**X
Y = R.real
Z = R.imag
X = np.arange(-SIZE/2, SIZE/2, 0.01, dtype='float')
ax.plot(X, Y, Z, label='$f(x)=(-1)^x$')
X = np.arange(-SIZE/2, SIZE/2, 0.01, dtype='complex')
R = (-1.3)**X
Y = R.real
Z = R.imag
X = np.arange(-SIZE/2, SIZE/2, 0.01, dtype='float')
ax.plot(X, Y, Z, label='$f(x)=(-1.3)^x$')
ax.set_xlabel('x')
ax.set_ylabel('yR')
ax.set_zlabel('yI')
ax.legend()
plt.show()
fig.savefig('fx-1-x.svg')
@chuangzhu
Copy link
Author

How to draw the diagram of $ y=(-1)^x $

Consider only the cases when y is real values

fx-1-x-real svg

Consider when y is complex values

Display the dependent variable y on the side complex plane, display the independent variable x on the x coordinate. That is to say, draw a "Complex Plane of Y - X" diagram.

fx-1-x svg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment