是否可以在matplotlib中的曲线下获得颜色渐变?

汤姆·库鲁辛格:

我碰巧在此页面上看到一个漂亮的图形,如下所示:

在此处输入图片说明

是否可以在matplotlib中获得这种颜色渐变?

乔·肯顿(Joe Kington):

以前有一些类似问题的答案(例如https://stackoverflow.com/a/22081678/325565),但他们建议使用次优方法。

先前的大多数答案都建议在pcolormesh填充上绘制白色多边形这不理想,原因有两个:

  1. 轴的背景不能透明,因为上面有一个填充的多边形
  2. pcolormesh 绘制速度很慢,插值也不流畅。

这需要更多的工作,但是有一种方法可以更快地绘制并提供更好的视觉效果:设置用绘制的图像的剪切路径imshow

举个例子:

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

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在matplotlib中的曲线下阴影区域

如何在matplotlib中的曲线下阴影区域

分位数在密度曲线下的阴影(填充或颜色)区域

是否可以在QML中对文本应用颜色渐变?

是否可以迅速获得颜色名称

Matplotlib-基于光谱颜色的曲线下颜色

如何用可视光谱颜色填充曲线下的区域

在R中用颜色渐变绘制曲线

matplotlib中的渐变填充和颜色过渡

动画可绘制XML中的渐变颜色可以吗?

直接保存时Matplotlib无法在曲线下正确渲染渐变

如果可以访问颜色,是否可以访问这些颜色之间的渐变?

用matplotlib填充曲线下的区域

查找曲线下的最大面积| 熊猫,matplotlib

是否可以在不将Scrapold背景包裹在容器中的情况下为它提供渐变颜色?

是否可以从matplotlib中的刻度线下方的某处绘制箭头

计算gnuplot中曲线下的面积

在iOS中是否可以基于RGB值获得颜色名称?

是否可以在matplotlib中为edgecolors分配颜色图?

如何使用Numpy拟合函数并获得曲线下的面积

在曲线下添加颜色,使用 AxesSubplot 对象(matplotlib 和 tkinter)

在 matplotlib 中更改颜色条渐变

如何在ggplot2中获得水平颜色渐变?

LOWESS 曲线下的 matplotlib 填充

每个直方图曲线下方的渐变填充 - Python

使用 Matplotlib 的颜色条扩展中的颜色渐变

是否可以在 Google Apps Scripts for Google Sheets 中为单个单元格创建颜色渐变?

如何获得精确召回曲线下的面积

如何用不同颜色填充KDE曲线下的区间