pythonpandasdataframematplotliberrorbar

Plotting a dataframe of error bars onto a dataframe


I've got a pandas data frame (df) of values as follows:

            0           1           2
0  100.000000  100.000000  100.000000
1    0.412497    0.668880  136.019498
2    5.144450   77.323610  163.496773
3   31.078457   78.151325  146.772621

I also have a data frame (deviation) with the error of each of those values:

          0         1         2
0  0.083579  0.048520  0.082328
1  0.005855  0.005904  0.046494
2  0.009907  0.080799  0.083671
3  0.045831  0.075932  0.044581

I have successfully been able to plot df with matplotlib as I desired:

enter image description here

However, I am struggling to get the error bars onto this graph. My code for plotting is currently as follows:

drugCount = 3
df = pd.DataFrame(drug)
deviation = pd.DataFrame(deviation)
df.columns = list(string.ascii_uppercase[0:drugCount])
ax = df.T.plot(kind='bar', yerr=deviation, color=['C0', 'C3', 'C1', 'C2'])

plt.xlabel('Drug')
plt.ylabel('% Cell viability')
plt.legend(labels=['Control', 'High', 'Medium', 'Low'])
plt.title('Viability of HeLa cells against various drugs')

plt.show()

As you can see, I am trying to pass the deviation data frame into the yerr flag, but it does not do anything, and I am getting the error:

/usr/local/lib/python3.8/site-packages/numpy/core/_asarray.py:83: UserWarning: Warning: converting a masked element to nan.
  return array(a, dtype, copy=False, order=order)

I have had a look online but it seems not many people are trying to add so many error bars like I am trying to. What do I need to change to allow this to work?


Solution

  • try casting deviation to list in (and multiply by 100 to see anything)

    ax = df.T.plot(kind='bar', yerr=list(deviation.values*100) color=['C0', 'C3', 'C1', 'C2'])
    

    enter image description here