CREATING BAR CHARTS
A Bar Graph or a Bar Chart is a graphical display of data using bars of different heights. A bar chart can be drawn vertically or horizontally using rectangles or bars of different heights/widths. PyPlot offers bar() or barh() functions to create a bar chart where you can specify the sequence for x-axis and corresponding sequence to be plotted on y-axis. Each y-value is plotted as bar on corresponding x-value on x-axis.
Example of Bar Chart:
If we have three sequence a, b, and c as:
Value
of a and b
import
numpy as np import
matplotlib.pyplot as plt a,b,c
= [1,2,3,4],[2,4,6,8],[1,4,9,16] plt.bar(a,b) plt.show() |
|
Value
of a and c
import
numpy as np import
matplotlib.pyplot as plt a,b,c
= [1,2,3,4],[2,4,6,8],[1,4,9,16] plt.bar(a,c) plt.show() |
|
You see that the first sequence given in bar() forms
the x-axis and the second sequence values are plotted on y-axis.
The simple bar graph is best used when there is just
one level of grouping to your variable.
Other Example of Bar Graph:
Q. Create a bar graph according to city and their
population.
Cities = [‘Delhi’, ‘Mumbai’, ‘Bangalore’, ‘Hyderabad’]
Population = [34565456, 30043563, 18765678, 13456117]
Solution:
import numpy as np import matplotlib.pyplot as plt Cities
= ['Delhi', 'Mumbai', 'Bangalore', 'Hyderabad'] Population
= [21265456, 30043563, 18765678, 23456117] plt.bar(Cities, Population) plt.xlabel('Cities') plt.ylabel('Population') plt.show() |
|
In the above example the to show the default color and
also you see the x-axis and y-axis according to the data. And Width is default.
Changing Width of the Bars in Bar Chart:
import numpy as np import
matplotlib.pyplot as plt Cities
= ['Delhi', 'Mumbai', 'Bangalore', 'Hyderabad'] Population
= [21265456, 30043563, 18765678, 23456117] plt.bar(Cities,
Population, width = 1/2) OR plt.bar(Cities,
Population, width = 0.5) #any other no plt.xlabel('Cities') plt.ylabel('Population') plt.show() |
|
If you specify a scalar value (a single value) for
width argument, then that width is applied to all the bars of the bar chart.
If we use the different values for width then the
width will be change:
import numpy as np import
matplotlib.pyplot as plt Cities
= ['Delhi', 'Mumbai', 'Bangalore', 'Hyderabad'] Population
= [21265456, 30043563, 18765678, 23456117] plt.bar(Cities,
Population, width = [0.5, .6, .7, 0.8]) plt.xlabel('Cities') plt.ylabel('Population') plt.show() |
|
Changing Colors of the Bars in a Bar Chart: You
can always change the color of the bars, single or multiple.
import
numpy as np import
matplotlib.pyplot as plt Cities = ['Delhi', 'Mumbai', 'Bangalore', 'Hyderabad'] Population
= [21265456, 30043563, 18765678, 23456117] plt.bar(Cities, Population, width = 0.5, color = ['red', 'g', 'black', 'b']) OR plt.bar(Cities,
Population, width = 0.5, color = ‘red’) # for single color in bar graph plt.xlabel('Cities') plt.ylabel('Population') plt.show() |
|
Horizontal Bar Graph:
import numpy as np import
matplotlib.pyplot as plt Cities = ['Delhi', 'Mumbai', 'Bangalore', 'Hyderabad'] Population
= [21265456, 30043563, 18765678, 23456117] plt.barh(Cities, Population, color = ['red', 'g', 'black', 'b']) plt.ylabel('Cities') plt.xlabel('Population') plt.show() |
|
To create a horizontal bar graph, you need to use
barh() function, in place of bar(). Also you need to give x and y axis labels
carefully.
Creating Multiple Bars Chart: In
real life, you may need to plot multiple data ranges on the same bar chart
creating multiple bars. As such PyPlot does not provide a specific function for
this, but you can always create one exploiting the width and color arguments of
bar().
Following example also creates a multiple bar chart plotting
three different data ranges.
Q. Val is a list having three lists inside it. It
contains summarized data of three different trials conducted by company A.
Create a bar chart that plots these three sub lists of Val in a single chart.
Keep the width of each bar as 0.25.
Solution:
import
numpy as np import matplotlib.pyplot as plt val
= [[5,18,35,15],[3,32,41,15],[7,25,42,17]] x=np.arange(4) plt.bar(x+0.00,val[0],color='b',width=0.25,
label='range1') plt.bar(x+0.25,val[1],color='r',width=0.25, label='range2') plt.bar(x+0.50,val[2],color='g',width=0.25, label='range3') plt.legend(loc
= 'upper left') plt.show() |
|
In the above example add legend for multiple data. We
plot multiple ranges on a single plot, it becomes necessary that legends are specified.
Recall that legend is a color or mark linked to a specific data range plotted.
Setting Xlimits and Ylimits:
If you may need to have own limits specified for x and y axes. For this, you
can use xlim() and ylim() functions to set limits for x-axis and y-axis
respectively.
Example:
import
numpy as np import
matplotlib.pyplot as plt a =
np.arange(4) b =
[5, 25, 45, 20] plt.xlim(-2.0,
4.0) #xlimits changed plt.bar(a,b) plt.title("Bar
Chart") plt.show() |
|
Setting Ticks for Axes: By
default, PyPlot will automatically decide which data points will have ticks on
the axes, but you can also decide which data points will have tick marks on x
and y axes.
Example: Takshila
School celebrated volunteering week where each section of class XI dedicated a
day for collecting amount for charity being supported by the school. Section A
volunteered on Monday, B on Tuesday, C on Wednesday and so on. There are six
sections in class XI. Amount collected by section A to F are 8000, 9000, 10000,
11500, 12500, 7500.
(i) Create a bar chart showing collection amount.
(ii) Plot the collected amount vs days using a bar
chart.
(iii) Plot the collected amount vs sections using a
bar chart.
Solution:
(i) import
numpy as np import matplotlib.pyplot as plt col
= [8000, 9000, 10000, 11500, 12500, 7500] x =
np.arange(6) # range to signify 6
days plt.title("Volunteering
Week Collection") plt.bar(x, col, color = 'g', width = 0.5) plt.show() |
|
(ii) import
numpy as np import
matplotlib.pyplot as plt col = [8000, 9000, 10000, 11500, 12500, 7500] x =
np.arange(6) # range to signify 6
days plt.title("Volunteering
Week Collection") plt.bar(x,
col, color = 'r', width = 0.5) plt.xticks(x,['Mon','Tue',
'Wed', 'Thu', 'Fri', 'Sat']) plt.xlabel("Days") plt.ylabel("Collection") plt.show() Note: See, tick label specified with additional sequence in xticks() |
|
(iii) import
numpy as np import
matplotlib.pyplot as plt col
= [8000, 9000, 10000, 11500, 12500, 7500] x =
np.arange(6) # range to signify 6
days plt.title("Volunteering
Week Collection") plt.bar(x,
col, color = 'olive', width = 0.5) plt.xticks(x,['A','B',
'C', 'D', 'E', 'F']) plt.xlabel("Sections") plt.ylabel("Collection") plt.show() Note: See, tick label specified with additional sequence in xticks() |
|
Example: Create
multiple line charts on common plot where three data ranges are plotted on same
chart. The data ranges to be plotted is/are:
Data = [[5, 30, 40, 25], [3, 13, 27, 31], [7, 28, 25, 40]]
Solution:
import
numpy as np import
matplotlib.pyplot as plt Data
= [[5,30,40,25], [3,13,27,31], [7,28,25,40]] X =
np.arange(4) plt.plot(X,
Data[0], color = 'r', label = 'range1') plt.plot(X,
Data[1], color = 'g', label = 'range2') plt.plot(X,
Data[2], color = 'b', label = 'range3') plt.legend(loc
= 'upper left') plt.title("Multi
Range Line chart") plt.xlabel('X') plt.ylabel('Y') plt.show() |
|
Shubham Verma
ReplyDelete