Matplotlib -使用fill_between在绘图中重新创建堆栈绘图网格线渲染

ztmd8pv5  于 2023-02-05  发布在  其他
关注(0)|答案(2)|浏览(101)

我需要用fill_between(无堆叠)重新创建一个简单的堆叠图,网格线呈现看起来不一样,我不知道如何让它们看起来一样,代码如下:

import pandas as pd
import numpy as np

mpl.rcParams.update(mpl.rcParamsDefault)
plt.style.use("ggplot")

_styles = {
'axes.edgecolor': '#bcbcbc',
'axes.facecolor': 'white',
'grid.color': '#b2b2b2',
'grid.linestyle': '--',
}

plt.rcParams.update(_styles)

def plot_a_plot(x_axis, actual, predicted, size=(10,4)):
    plt.figure(figsize=size)
    
    p1 = plt.plot(x_axis, actual, alpha=0.5, label=actual.name, lw=0.5)
    plt.fill_between(x_axis, actual.astype(np.float64), color='#F0A498', alpha=1, zorder=1)
    
    p2 = plt.plot(x_axis, predicted, alpha=0.5, label=predicted.name, lw=0.5)
    plt.fill_between(x_axis, predicted.astype(np.float64), color='C1', alpha=0.5, zorder=0)
    
    plt.grid(True, zorder=10)
    
    plt.title('Plot with fill_between')
    plt.show()

def plot_a_stackplot(x_axis, actual, predicted, size=(10,4)):
    y = np.vstack([actual.astype(float), predicted.astype(float)])
    plt.figure(figsize=size)
    plt.stackplot(x_axis, y, labels=[actual.name, predicted.name], alpha=0.5, edgecolors="face")
   
    plt.title('Stackplot')
    plt.show()

arr = np.random.rand(10)

data = {
  "actual": arr,
  "predicted": arr*2
}

df = pd.DataFrame(data)

x_axis = df.index
actual = df['actual']
predicted = df['predicted']

plot_a_plot(x_axis, actual, predicted)
plot_a_stackplot(x_axis, actual, predicted)

View example here
改变zorder似乎没有任何效果,我也玩过alpha水平等等--似乎什么都不起作用。堆栈图上的网格线看起来就是它应该看的样子,简单图上的网格线看起来很模糊。

isr3a4wc

isr3a4wc1#

zorder似乎不能很好地与值0配合使用,在这个demo中,它总是被选择为〉=1。
如果您将函数更改为:

def plot_a_plot(x_axis, actual, predicted, size=(10,4)):
    plt.figure(figsize=size)
    
    p1 = plt.plot(x_axis, actual, alpha=0.5, label=actual.name, lw=0.5)
    plt.fill_between(x_axis, actual.astype(np.float64), color='#F0A498', alpha=1, zorder=2)
    
    p2 = plt.plot(x_axis, predicted, alpha=0.5, label=predicted.name, lw=0.5)
    plt.fill_between(x_axis, predicted.astype(np.float64), color='C1', alpha=0.5, zorder=1)
    
    plt.grid(True, zorder=10)
    
    plt.title('Plot with fill_between')
    plt.show()

编辑:如果你想让底部区域(红色)看起来和stackplot一样,你应该使用正确的颜色。你可以用以下方法找到颜色:

for color in plt.rcParams['axes.prop_cycle']:
    print(color)

第一个是你要找的:#E24A33C0也可以)
从这里开始,对fill_between的第二次调用应该填充actualpredicted之间的区域,否则填充区域将重叠:

def plot_a_plot(x_axis, actual, predicted, size=(10,4)):
    plt.figure(figsize=size)
    
    p1 = plt.plot(x_axis, actual, alpha=0.5, label=actual.name, lw=0.5)
    plt.fill_between(x_axis, actual.astype(np.float64), color='#E24A33', alpha=0.5, zorder=2)
    
    p2 = plt.plot(x_axis, predicted, alpha=0.5, label=predicted.name, lw=0.5)
    plt.fill_between(x_axis, actual, predicted, color='C1', alpha=0.5, zorder=1)

    plt.grid(True, zorder=10)

    plt.title('Plot with fill_between')
    plt.show()
dfty9e19

dfty9e192#

尝试以下操作(在本例中我没有添加标签):

def plot_a_plot(x_axis, actual, predicted, size=(10, 4)):
    fig, ax = plt.subplots(figsize=size)
    ax.plot(x_axis, actual, lw=0.5, alpha=0.5)
    ax.fill_between(
        x_axis,
        actual,
        np.zeros_like(data["actual"]),  # explicitly set the "bottom" to be zero (this is the default so isn't actually required here)
        color='#F0A498',
        alpha=0.5,  # set alpha to 0.5
        zorder=1
    )
    
    ax.plot(x_axis, predicted, lw=0.5, alpha=0.5)
    ax.fill_between(
        x_axis,
        predicted,
        actual,  # explicitly set the "bottom" to be actual, so it doesn't cover actual
        color='C1',
        alpha=0.5,
        zorder=0
    )
    ax.grid(zorder=10)
    fig.show()

这基本上就是stackplot正在做的事情(尽管它显然绘制了累积的“顶部”和“底部”)。

相关问题