将水流箭头添加到Matplotlib轮廓图 [英] Adding water flow arrows to Matplotlib Contour Plot
问题描述
我正在用Matplotlib生成地下水高程等值线。见下文
I am generating a groundwater elevation contour with Matplotlib. See below
这里是我现在所拥有的;如何添加水流箭头,如下图所示?
Here is what I have now; how can I add water flow arrows like the image below?
I想要添加箭头使其看起来像这样:
I want to add arrows to make it look like this:
如果有人有一些想法和/或代码示例,将不胜感激。
If anyone has some ideas and/or code samples that would be greatly appreciated.
推荐答案
您需要使用最新版本(> = 1.2)的matplotlib,但 streamplot
执行此操作。您只需要取水头的负梯度(地表含水层又称地下水位)网格即可。
You'll need a recent (>= 1.2) version of matplotlib, but streamplot
does this. You just need to take the negative gradient of your head (a.k.a. "water table" for surface aquifers) grid.
下面是一个从水头的随机点观测中生成的快速示例:
As a quick example generated from random point observations of head:
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()
这篇关于将水流箭头添加到Matplotlib轮廓图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!