Naive_Natural2511
Naive_Natural2511

Reputation: 717

Plotting bar chart in python

I am new to Python programming. I am learning Python.

The below code helped me plot a bar chart. I am trying to understand the code. I could not understand the lines 5,6,7 and 8. i.e.,

N = len(data)
x = np.arange(1,N+1)
y = [num for (s, num) in data ]
labels = [ s for (s, num) in data ]

Also, why are we taking x+width/2.0 while plotting x axis labels? And, how to bring a small width at the start of the graph before House Theft? The bar usually starts with 0. I am not sure how to bring a small width before the start of the first bar. I tried, but it is not coming properly.

The full program is as follows.

import matplotlib.pyplot as plt
import numpy as np
data = [ ("House Theft", 57), ("House Fire", 48),
            ("Car Theft", 156), ( "Car Accident", 245)]
N = len(data)
x = np.arange(1,N+1)
y = [num for (s, num) in data ]
labels = [ s for (s, num) in data ]
width = 0.35 #Use 1 to make it as a histogram
bar1 = plt.bar( x, y, width, color="y")
plt.ylabel( 'Frequency' )
plt.xticks(x + width/2.0, labels )
plt.show()

Upvotes: 1

Views: 2517

Answers (2)

wwii
wwii

Reputation: 23743

Multiple assignment, tuple/sequence packing/unpacking:

>>> 
>>> data = [ ("House Theft", 57), ("House Fire", 48),
            ("Car Theft", 156), ( "Car Accident", 245)]
>>> 
>>> for thing in data:
    (s, num) = thing
    print thing, '\t', s, '\t', num

('House Theft', 57)     House Theft     57
('House Fire', 48)      House Fire      48
('Car Theft', 156)      Car Theft       156
('Car Accident', 245)   Car Accident    245
>>>

>>> for (s, num) in data:
    print s, '\t\t', num


House Theft         57
House Fire          48
Car Theft           156
Car Accident        245
>>>

plt.xticks(x + width/2.0, labels ) will offset the ticks on the x axis by on-half of the width. Not sure why it was done, except maybe for the visual effect.

>>> x = np.arange(1,11)
>>> x
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
>>> width = .5
>>> x + width/2
array([  1.25,   2.25,   3.25,   4.25,   5.25,   6.25,   7.25,   8.25,   9.25,  10.25])
>>> 

Upvotes: 1

charlestati
charlestati

Reputation: 78

  • N = len(data)

The value of N is now the length of the array data. In your case, the length of data is 4.

  • x = np.arange(1,N+1)

The value of x is now [1, 2, 3, 4], see this example from the doc:

>>> np.arange(3)
array([0, 1, 2])
>>> np.arange(3.0)
array([ 0.,  1.,  2.])
>>> np.arange(3,7)
array([3, 4, 5, 6])
>>> np.arange(3,7,2)
array([3, 5])
  • y = [num for (s, num) in data ]

The value of y is [57, 48, 156, 245].

for (s, num) in data iterates the values of data. Since the values of data have two parts ("House Theft", 57), for each loop s takes the value of the first part ("House Theft" for the first loop) and num the value of the second part (57 for the first loop). Since you only want the numbers (the second part), num for (s, num) in data only takes the num and your array is then filled with these since the expression is between brackets [].

It creates an array from the "result" of the expression num for (s, num) in data.

  • labels = [ s for (s, num) in data ]

Same as before but with the strings instead of the value.

I still have confusions with nomenclature (array, tuple, list…), if someone could check my answer I'd appreciate it as it'd help both the author and me to learn the correct Python vocabulary!

Upvotes: 0

Related Questions