Python Pandas error bars are not plotted and how to customize the index

arnold

I need your help on plotting error bars using Pandas in Python. I have read the Pandas documentation, and did some trials and errors, but got no satisfying result.

Here is my code:

'''
usage : (python) rc-joint-plot-error-bar.py
'''

from __future__ import print_function
import pandas as pd
import matplotlib.pyplot as plt

filename = 'rc-plot-error-bar.csv'

df = pd.read_csv(filename, low_memory = False)

headers = ['Specimen', 'CA_Native_Mean', 'CA_Implant_Mean', 'CP_Native_Mean',
    'CP_Implant_Mean', 'CA_Native_Error', 'CA_Implant_Error', 'CP_Native_Error',
    'CP_Implant_Error']
    
for header in headers :
    df[header] = pd.to_numeric(df[header], errors = 'coerce')
    
CA_means = df[['CA_Native_Mean','CA_Implant_Mean']]
CA_errors = df[['CA_Native_Error','CA_Implant_Error']]

CP_means = df[['CP_Native_Mean', 'CP_Implant_Mean']]
CP_errors = df[['CP_Native_Error', 'CP_Implant_Error']]

CA_means.plot.bar(yerr=CA_errors)
CP_means.plot.bar(yerr=CP_errors)

plt.show()

Here is what my dataframe looks like:

   Specimen  CA_Native_Mean  CA_Implant_Mean  CP_Native_Mean  CP_Implant_Mean  \
0         1               1         0.738366               1         1.087530
1         2               1         0.750548               1         1.208398
2         3               1         0.700343               1         1.394535
3         4               1         0.912814               1         1.324024
4         5               1         1.782425               1         1.296495
5         6               1         0.415147               1         0.479259
6         7               1         0.934014               1         1.084714
7         8               1         0.526591               1         0.873022
8         9               1         1.409730               1         2.051518
9        10               1         1.745822               1         2.134407

   CA_Native_Error  CA_Implant_Error  CP_Native_Error  CP_Implant_Error
0                0          0.096543                0          0.283576
1                0          0.076927                0          0.281199
2                0          0.362881                0          0.481450
3                0          0.400091                0          0.512375
4                0          2.732206                0          1.240796
5                0          0.169731                0          0.130892
6                0          0.355951                0          0.272396
7                0          0.258266                0          0.396502
8                0          0.360461                0          0.451923
9                0          0.667345                0          0.404856

If I ran the code, I got the following figures: plotting CA_means, but CA_errors are not shown plotting CP_means, but CP_errors are not shown

My questions are:

  1. Could you please let me know how to make the error bars appear in the figures?
  2. How to change the index (the values of x-axis) from 0-9 into 1-10?

Big thanks!

Regards, Arnold A.

Sergey Bushmanov

You're almost there!

  1. For your error bars to show up in the plot, the column names in yerr should match those of the data in the bar plot. Try renaming CA_errors.

  2. For changing x-labels, try ax.set_xticklabels(df.Specimen);


_, ax= plt.subplots() 
CA_means = df[['CA_Native_Mean','CA_Implant_Mean']] 
CA_errors = df[['CA_Native_Error','CA_Implant_Error']].\ 
                rename(columns={'CA_Native_Error':'CA_Native_Mean', 
                                'CA_Implant_Error':'CA_Implant_Mean'}) 
CA_means.plot.bar(yerr=CA_errors, ax=ax) 
ax.set_xticklabels(df.Specimen);

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python Pandas error bars are not plotted and how to customize the index

From Dev

Error bars are not plotted when axes are reversed (plotCI from package plotrix)

From Dev

Error bars are not plotted when axes are reversed (plotCI from package plotrix)

From Dev

How to have clusters of stacked bars with python (Pandas)

From Dev

Bars to be plotted over map

From Dev

how to add error bars to histogram diagram in python

From Dev

How to plot error bars in polar coordinates in python?

From Dev

How do you add error bars to Bokeh plots in python?

From Java

How do you remove spaces between bars in bar charts for where plotted values are zero?

From Dev

Python histogram with points and error bars

From Dev

Python histogram with points and error bars

From Dev

Python index error: How to debug

From Dev

Adding error bars to grouped bar plot in pandas

From Dev

Plotting error bars on barplots with multiple series in pandas

From Dev

How to customize the colorbar in python?

From Dev

How to plot specific rows and columns of pandas dataframe (based on name of row and name of column) in bar plot with error bars?

From Dev

How to get color of most recent plotted line in Python's plt

From Dev

How to convert an arrange in a date time format to be plotted in python?

From Dev

How to change color of plotted curves when using Animations in Python MatPlotLib?

From Dev

How to convert an arrange in a date time format to be plotted in python?

From Dev

How to pass index to function Python Pandas

From Dev

Python / Pandas : How many levels in a dataframe index?

From Dev

Python / Pandas : How many levels in a dataframe index?

From Dev

Pandas : Python how to do index Groupby with replicates

From Dev

How to customize a UITableview's index look?

From Dev

Issue in Error bars in Seaborn barplot - Python

From Dev

ggplot how to create paired error bars

From Dev

How to show sample error bars in the legend in R?

From Dev

How to show sample error bars in the legend in R?

Related Related

  1. 1

    Python Pandas error bars are not plotted and how to customize the index

  2. 2

    Error bars are not plotted when axes are reversed (plotCI from package plotrix)

  3. 3

    Error bars are not plotted when axes are reversed (plotCI from package plotrix)

  4. 4

    How to have clusters of stacked bars with python (Pandas)

  5. 5

    Bars to be plotted over map

  6. 6

    how to add error bars to histogram diagram in python

  7. 7

    How to plot error bars in polar coordinates in python?

  8. 8

    How do you add error bars to Bokeh plots in python?

  9. 9

    How do you remove spaces between bars in bar charts for where plotted values are zero?

  10. 10

    Python histogram with points and error bars

  11. 11

    Python histogram with points and error bars

  12. 12

    Python index error: How to debug

  13. 13

    Adding error bars to grouped bar plot in pandas

  14. 14

    Plotting error bars on barplots with multiple series in pandas

  15. 15

    How to customize the colorbar in python?

  16. 16

    How to plot specific rows and columns of pandas dataframe (based on name of row and name of column) in bar plot with error bars?

  17. 17

    How to get color of most recent plotted line in Python's plt

  18. 18

    How to convert an arrange in a date time format to be plotted in python?

  19. 19

    How to change color of plotted curves when using Animations in Python MatPlotLib?

  20. 20

    How to convert an arrange in a date time format to be plotted in python?

  21. 21

    How to pass index to function Python Pandas

  22. 22

    Python / Pandas : How many levels in a dataframe index?

  23. 23

    Python / Pandas : How many levels in a dataframe index?

  24. 24

    Pandas : Python how to do index Groupby with replicates

  25. 25

    How to customize a UITableview's index look?

  26. 26

    Issue in Error bars in Seaborn barplot - Python

  27. 27

    ggplot how to create paired error bars

  28. 28

    How to show sample error bars in the legend in R?

  29. 29

    How to show sample error bars in the legend in R?

HotTag

Archive