Plotting shaded uncertainty region in line plot in matplotlib when data has NaNs

前端 未结 2 1000
北荒
北荒 2020-12-08 22:57

I would like a plot which looks like this:

I am trying to do this with matplotlib:

fig, ax = plt.subplots()

with sns.axes_style(\"darkgrid\"):
             


        
2条回答
  •  有刺的猬
    2020-12-08 23:07

    Ok. So one of the problem was that the dtype of my data was object and not float and this caused fill_between to fail when it looked to see if the numbers were finite. I finally managed to do it by (a) converting to float and then (b) to solve the problem of the matching colours for uncertainty and line, to use a colour palette. So I have:

    import seaborn as sns
    import numpy as np
    import matplotlib.pyplot as plt
    import pandas as pd
    fig, ax = plt.subplots()
    clrs = sns.color_palette("husl", 5)
    with sns.axes_style("darkgrid"):
        epochs = list(range(101))
        for i in range(5):
            meanst = np.array(means.ix[i].values[3:-1], dtype=np.float64)
            sdt = np.array(stds.ix[i].values[3:-1], dtype=np.float64)
            ax.plot(epochs, meanst, label=means.ix[i]["label"], c=clrs[i])
            ax.fill_between(epochs, meanst-sdt, meanst+sdt ,alpha=0.3, facecolor=clrs[i])
        ax.legend()
        ax.set_yscale('log')
    

    which gave me the following result:

提交回复
热议问题