Issue
I'd like to make a plot that looks like this one (an elevation graph):
Here they're plotting the elevation vs distance, and the region under the graph is colored by the slope (i.e., dElevation/dx). Reds have a positive slope, greens are flat-ish and blues are negative slopes.
If I have x and elevation, it's easy to calculate the slope (dElevation/dx), but I'm stumped how to fill the graph in with a color that changes by slope.
Solution
Mainly using this approach and adjusting it a little makes this possible:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.path import Path
from matplotlib.patches import PathPatch
x = np.arange(0,10,0.1)
y = 0.5*np.sin(x)+0.6
grad = np.gradient(y)
elevation_origin = -0.05 # set the zero line until which the color map is filled in.
fig,ax = plt.subplots(1,1)
ax.plot(x, y, label='Elevation')
path = Path(np.array([np.append(x,x[::-1]), np.append(y,np.zeros_like(y))]).T)
patch = PathPatch(path, facecolor='none')
ax.add_patch(patch)
im = plt.imshow(grad.reshape(1,y.size), cmap=plt.cm.jet, interpolation='bicubic',
extent=[x[0], x[-1], elevation_origin, np.max(y)],aspect='auto',
clip_path=patch, clip_on=True)
plt.show()
Of course you can choose different color maps if desired but jet
should match your example picture. Note that this approach needs to be modified if there are elevations below elevation_origin
. Please also note that this was possible to find (at least most of it) with a web search.
Answered By - David Wierichs
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.