Setting Different error bar colors in bar plot in Matplotlib - GeeksforGeeks

15 Jan, 2022
Python provides us with a variety of libraries where Matplotlib is one of them. It is used for data visualization purposes. In this article, we will be setting different error bar colors in the bar plot in Matplotlib.
Various plots of matplotlib such as bar charts, line plots can use error bars. Error bars are used for showing the precision in measurements or calculated values. Without Error Bars, the plot created using matplotlib from a set of values looks to be of high precision or high confidence.
Syntax: matplotlib.pyplot.errorbar(x, y, yerr=None, xerr=None, fmt=”, ecolor=None, elinewidth=None, capsize=None, barsabove=False, lolims=False, uplims=False, xlolims=False, xuplims=False, errorevery=1, capthick=None, \*, data=None, \*\*kwargs)
Parameters: This method accept the following parameters that are described below:
x, y: These parameters are the horizontal and vertical coordinates of the data points.
ecolor: This parameter is an optional parameter. And it is the color of the errorbar lines with default value NONE.
elinewidth: This parameter is also an optional parameter. And it is the linewidth of the errorbar lines with default value NONE.
capsize: This parameter is also an optional parameter. And it is the length of the error bar caps in points with default value NONE.
barsabove: This parameter is also an optional parameter. It contains boolean value True for plotting errorsbars above the plot symbols.Its default value is False.
Example 1:
Step 1: Create a bar plot at first.
Python3
# import matplotlib packageimport matplotlib.pyplot as plt # Store set of values in x# and height for plotting# the graphx = range(4)height = [ 3, 6, 5, 4] # using tuple unpacking# to grab fig and axesfig, ax = plt.subplots() # Creating the bar plot# with opacity=0.1ax.bar(x, height, alpha = 0.1)
 Output: 
Step 2: Add Error Bar to each of the points: 
Python3
# import matplotlib packageimport matplotlib.pyplot as plt # Store set of values in x# and height for plotting# the graphx= range(4)height=[ 3, 6, 5, 4] # using tuple unpacking# to grab fig and axesfig, ax = plt.subplots() # Creating the bar plot# with opacity=0.1ax.bar(x, height, alpha = 0.1) # Zip function acts as an# iterator for tuples so that# we are iterating through# each set of values in a loop for pos, y, err in zip(x, height, error):    ax.errorbar(pos, y, err, lw = 2,                capsize = 4, capthick = 4,                color = "green")     # Showing the plotted error bar# plot with same color which is# greenplt.show()
Output: 
Step 3: Setting different error bar colors in the bar plot(Example 1): 
Python3
# importing matplotlibimport matplotlib.pyplot as plt # Storing set of values in# x, height, error and colors for plotting the graphx= range(4)height=[ 3, 6, 5, 4]error=[ 1, 5, 3, 2]colors = ['red', 'green', 'blue', 'black'] # using tuple unpacking# to grab fig and axesfig, ax = plt.subplots() # plotting the bar plotax.bar( x, height, alpha = 0.1) # Zip function acts as an# iterator for tuples so that# we are iterating through# each set of values in a loopfor pos, y, err, colors in zip(x, height,                               error, colors):       ax.errorbar(pos, y, err, lw = 2,                capsize = 4, capthick = 4,                color = colors)     # Showing the plotted error bar# plot with different colorplt.show()
Output:
Example 2: Setting different error bar colors in the bar plot: 
Python3
# importing matplotlib packageimport matplotlib.pyplot as plt # importing the numpy packageimport numpy as np # Storing set of values in# names, x, height,# error and colors for plotting the graphnames= ['Bijon', 'Sujit', 'Sayan', 'Saikat']x=np.arange(4)marks=[ 60, 90, 55, 46]error=[ 11, 15, 5, 9]colors = ['red', 'green', 'blue', 'black'] # using tuple unpacking# to grab fig and axesfig, ax = plt.subplots() # plotting the bar plotax.bar(x, marks, alpha = 0.5,       color = colors) # Zip function acts as an# iterator for tuples so that# we are iterating through# each set of values in a loopfor pos, y, err, colors in zip(x, marks,                               error, colors):       ax.errorbar(pos, y, err, lw = 2,                capsize = 4, capthick = 4,                color = colors)     # Showing the plotted error bar# plot with different colorax.set_ylabel('Marks of the Students') # Using x_ticks and x_labels# to set the name of the# students at each pointax.set_xticks(x)ax.set_xticklabels(names)ax.set_xlabel('Name of the students') # Showing the plotplt.show()
Output: 
Example 3: Setting different error bar colors in the bar plot. 
Python3
# importing matplotlibimport matplotlib.pyplot as plt # importing the numpy packageimport numpy as np # Storing set of values in# names, x, height, error,# error1 and colors for plotting the graphnames= ['USA', 'India', 'England', 'China']x=np.arange(4)economy=[21.43, 2.87, 2.83, 14.34]error=[1.4, 1.5, 0.5, 1.9]error1=[0.5, 0.2, 0.6, 1]colors = ['red', 'grey', 'blue', 'magenta'] # using tuple unpacking# to grab fig and axesfig, ax = plt.subplots() # plotting the bar plotax.bar(x, economy, alpha = 0.5,       color = colors) # Zip function acts as an# iterator for tuples so that# we are iterating through# each set of values in a loopfor pos, y, err,err1, colors in zip(x, economy,                                    error, error1,                                    colors):       ax.errorbar(pos, y, err, err1, fmt = 'o',                lw = 2, capsize = 4, capthick = 4,                color = colors)     # Showing the plotted error bar# plot with different colorax.set_ylabel('Economy(in trillions)') # Using x_ticks and x_labels# to set the name of the# countries at each pointax.set_xticks(x)ax.set_xticklabels(names)ax.set_xlabel('Name of the countries') # Showing the plotplt.show()
Output: 
 
ruhelaa48
Picked
Python-matplotlib
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Pandas dataframe.groupby()
Python | Get unique values from a list
Defaultdict in Python
Python | os.path.join() method
Python Classes and Objects
Create a directory in Python