Matplotlib Bars

In this article, we’ll dive into the Matplotlib bars, including how to create and customize bar charts, as well as some tips for making your charts more effective.

Bar charts are a great way to display and compare data across categories, and Matplotlib bar chart function makes it easy to create customizable and visually appealing charts.

Matplotlib Bars



Creating Bars Chart

Through Pyplot’s bar() function, you can generate bar graphs:

Generate a bar graph of employee data:

Example: 

import matplotlib.pyplot as pt import numpy as npy emp_name = npy.array(["Harry", "Kate", "Tim", "Sarah", "Dustin", 'Jack']) emp_salary = npy.array([3000, 2000, 2500, 1500, 1800, 2800]) #salary in $ pt.bar(emp_name,emp_salary) pt.show()

Display the student results with a bar graph:

Example: 

import matplotlib.pyplot as pt import numpy as npy student_name = npy.array(["Liza", "Micheal","Shakira","Mike", "Robert", "Jessica", "Max", 'Eleven']) student_cgpa = npy.array([3.3, 2.2, 3.4,4,2.3, 3.1, 3.9, 2.5]) pt.bar(student_name,student_cgpa) pt.show()
It is important to note that the bar() function accepts arguments that indicate the layout of the bars.

As arrays, the first and second arguments describe the types and their data.

Example: 

import matplotlib.pyplot as pt import numpy as npy mrx = npy.array(["Football", "Cricket","Field Hockey","Tennis"]) ample = npy.array([3.5, 2.5, 2, 1]) #estimated fans ratio in billion pt.bar(mrx,ample) pt.show()

Show the statical data of football leagues with bar() function:

Example: 

import matplotlib.pyplot as pt import numpy as npy leagues = npy.array(["Premier League", "La Liga", "Bundesliga", "Serie A", "Ligue 1"]) ratings = npy.array([93.2, 91.2, 90.4, 89.4, 85.4]) pt.bar(leagues,ratings) pt.show()

Matplotlib barh() – Horizontal Bars

You can implement the barh() function if you prefer to visualize the bars horizontally in place of vertically:

In the following employee data example insert six horizontal bars:

Example: 

import matplotlib.pyplot as pt import numpy as npy emp_name = npy.array(["Harry", "Kate", "Tim", "Sarah", "Dustin", 'Jack']) emp_salary = npy.array([3000, 2000, 2500, 1500, 1800, 2800]) #salary in $ pt.barh(emp_name,emp_salary) pt.show()

Utilize the barh() function to display the following graph:

Example: 

import matplotlib.pyplot as pt import numpy as npy student_name = npy.array(["Liza", "Micheal","Shakira","Mike", "Robert", "Jessica", "Max", 'Eleven']) student_cgpa = npy.array([3.3, 2.2, 3.4,4,2.3, 3.1, 3.9, 2.5]) pt.barh(student_name,student_cgpa) pt.show()

Bar Color

To customize the color of Matplotlib bars, invoke bar() or barh() functions with the keyword argument color:

Assign the magenta color to the following bar graph:

Example: 

import matplotlib.pyplot as pt import numpy as npy sports = npy.array(["Football", "Cricket","Field Hockey","Tennis"]) ratio = npy.array([3.5, 2.5, 2, 1]) #estimated fans ratio in billion pt.bar(sports,ratio, color = 'm') pt.show()

First apply black color to all bars then set different edge colors on bars:

Example: 

import matplotlib.pyplot as pt import numpy as npy leagues = npy.array(["Premier League", "La Liga", "Bundesliga", "Serie A", "Ligue 1"]) ratings = npy.array([93.2, 91.2, 90.4, 89.4, 85.4]) ec = ['yellow', 'green', 'pink', 'red', 'blue'] pt.bar(leagues,ratings, color = 'k', edgecolor = ec, linewidth = 5) pt.show()

Color Names

You can also utlize 140 supported color names which defined in matplotlib Bars document.

Utilize the keyword argument color = ‘Maroon‘:

Example: 

import matplotlib.pyplot as pt import numpy as npy sports = npy.array(["Football", "Cricket","Field Hockey","Tennis"]) ratio = npy.array([3.5, 2.5, 2, 1]) #estimated fans ratio in billion pt.bar(sports,ratio, color = 'Maroon') pt.show()

Implement ‘Linen’ color on all bars then apply different edge colors to the following bars:

Example: 

import matplotlib.pyplot as pt import numpy as npy leagues = npy.array(["Premier League", "La Liga", "Bundesliga", "Serie A", "Ligue 1"]) ratings = npy.array([93.2, 91.2, 90.4, 89.4, 85.4]) ec = ['Maroon', 'MidnightBlue', 'LightSkyBlue', 'DarkSlateGray', 'DarkOrange'] pt.bar(leagues,ratings, color = 'Linen', edgecolor = ec, linewidth = 5) pt.show()

Color Hex

Alternatively, you can also work with Hexadecimal color values:

Insert five bars with a hexadecimal color ‘#ffb399‘:

Example: 

