Python Cmap w/线图。可以在线下(但为实线)或渐变(但为线上)打印

Python Cmap w/线图。可以在线下(但为实线)或渐变(但为线上)打印,python,matplotlib,graph,data-visualization,gradient,Python,Matplotlib,Graph,Data Visualization,Gradient,我正在尝试一个基于pandas和matplotlib处理的数据集的可视化问题。我把数据画成直线图。我的目标是用cmap(例如“等离子”)对曲线下方的区域进行渐变 然而,由于不同的原因,我两次最好的尝试都是错误的。第一个将使用渐变着色,但仅在线条上。第二个将在线条下着色,但仅使用纯色。我被困了很长时间。。。谢谢大家! ax = plot_chance_death.plot(kind='line', x = 'State', y = 'Percent Chance', ax=ax, color='

我正在尝试一个基于pandas和matplotlib处理的数据集的可视化问题。我把数据画成直线图。我的目标是用cmap(例如“等离子”)对曲线下方的区域进行渐变

然而,由于不同的原因,我两次最好的尝试都是错误的。第一个将使用渐变着色,但仅在线条上。第二个将在线条下着色,但仅使用纯色。我被困了很长时间。。。谢谢大家!

ax = plot_chance_death.plot(kind='line', x = 'State', y = 'Percent Chance', 
ax=ax, color='indigo')
l1 = ax.lines[0]
x1 = l1.get_ydata()
y1 = l1.get_xdata()
fig, ax = plt.subplots()

# plot only the outline of the polygon, and capture the result
poly, = ax.fill(x1, y1, facecolor='none')

# get the extent of the axes
xmin, xmax = ax.get_xlim()
ymin, ymax = ax.get_ylim()

# create a dummy image
img_data = np.arange(ymin,ymax,(ymax-ymin)/100.)
img_data = img_data.reshape(img_data.size,1)

# plot and clip the image
im = ax.imshow(img_data, aspect='auto',  origin='upper', cmap='plasma', 
extent=[xmin,xmax,ymin,ymax], vmin=1., vmax=y1.max())

#this shows the gradient but above the line
im.set_clip_path(poly)

###this solution colors underneath but solid color
ax.fill_between(x1, y1, y2=0, cmap='plasma',  norm=(0,.5))

将要用作剪辑路径的区域底部包含到
路径中是有意义的。您可以从数据的
绘图
创建
路径
,然后将两个底部点添加到其中

import pandas as pd
import numpy as np; np.random.seed(42)
import matplotlib.pyplot as plt
from matplotlib.path import Path

df = pd.DataFrame({"x" : np.linspace(0,0.05,40),
                   "y" : np.cumsum(np.random.rand(40))[::-1]*3})


fig, ax = plt.subplots()


l, = ax.plot(df.x, df.y, color="k")

# get the extent of the axes
xmin, xmax = ax.get_xlim()
ymin, ymax = ax.get_ylim()

# create a dummy image
img_data = np.arange(ymin,ymax,(ymax-ymin)/100.)
img_data = img_data.reshape(img_data.size,1)

# plot and clip the image
im = ax.imshow(img_data, aspect='auto',  origin='upper', cmap='plasma', 
               extent=[xmin,xmax,ymin,ymax], vmin=1., vmax=df.y.max())

px,py = l.get_data()

p0 = [[px[-1], py.min()], [px[0], py.min()]]
p = np.concatenate((np.c_[px,py],p0))
path = Path(p)

im.set_clip_path(path, transform=ax.transData)

plt.show()