python中基于绘制线之间距离的绘制线之间的颜色渐变
我想在这里修改python代码:因此,渐变不是颜色的垂直渐变,而是两条曲线之间的垂直差的函数。因此,如果曲线发散,颜色会变暗python中基于绘制线之间距离的绘制线之间的颜色渐变,python,matplotlib,Python,Matplotlib,我想在这里修改python代码:因此,渐变不是颜色的垂直渐变,而是两条曲线之间的垂直差的函数。因此,如果曲线发散,颜色会变暗 import numpy as np import matplotlib.pyplot as plt import matplotlib.colors as mcolors from matplotlib.patches import Polygon np.random.seed(1977) def main(): for _ in range(2):
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
from matplotlib.patches import Polygon
np.random.seed(1977)
def main():
for _ in range(2):
gradient_fill(*generate_data(100))
plt.show()
def generate_data(num):
x = np.linspace(0, 100, num)
y = np.random.normal(0, 1, num).cumsum()
return x, y
def gradient_fill(x, y, fill_color=None, ax=None, **kwargs):
"""
Plot a line with a linear alpha gradient filled beneath it.
Parameters
----------
x, y : array-like
The data values of the line.
fill_color : a matplotlib color specifier (string, tuple) or None
The color for the fill. If None, the color of the line will be used.
ax : a matplotlib Axes instance
The axes to plot on. If None, the current pyplot axes will be used.
Additional arguments are passed on to matplotlib's ``plot`` function.
Returns
-------
line : a Line2D instance
The line plotted.
im : an AxesImage instance
The transparent gradient clipped to just the area beneath the curve.
"""
if ax is None:
ax = plt.gca()
line, = ax.plot(x, y, **kwargs)
if fill_color is None:
fill_color = line.get_color()
zorder = line.get_zorder()
alpha = line.get_alpha()
alpha = 1.0 if alpha is None else alpha
z = np.empty((100, 1, 4), dtype=float)
rgb = mcolors.colorConverter.to_rgb(fill_color)
z[:,:,:3] = rgb
z[:,:,-1] = np.linspace(0, alpha, 100)[:,None]
xmin, xmax, ymin, ymax = x.min(), x.max(), y.min(), y.max()
im = ax.imshow(z, aspect='auto', extent=[xmin, xmax, ymin, ymax],
origin='lower', zorder=zorder)
xy = np.column_stack([x, y])
xy = np.vstack([[xmin, ymin], xy, [xmax, ymin], [xmin, ymin]])
clip_path = Polygon(xy, facecolor='none', edgecolor='none', closed=True)
ax.add_patch(clip_path)
im.set_clip_path(clip_path)
ax.autoscale(True)
return line, im
main()
这就是该计划的结果:
一种简单的方法是在x1,y1和x2,y2坐标之间创建一个不规则网格,z值等于给定x处的差值y1-y2。这里我使用的是scipy的griddata,但是有一些不同的技术 像这样的
# some data..
x = np.array([[np.linspace(0,100,200)],[np.linspace(0,100,200)]])
y1 = np.linspace(0,100,200)
y2 = [2*i + 2 for i in y1]
y = np.array([y1,y2])
z = np.array([y2 - y1,y2 - y1]) # distance value
# Method
x=x.ravel() #Flat input into 1d vector
x=(x[x!=np.isnan]) #eliminate any NaN
y=y.ravel()
y=(y[y!=np.isnan])
z=z.ravel()
z=(z[z!=np.isnan])
znew = griddata((x, y), z, (x[None,:], y[:,None]), method='linear') # grid the data and interpolate z so z will be equivalent at all equal x coords.
levels = np.linspace(z.min(), z.max(), 100)
plt.ylabel('Y', size=15)
plt.xlabel('X', size=15)
cmap = plt.cm.Blues
cs = plt.contourf(x , y, znew, levels=levels, cmap=cmap) # colour according to z
cbar = plt.colorbar(cs)
cbar.set_label('distance', rotation=90, fontsize=15)
cbar.set_ticks([50,100])
plt.show()
谢谢@smashbro,在我的示例中,对于曲线,您的解决方案有效吗?i、 它们不是单调递增的,x和y阵列只是坐标,因此只要正确创建2d网格,z值的分配是直接进行的。