import matplotlib.pyplot as pt import numpy as npy sports = npy.array(["Football", "Cricket","Field Hockey","Tennis"]) ratio = npy.array([3.5, 2.5, 2, 1]) #estimated fans ratio in billion pt.bar(sports,ratio, color = '#ffb399') pt.show()

Utilize the linewidth argument along with the edgecolor argument:

Example: 

import matplotlib.pyplot as pt import numpy as npy leagues = npy.array(["Premier League", "La Liga", "Bundesliga", "Serie A", "Ligue 1"]) ratings = npy.array([93.2, 91.2, 90.4, 89.4, 85.4]) ec = ['#29293d', '#3d3d5c', '#52527a', '#666699', '#8585ad'] pt.bar(leagues,ratings, color = '#c2c2d6', edgecolor = ec, linewidth = 5) pt.show()

Bar Width

According to Matplotlib bars, the bar() function accepts the keyword argument width to define the width:

In the below example utilize the width argument:

Example: 

import matplotlib.pyplot as pt import numpy as npy emp_name = npy.array(["Harry", "Kate", "Tim", "Sarah", "Dustin", 'Jack']) emp_salary = npy.array([3000, 2000, 2500, 1500, 1800, 2800]) #salary in $ pt.bar(emp_name,emp_salary, width = 0.4) pt.show()

Assign a particular width to all bars and apply a different color to each bar:

Example: 

import matplotlib.pyplot as pt import numpy as npy student_name = npy.array(["Liza", "Micheal","Shakira","Mike", "Robert", "Jessica", "Max", 'Eleven']) student_cgpa = npy.array([3.3, 2.2, 3.4,4,2.3, 3.1, 3.9, 2.5]) c = npy.array(['red', 'green', 'blue','yellow','magenta', 'black', 'gray', 'brown']) pt.bar(student_name,student_cgpa, width = 0.3, color = c) pt.show()

By default, width is set to 0.8

Reminder: Consider height in place of width for horizontal bars.

Bar Height

According to Matplotlib bars, the bar() function accepts the keyword argument height to define the height:

Apply the width argument in the following example:

Example: 

import matplotlib.pyplot as pt import numpy as npy emp_name = npy.array(["Harry", "Kate", "Tim", "Sarah", "Dustin", 'Jack']) emp_salary = npy.array([3000, 2000, 2500, 1500, 1800, 2800]) #salary in $ pt.barh(emp_name,emp_salary, height = 0.2) pt.show()

Provide a specific width to all bars and implement a distinct color to every bar:

Example: 

import matplotlib.pyplot as pt import numpy as npy student_name = npy.array(["Liza", "Micheal","Shakira","Mike", "Robert", "Jessica", "Max", 'Eleven']) student_cgpa = npy.array([3.3, 2.2, 3.4,4,2.3, 3.1, 3.9, 2.5]) c = npy.array(['red', 'green', 'blue','yellow','magenta', 'black', 'gray', 'brown']) pt.barh(student_name,student_cgpa, height = 0.5, color = c) pt.show()

By default, height is set to 0.8

Example Explanation

In above example we have used Matplotlib library to create a horizontal bar chart that displays the CGPA of eight students. The code does the following:

  • It creates an array named student_name that contains the names of the students, and an array named student_cgpa that contains the CGPA of each student.
  • It creates an array named c with eight different color values that will be used to color the bars of the horizontal bar chart.
  • It uses the barh function of the pyplot module to create the horizontal bar chart. The function takes student_name, student_cgpa, height and color as parameters.
  • The height parameter represents the height of the bars, and the color parameter is used to color the bars.
  • Finally, the show function of the pyplot module is called to display the bar chart on the screen. The horizontal bar chart shows the CGPA of the eight students with bars of different colors for each student, where the length of the bar indicates the CGPA of the student.

Benefits

Matplotlib bars offer several advantages, which include:

  • Matplotlib bars make it easy to visualize data in a simple and understandable format. Bar graphs are especially useful for displaying categorical data as they allow you to see patterns, trends, and comparisons among groups.
  • Matplotlib bars are highly customizable. You can modify the colors, widths, and orientations of the bars as well as add labels, titles, and legends to the graph.
  • Matplotlib bars are compatible with Python, which is a widely used programming language for data analysis and science. This makes it easy for programmers and data analysts to use the library to create high-quality visualizations.
  • Matplotlib bars can be made interactive by using widgets and sliders, allowing users to modify the graph parameters and see the effects in real-time.
  • Matplotlib provides a wide range of graph types, including stacked bar graphs, horizontal bar graphs, and grouped bar graphs, making it possible to display data in a variety of formats. This allows for greater flexibility in presenting data and catering to different types of audiences.

Conclusion

Matplotlib bars provide several advantages for data analysis and visualization. They offer a powerful means of presenting categorical data in a straightforward and comprehensible manner, and their flexibility allows for extensive customization to fit varying preferences and requirements. Furthermore, Matplotlib bars integrate well with Python, making them widely used among programmers and data analysts.

The availability of interactive features and a diverse array of graph types further enhances their usefulness, making Matplotlib bars an ideal tool for effectively communicating data insights.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *