Bar chart

To chart the values 25, 5, 150, and 100 into a bar graph, we will store the values in an array and pass it to the bar function. The bars in the graph represent the magnitude along the y-axis:

    import matplotlib.pyplot as plt 

data = [25., 5., 150., 100.]
x_values = range(len(data))
plt.bar(x_values, data)

plt.show()

x_values stores an array of values generated by range(len(data)). Also, x_values will determine the points on the x-axis where the bars will be drawn. The first bar will be drawn on the x-axis where x is zero. The second bar with data 5 will be drawn on the x-axis where x is 1:

The width of each bar can be changed by modifying the following line:

    plt.bar(x_values, data, width=1.)  

This should produce the following graph:

However, this is not visually appealing because there is no space between the bars anymore, which makes it look clumsy. Each bar now occupies one unit on the x-axis.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset