Python hosting : Host, run, and code Python in the cloud!
Matplotlib may be used to create bar charts. You might like the Matplotlib gallery .
Matplotlib is a python library for visualizing data. You can use it to create bar charts in python. Installation of matplot is on pypi, so just use pip: pip install matplotlib
The course below is all about data visualization:
Related course: Data Visualization with Matplotlib and Python
Bar chart code A bar chart shows values as vertical bars, where the position of each bar indicates the value it represents. matplot aims to make it as easy as possible to turn data into Bar Charts.
A bar chart in matplotlib made from python code. The code below creates a bar chart:
import matplotlib.pyplot as plt; plt.rcdefaults()import numpy as npimport matplotlib.pyplot as pltobjects = ('Python' , 'C++' , 'Java' , 'Perl' , 'Scala' , 'Lisp' ) y_pos = np.arange(len (objects)) performance = [10 ,8 ,6 ,4 ,2 ,1 ] plt.bar(y_pos, performance, align='center' , alpha=0.5 ) plt.xticks(y_pos, objects) plt.ylabel('Usage' ) plt.title('Programming language usage' ) plt.show()
Output:
Python Bar Chart
Matplotlib charts can be horizontal, to create a horizontal bar chart:
import matplotlib.pyplot as plt; plt.rcdefaults()import numpy as npimport matplotlib.pyplot as pltobjects = ('Python' , 'C++' , 'Java' , 'Perl' , 'Scala' , 'Lisp' ) y_pos = np.arange(len (objects)) performance = [10 ,8 ,6 ,4 ,2 ,1 ] plt.barh(y_pos, performance, align='center' , alpha=0.5 ) plt.yticks(y_pos, objects) plt.xlabel('Usage' ) plt.title('Programming language usage' ) plt.show()
Output:
Bar chart horizontal
Bar chart comparison You can compare two data series using this Matplotlib code:
import numpy as npimport matplotlib.pyplot as pltn_groups = 4 means_frank = (90 , 55 , 40 , 65 ) means_guido = (85 , 62 , 54 , 20 ) fig, ax = plt.subplots() index = np.arange(n_groups) bar_width = 0.35 opacity = 0.8 rects1 = plt.bar(index, means_frank, bar_width, alpha=opacity, color='b' , label='Frank' ) rects2 = plt.bar(index + bar_width, means_guido, bar_width, alpha=opacity, color='g' , label='Guido' ) plt.xlabel('Person' ) plt.ylabel('Scores' ) plt.title('Scores by person' ) plt.xticks(index + bar_width, ('A' , 'B' , 'C' , 'D' )) plt.legend() plt.tight_layout() plt.show()
Output:
Python Bar Chart comparison
Stacked bar chart The example below creates a stacked bar chart with Matplotlib. Stacked bar plots show diffrent groups together.
import matplotlib.pyplot as pltx = ['A' , 'B' , 'C' , 'D' ] y1 = [100 , 120 , 110 , 130 ] y2 = [120 , 125 , 115 , 125 ] plt.bar(x, y1, color='g' ) plt.bar(x, y2, bottom=y1, color='y' ) plt.show()
Output:
Download All Matplotlib Examples
Leave a Reply:
Thank you......You made my day..... :).......