Matplotlib Example¶
Quiver and Contour
In [1]:
Copied!
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sns
from mpl_toolkits.basemap import Basemap
from matplotlib.colors import LinearSegmentedColormap
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sns
from mpl_toolkits.basemap import Basemap
from matplotlib.colors import LinearSegmentedColormap
Define colormap
In [2]:
Copied!
pallete_name = "husl"
colors = sns.color_palette(pallete_name, 8)
pallete_name = "husl"
colors = sns.color_palette(pallete_name, 8)
Define grid
In [3]:
Copied!
colors.reverse()
cmap = mpl.colors.LinearSegmentedColormap.from_list(pallete_name, colors)
(y, x) = np.mgrid[-2:2.1:0.2, -2:2.2:0.2]
colors.reverse()
cmap = mpl.colors.LinearSegmentedColormap.from_list(pallete_name, colors)
(y, x) = np.mgrid[-2:2.1:0.2, -2:2.2:0.2]
Compute values
In [4]:
Copied!
z = x * np.exp(-x**2 - y**2)
(dx, dy) = np.gradient(z)
z = x * np.exp(-x**2 - y**2)
(dx, dy) = np.gradient(z)
In [5]:
Copied!
plt.title("Quiver and Contour", fontsize=20)
plt.quiver(x, y, dx, dy, z, cmap=cmap)#箭頭
plt.contour(x, y, z, 10, cmap=cmap)#輪廓
fig = plt.gcf()
plt.title("Quiver and Contour", fontsize=20)
plt.quiver(x, y, dx, dy, z, cmap=cmap)#箭頭
plt.contour(x, y, z, 10, cmap=cmap)#輪廓
fig = plt.gcf()
Vertical and Horizontal Structure of Typhoon
In [6]:
Copied!
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
def z_func(x, y):
return ( (-20) * np.exp((-0.2) * ((x**2 + y**2) * 0.5)**(0.5)) ) - \
np.exp( (0.5) * (np.cos(2*np.pi*x) + np.cos(2*np.pi*y) ) ) + \
np.exp(1) + 20
x = y = np.arange(-6, 6, 0.1)
X, Y = np.meshgrid(x, y)
Z = z_func(X, Y)
ec = ax.plot_surface(X, Y, Z, cmap='coolwarm')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z', fontsize=10, rotation=0)
ax.set_zlim(0, 15)
cbar = plt.colorbar(ec, orientation='horizontal', shrink=0.5)
cbar.set_label('Z')
plt.title('Vertical and Horizontal Structure of Typhoon')
plt.legend(scatterpoints=1, frameon=False, labelspacing=1,)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
def z_func(x, y):
return ( (-20) * np.exp((-0.2) * ((x**2 + y**2) * 0.5)**(0.5)) ) - \
np.exp( (0.5) * (np.cos(2*np.pi*x) + np.cos(2*np.pi*y) ) ) + \
np.exp(1) + 20
x = y = np.arange(-6, 6, 0.1)
X, Y = np.meshgrid(x, y)
Z = z_func(X, Y)
ec = ax.plot_surface(X, Y, Z, cmap='coolwarm')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z', fontsize=10, rotation=0)
ax.set_zlim(0, 15)
cbar = plt.colorbar(ec, orientation='horizontal', shrink=0.5)
cbar.set_label('Z')
plt.title('Vertical and Horizontal Structure of Typhoon')
plt.legend(scatterpoints=1, frameon=False, labelspacing=1,)
plt.show()
No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
In [7]:
Copied!
fig = plt.figure(figsize=(6, 6))
CS = plt.contour(X, Y, Z, 20, cmap='coolwarm')
cbar = plt.colorbar(CS, orientation = 'horizontal',)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Vertical and Horizontal Structure of Typhoon')
plt.show()
fig = plt.figure(figsize=(6, 6))
CS = plt.contour(X, Y, Z, 20, cmap='coolwarm')
cbar = plt.colorbar(CS, orientation = 'horizontal',)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Vertical and Horizontal Structure of Typhoon')
plt.show()
In [ ]:
Copied!