Bar charts

A bar chart is a chart that represents your data in rectangular bars. You can plot them vertically or horizontally. Create a script called  bar_chart.py and write the following content in it:

import matplotlib.pyplot as plt
from matplotlib import style

style.use('ggplot')

x1 = [4,8,12]
y1 = [12,16,6]
x2 = [5,9,11]
y2 = [6,16,8]

plt.bar(x1,y1,color = 'g',linewidth=3)
plt.bar(x2,y2,color = 'r',linewidth=3)
plt.title("Bar plot")

plt.xlabel("x axis")
plt.ylabel("y axis")

plt.show()

Run the script and you will get the following output:

student@ubuntu:~/work$ python3 bar_chart.py

The output is as follows:

In the preceding example, we have two sets of values: x1, y1 and x2, y2. After getting the numerical data, we used the plt.bar() method to plot the bar chart for the present data.

There are multiple techniques available to plot the data. Among them, there are a few techniques or methods of data visualization using matplotlib, which we have seen. We can also perform such operations using another tool of data visualization: plotly.

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

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