w3resource

Matplotlib Bar Chart: Display a bar chart of the popularity of programming Languages and specify the position of each bar plot

Matplotlib Bar Chart: Exercise-7 with Solution

Write a Python programming to display a bar chart of the popularity of programming Languages. Specify the position of each bar plot.

Sample data:
Programming languages: Java, Python, PHP, JavaScript, C#, C++
Popularity: 22.2, 17.6, 8.8, 8, 7.7, 6.7

Sample Solution:

Python Code:

import matplotlib.pyplot as plt
x = ['Java', 'Python', 'PHP', 'JavaScript', 'C#', 'C++']
popularity = [22.2, 17.6, 8.8, 8, 7.7, 6.7]
x_pos = [i for i, _ in enumerate(x)]
plt.xlabel("Languages")
plt.ylabel("Popularity")
plt.title("PopularitY of Programming Language\n" + "Worldwide, Oct 2017 compared to a year ago")
plt.xticks(x_pos, x)
# Select the position of each barplots on the x-axis (space=1,3,3,2,1)
y_pos = [0,1,4,7,9,10]
# Create bars
plt.bar(y_pos, popularity)
# Turn on the grid
plt.minorticks_on()
plt.grid(which='major', linestyle='-', linewidth='0.5', color='red')
# Customize the minor grid
plt.grid(which='minor', linestyle=':', linewidth='0.5', color='black')
plt.show()

Sample Output:

Matplotlib BarChart: Display a bar chart of the popularity of programming Languages and specify the position of each barplot

Python Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Python programming to display a bar chart of the popularity of programming Languages. Make blue border to each bar.
Next: Write a Python programming to display a bar chart of the popularity of programming Languages. Select the width of each bar and their positions.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/graphics/matplotlib/barchart/matplotlib-barchart-exercise-7.php