Pandas dataframe errorbar plots separated by category

1.6k Views Asked by At

So I've tried seaborn and pandas native plotting features and feel like I'm relegated to using pure matplotlib which is a disappointment but this is the problem I currently have.

I have a pandas data frame that has x-values in time, y-values in magnitude, and custom error values in a column called uncertainty. I also have a column called Filters. For each filter, I want to plot essentially an error bar plot with x=time, y=magnitude, yerr = uncertainty. Here is a sample data (since the real data is proprietary):

Image   Filter    Time  Magnitude   Uncertainty
File1   1 micron    0   12.1    0.008
File2   1.5 micron  0   13.4    0.01
File3   1 micron    1   12.9    0.01
File4   1.5 micron  1   13.8    0.013
File5   1 micron    2   14.2    0.014
File6   1.5 micron  2   14.66   0.0155
File7   1 micron    3   15.12   0.017
File8   1.5 micron  3   15.58   0.0185
File9   1 micron    4   16.04   0.02
File10  1.5 micron  4   16.5    0.0215
File11  1 micron    5   16.96   0.023
File12  1.5 micron  5   17.42   0.0245
File13  1 micron    6   17.88   0.026
File14  1.5 micron  6   18.34   0.0275
File15  1 micron    7   18.8    0.029
File16  1.5 micron  7   19.26   0.0305
File17  1 micron    8   19.72   0.032
File18  1.5 micron  8   20.18   0.0335
File19  1 micron    9   20.64   0.035
File20  1.5 micron  9   21.1    0.0365

So on a single plot, I would like a series of points with errorbars (based on the uncertainty column) corresponding to that filter (either 1 micron or 1.5 micron).

If I do this in seaborn I get the following:

Seaborn's attempt at doing a plot

I cannot do a map with errorbar (as is commented in the line above) because it claims:

"ValueError: yerr must be a scalar, the same dimensions as y, or 2xN."

If I try this in pandas directly, I get the following:

Pandas attempt at doing this

Neither of these is what I want. I thought pandas and seaborn were supposed to alleviate all these issues and make it simpler and easier to do this sort of work without having to do contrived nonsense like reading it into numpy genfromtxt, sorting by filter, etc.

1

There are 1 best solutions below

0
On

This is achieving what you want:

g = sns.FacetGrid(data=df, hue='Filter')
g.map(plt.errorbar, 'Time', 'Magnitude', 'Uncertainty', fmt='o', elinewidth=1, capsize=5, capthick=1)
g.add_legend()

enter image description here

as @FabienP said in his comment, your problem might simply be that your errorbars are too small to see. Do you mean to plot yerr=Magnitude*Uncertainty?