In this Matplotlib Python tutorial we want to learn about Matplotlib Python Bar Chart, so Matplotlib is widely used in the Python data science ecosystem for its easiness. It offers different customization and allows you to create visually appealing and informative charts. With Matplotlib, you have complete control over different elements of your chart, such as labels, colors and axes.
First of all we need to install Python Matplotlib and you can use pip for that.
1 |
pip install matplotlib |
This is the complete code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
import matplotlib.pyplot as plt # Sample data categories = ['Apples', 'Bananas', 'Oranges'] counts = [25, 40, 30] # Creating a basic bar chart plt.bar(categories, counts) plt.show() # Customizing the bar chart plt.bar(categories, counts) plt.xlabel('Categories') plt.ylabel('Counts') plt.title('Fruit Counts') plt.show() # Changing colors plt.bar(categories, counts, color=['red', 'green', 'orange']) plt.show() # Adjusting the figure size plt.figure(figsize=(8, 6)) plt.bar(categories, counts) plt.show() # Rotating the x-axis labels plt.bar(categories, counts) plt.xticks(rotation=45) plt.show() # Adding annotations plt.bar(categories, counts) for i, count in enumerate(counts): plt.text(i, count + 1, str(count), ha='center') plt.show() # Creating a horizontal bar chart plt.barh(categories, counts) plt.show() # Creating a stacked bar chart additional_counts = [10, 20, 15] plt.bar(categories, counts, label='Initial Counts') plt.bar(categories, additional_counts, label='Additional Counts', bottom=counts) plt.legend() plt.show() |
Now let’s break down this code, To begin, we need to import the necessary libraries and generate some sample data:
1 2 3 4 5 |
import matplotlib.pyplot as plt # Sample data categories = ['Apples', 'Bananas', 'Oranges'] counts = [25, 40, 30] |
After that we can create a simple bar chart using the bar function provided by Matplotlib:
1 2 |
plt.bar(categories, counts) plt.show() |
Python Matplotlib Customizing the Bar Chart
Matplotlib allows us to customize different aspects of our bar chart. Let’s explore some common customizations:
In here we are adding label and titles to our bar chart.
1 2 3 4 5 |
plt.bar(categories, counts) plt.xlabel('Categories') plt.ylabel('Counts') plt.title('Fruit Counts') plt.show() |
This code will change the color of matplotlib bar chart.
1 2 |
plt.bar(categories, counts, color=['red', 'green', 'orange']) plt.show() |
In here we can adjusting the figure size
1 2 3 |
plt.figure(figsize=(8, 6)) plt.bar(categories, counts) plt.show() |
In here we are rotating the x-axis labels:
1 2 3 |
plt.bar(categories, counts) plt.xticks(rotation=45) plt.show() |
Matplotlib also supports interactive features, and it allows users to explore and interact with the data. We can utilize these features to enhance our bar chart:
In here we can add annotations
1 2 3 4 |
plt.bar(categories, counts) for i, count in enumerate(counts): plt.text(i, count + 1, str(count), ha='center') plt.show() |
Using this code you can create horizontal bar chart
1 2 |
plt.barh(categories, counts) plt.show() |
with this code we can create stacked bar chart
1 2 3 4 5 |
additional_counts = [10, 20, 15] plt.bar(categories, counts, label='Initial Counts') plt.bar(categories, additional_counts, label='Additional Counts', bottom=counts) plt.legend() plt.show() |
Run the complete code and this will be the result
