Plotting a shaded area of ​​uncertainty in a line graph in matplotlib when data has NaNs

I need a plot that looks like this: plot with uncertainty

I am trying to do this with matplotlib:

fig, ax = plt.subplots()

with sns.axes_style("darkgrid"):
    for i in range(5):
        ax.plot(means.ix[i][list(range(3,104))], label=means.ix[i]["label"])
        ax.fill_between(means.ix[i][list(range(3,104))]-stds.ix[i][list(range(3,104))], means.ix[i][list(range(3,104))]+stds.ix[i][list(range(3,104))])
    ax.legend()

I want the shaded area to be the same color as the line in the center. But right now, my problem is that it meanshas some NaNand fill_betweendoes not accept it. I get an error

TypeError: ufunc 'isfinite' is not supported for input types, and inputs cannot be safely enforced to any supported types in accordance with the casting rule `` safe ''

Any ideas on how I can achieve what I want? The solution is not to use matplotlib if it can draw my series of points with their uncertainty for several series.

+4
2

NaNs means DataFrame ?

, means DataFrame NaN, . , stds DataFrame, , NaN , , NaN means, temp_means, , temp_means, std stds.

() (), NaN s

x = np.linspace(0, 30, 100)
y = np.sin(x/6*np.pi)
error = 0.2

means = pd.DataFrame(np.array([x,y]).T,columns=['time','mean'])
stds = pd.DataFrame(np.zeros(y.shape)+error)

#sprinkle some NaN in the mean
sprinkles = means.sample(10).index
means.loc[sprinkles] = np.NaN


fig, axs = plt.subplots(2,1)

axs[0].plot(means.ix[:,0], means.ix[:,1])
axs[0].fill_between(means.ix[:,0], means.ix[:,1]-stds.ix[:,0], means.ix[:,1]+stds.ix[:,0])

temp_means = means.dropna()

axs[1].plot(temp_means.ix[:,0], temp_means.ix[:,1])
axs[1].fill_between(temp_means.ix[:,0], temp_means.ix[:,1]-stds.loc[temp_means.index,0], temp_means.ix[:,1]+stds.loc[temp_means.index,0])


plt.show()

enter image description here

+2

Ok. , dtype object, float, fill_between, , . , , (a) float, (b), , . :

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')

: enter image description here

0

Source: https://habr.com/ru/post/1673388/


All Articles