Matplotlib 弃用警告:用 X 和 Y 指定四边形的角,或者

Matplotlib Deprecation Warning: specify the corners of the quadrilaterals with X and Y, or

我有以下代码来创建彩色价格图表,这是一个输出示例:

代码:

import numpy as np
import pandas as pd
from matplotlib.colors import ListedColormap
from matplotlib import pyplot as plt

def plot(ax: plt.Axes, df: pd.DataFrame, points: pd.Series, booleans: pd.Series, c1: list, c2: list):
    ncols = len(booleans)
    s = booleans.astype(object).replace({False: 0, True: 1})
    z = np.array(s).reshape(1, ncols)
    x = np.array(df.index)
    y = [points.min(), points.max()]
    N = 256
    vals = np.ones((N, 4))
    vals[:, 0] = np.linspace(c1[0], c2[0], N)
    vals[:, 1] = np.linspace(c1[1], c2[1], N)
    vals[:, 2] = np.linspace(c1[2], c2[2], N)
    newcmp = ListedColormap(vals)
    plt.rcParams['pcolor.shading'] = 'auto'
    ax.pcolormesh(x, y, z, alpha=0.2, cmap=newcmp)
    return ax

完全公开,我不明白颜色图在 matplotlib 中是如何工作的,这段代码是根据另一个 SO 问题稍微修改的。

每当我 运行 此代码时都会打印以下错误,而且我尝试过的任何方法似乎都无法解决问题。

注意:在我尝试不同的解决方案并使用 importlib.reload() 重新加载我的 jupyter notebook 中的函数后,错误确实消失了,但它总是稍后回来。

MatplotlibDeprecationWarning: shading='flat' when X and Y have the same dimensions as C is deprecated since 3.3. Either specify the corners of the quadrilaterals with X and Y, or pass shading='auto', 'nearest' or 'gouraud', or set rcParams['pcolor.shading']. This will become an error two minor releases later.

这个问题有点难以回答,因为缺少有关数组 booleans 的创建方式及其含义的信息。

假设booleans表示有属性条线段,需要比点数()短1。您可以使用默认的 'flat' 阴影(网格中每个单元格一种平面颜色)。在下面的代码示例中,df['is ascending'] 中的最后一个值是虚拟的,可以为 colormesh 删除。

import numpy as np
import pandas as pd
from matplotlib.colors import ListedColormap
from matplotlib import pyplot as plt

def plot(ax: plt.Axes, df: pd.DataFrame, points: pd.Series, booleans: pd.Series, c1: list, c2: list):
     s = booleans
     if len(s) == len(df):
          s = s[:-1]  # s needs to be one less than the number of values in the index
     z = np.array(s).reshape(1, -1)
     x = np.array(df.index)
     y = [points.min(), points.max()]
     N = 256
     newcmp = ListedColormap([c1, c2])
     ax.pcolormesh(x, y, z, alpha=0.2, cmap=newcmp, shading='flat')

N = 50
df = pd.DataFrame({'val': np.random.normal(1, 10, N).cumsum() + 1000},
                  index=pd.date_range('20220101', periods=N, freq='D'))
df['is ascending'] = df['val'] > df['val'].shift(-1)

ax = df['val'].plot()
plot(ax, df, df['val'], df['is ascending'], 'limegreen', 'crimson')

另一方面,如果 booleans 是 属性 个点,则可以使用具有两行且列数与点数完全相同的网格。在这种情况下,'gouraud' 着色将处理每个顶点的着色。


def plot(ax: plt.Axes, df: pd.DataFrame, points: pd.Series, booleans: pd.Series, c1: list, c2: list):
     s = booleans
     z = np.tile(np.array(s), (2, 1))
     x = np.array(df.index)
     y = [points.min(), points.max()]
     N = 256
     newcmp = ListedColormap([c1, c2])
     ax.pcolormesh(x, y, z, cmap=newcmp, shading='gouraud')

N = 50
df = pd.DataFrame({'val': np.random.normal(1, 10, N).cumsum() + 1000},
                  index=pd.date_range('20220101', periods=N, freq='D'))
df['is larger than mean'] = df['val'] > df['val'].mean()

ax = df['val'].plot()
plot(ax, df, df['val'], df['is larger than mean'], 'lightgreen', 'lightcoral')