Adding water flow arrows to Matplotlib Contour Plot

谁都会走 提交于 2019-12-03 10:03:06

问题


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 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:


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()



来源:https://stackoverflow.com/questions/16529892/adding-water-flow-arrows-to-matplotlib-contour-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!