使用matplotlib在折线图下方填充渐变?

使用matplotlib在折线图下方填充渐变?,matplotlib,Matplotlib,我正试图制作一个线条图,在线条下面有渐变填充。我在网上搜索了几个小时寻找解决方案,但没有一个专门针对我所寻找的问题 ma = average_rate(t[0], window=900, interval=60) fig = Figure(figsize=(8.5, 1.5), dpi=100) canvas = FigureCanvasAgg(fig) col = '#4f81b3' ax = fig.add_axes([0.076, 0.11, 0.88, 0.74]) dts, val

我正试图制作一个线条图,在线条下面有渐变填充。我在网上搜索了几个小时寻找解决方案,但没有一个专门针对我所寻找的问题

ma = average_rate(t[0], window=900, interval=60)
fig = Figure(figsize=(8.5, 1.5), dpi=100)
canvas = FigureCanvasAgg(fig)

col = '#4f81b3'
ax = fig.add_axes([0.076, 0.11, 0.88, 0.74])

dts, vals = zip(*ma)
ax.fill(dts, vals, color=col)
fig.savefig(b, format='png')
这将生成以下图表:

我曾尝试用在线找到的代码使用colormaps、contourf、fill_between等,但无法使其正常工作,我真的希望有人能为这个问题提供一个简单的解决方案

在@Ajean的帮助下,我的最新代码如下:

    # dtseries contains a list of datetime.datetime values
    # yvalues contains a corresponding list of y-axis values
    # len(dtseries) == len(yvalues)

    import numpy as np

    # Need dpi for png generation
    fig = Figure(figsize=(8.5, 2), dpi=100)
    # Create axes directly on figure [left, bottom, width, height]
    ax = fig.add_axes([0.076, 0.11, 0.88, 0.74])

    xlims = mdates.date2num([dtseries[0], dtseries[-1]])

    # Construct an image linearly increasing in y
    xv, yv = np.meshgrid(np.linspace(0,1,50), np.linspace(0,1,50))
    zv = yv

    ax.imshow(zv, cmap='PuBu', origin='lower',
              extent=[xlims[0], xlims[1], min(yvalues), max(yvalues)])

    # Erase above the data by filling with white
    ax.fill_between(dtseries, yvalues, max(yvalues), color='w')

    # Make the line plot over the top
    colr = '#325272'
    ax.plot(dtseries, yvalues, color=colr, linewidth=0.5)

    ax.set_ylim(min(yvalues), max(yvalues))

    # Render chart as png to memory
    b = BytesIO()
    fig.savefig(b, format='png')
    return b.getvalue()
这就是我得到的:


这个问题实际上有一个相当好的答案,下面借用了主要思想,但我用调用
imshow
代替了
tourtf
,因为我认为它看起来更平滑。我借用了关键元素,即在整个图像上放置渐变,然后使用
fill\u在
之间“擦除”数据

import numpy as np
import matplotlib.pyplot as plt
import datetime
import matplotlib.dates as mdates

# Fake data using dates as requested
xdata = np.array([datetime.datetime.today()+
                 datetime.timedelta(days=1)*i for i in range(15)])
ydata = np.cumsum(np.random.uniform(size=len(xdata)))
xlims = mdates.date2num([xdata[0], xdata[-1]])

# Construct an image linearly increasing in y
xv, yv = np.meshgrid(np.linspace(0,1,50), np.linspace(0,1,50))
zv = yv

# Draw the image over the whole plot area
fig, ax = plt.subplots(figsize=(5,3))
ax.imshow(zv, cmap='YlGnBu_r', origin='lower',
          extent=[xlims[0], xlims[1], ydata.min(), ydata.max()])

# Erase above the data by filling with white
ax.fill_between(xdata, ydata, ydata.max(), color='w')

# Make the line plot over the top
ax.plot(xdata, ydata, 'b-', linewidth=2)

ax.set_ylim(ydata.min(), ydata.max())
fig.autofmt_xdate()

plt.show()
这给了我这样一个情节:


我非常感谢您的反馈。我会给你反馈,一旦我可以在我的电脑前再次。如果我使用x轴上的时间序列而不是常规的数字集,这会有问题吗RobIt只会影响你设定限制的方式。imshow不会对纯日期做出正确反应,但您可以利用
matplotlib.dates中的
date2num
将您的限制转换为正确的内容(放入
extent=
),我非常感谢您的帮助。我正在努力使用网格网格函数,因为我使用的是一个时间序列,我对numpy不太熟悉。您是否可以使用x轴上的时间序列而不是常规数值来更新答案?这将是一个很大的帮助!你根本不需要改变网格的部分,那只是为了生成图像的数据。你必须改变范围。我更新了答案。谢谢你的反馈。我有与您显示的相同的扩展参数,但没有图表。显然,我现在正在做一些愚蠢的事情,这可能与我没有使用pyplot有关,因为这段代码在Ubuntu服务器上运行,我直接生成和操作图形。我将用我现在的代码更新我上面的帖子,也许你可以看到问题所在…没有数据很难调试。。。你有什么错误吗?看起来您应该在某个地方出错了,因为它根本没有打印任何内容。@Ajean没有错误-只是没有输出任何内容。我已经接受了你的解决方案,非常感谢你的帮助。我可能会用一些假数据再试一次,以便我们都使用相同的数据集。再次感谢您的帮助@Ajean:如果你有兴趣帮助我,请告诉我,或者可以告诉我在哪里可以用$$完成我的“管道”图表。现在我有更重要的事要做。谢谢