Tuesday, 16 February 2021

3.2 Creating Bar Charts in matplotlib

 


Creating Bar Charts:

A Bar chart or a Bar Graph 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 heights/widths PyPlot offers bar() function to create vertical bar chart and barh() function for horizontal bar.

 

a, b = [1,2,3,4], [2,4,6,8]

plt.bar(a,b)

plt.xlabel(“Values”)

plt.ylabel(“Doubles”)

plt.show()

 

Changing Widths of the Bar:

Default width of a bar is 0.8 units

#Same Width for all bar

plt.bar(x,y,width=<float value>)                              

For different width of all bar

width = [0.5, 0.6, 0.7, 0.8]

 

Changing Color of the Bar:

Same color for all bar

plt.bar(x, y, color=<color code / name>)

Different color for all bar

color=[‘red’,’b’,’g’,’black’]

 

Assignment - 3

1.       Create a bar chart which display the name of four cities on x axis and population of those cities on y axis. 

2.       Create a bar chart which display the types of medals (Gold, Silver, Bronze, Total) on x axis and number of medals won by India.

3.       Create a bar chart which display the types of medals (Gold, Silver, Bronze, Total) on x axis and number of medals won by India and medals won by Australia in Same chart.

4.       Create a bar chart which display the types of medals (Gold, Silver, Bronze, Total) on x axis and number of medals won by India with different bar width and different color.

5.       Val is a list having three lists inside it. It contains summarized data of three different trials conducted by a company. Create a bar chart that plots these subtitles of Val in a Single chart. Keep the width of each bar as 0.25

 


No comments:

Post a Comment