Skip to content

Instantly share code, notes, and snippets.

@watermouth
Last active March 3, 2018 12:47
Show Gist options
  • Select an option

  • Save watermouth/33cf8c9a12ebd9026139e1bc12f58861 to your computer and use it in GitHub Desktop.

Select an option

Save watermouth/33cf8c9a12ebd9026139e1bc12f58861 to your computer and use it in GitHub Desktop.
  • create meshgrid data and plot contour
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

x = np.linspace(-np.pi, np.pi, 360) # linearly spaced numbers
y = np.linspace(-1,1,100) # linearly spaced numbers
xx, yy = np.meshgrid(x, y)
# input values for each axis, then it returns values for each axis of mesh grid data points
# ex. [1,2], [3,4] => grid points are (1,3), (2,3), (1,4), (2,4), so it returns [array([[1,2],[1,2]]),array([[3,3],[4,4]])]
zz = np.sin(xx) + yy
plt.plot([-np.pi, np.pi],[0,0], "k-")
plt.plot([0,0],[-1,1], "k-")
plt.contour(xx, yy, zz, cmap=matplotlib.cm.brg)
#plt.pcolor(xx, yy, zz, cmap=matplotlib.cm.brg) # replace contour with pcolor if you like
plt.colorbar()
plt.show()
  • you can fill with contourf:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

x = np.linspace(-np.pi, np.pi, 360) # linearly spaced numbers
y = np.linspace(-1,1,100) # linearly spaced numbers
xx, yy = np.meshgrid(x, y)
zz = np.select([xx+yy < 0, xx+yy > 1], [1,2]) # np.select(conodition, values)
print(zz)

from matplotlib.colors import ListedColormap
custom_cmap = ListedColormap(['#fafab0','#9898ff','#a0faa0'])
plt.contourf(xx, yy, zz, cmap=custom_cmap, linewidth=5)
plt.colorbar()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment