将水流箭头添加到Matplotlib Contour Plot

时间:2013-05-13 19:43:14

标签: python matplotlib contour

我正在使用Matplotlib生成地下水高程轮廓。见下文

这是我现在拥有的;如何添加水流箭头,如下图所示? enter image description here

我想添加箭头使它看起来像这样: Groundwater Contour with Arrows

如果有人有一些非常感激的想法和/或代码示例。

1 个答案:

答案 0 :(得分:13)

您需要最近的(> = 1.2)版本的matplotlib,但streamplot会这样做。你只需要采取负头的梯度(a.k.a。“地下含水层的水位”)网格。

作为从头部的随机点观察产生的快速示例:

import numpy as np
from scipy.interpolate import Rbf
import matplotlib.pyplot as plt
# Make data repeatable
np.random.seed(1981)

# Generate some random wells with random head (water table) observations
x, y, z = np.random.random((3, 10))

# Interpolate these onto a regular grid
xi, yi = np.mgrid[0:1:100j, 0:1:100j]
func = Rbf(x, y, z, function='linear')
zi = func(xi, yi)

# -- Plot --------------------------
fig, ax = plt.subplots()

# Plot flowlines
dy, dx = np.gradient(-zi.T) # Flow goes down gradient (thus -zi)
ax.streamplot(xi[:,0], yi[0,:], dx, dy, color='0.8', density=2)

# Contour gridded head observations
contours = ax.contour(xi, yi, zi, linewidths=2)
ax.clabel(contours)

# Plot well locations
ax.plot(x, y, 'ko')

plt.show()

enter image description here