MATPLOTLIB Tutorial Series | Part 2

Note: This article has also featured on geeksforgeeks.com .

This is the 2nd article in the series which covers graphing in python using matplotlib.

This article will cover different types of plots that we can make using matplotlib.

1. Bar Chart

import matplotlib.pyplot as plt

# x-coordinates of left sides of bars
left = [1, 2, 3, 4, 5]

# heights of bars
height = [10, 24, 36, 40, 5]

# labels for bars
tick_label = ['one', 'two', 'three', 'four', 'five']

# plotting a bar chart
plt.bar(left, height, tick_label = tick_label,
        width = 0.8, color = ['red', 'blue'])

# naming the x-axis
plt.xlabel('x - axis')
# naming the y-axis
plt.ylabel('y - axis')
# plot title
plt.title('My bar chart!')

# function to show the plot
plt.show()

Output of above program looks like this:

mp4

  • Here, we use plt.bar() function to plot a bar chart.
  • x-coordinates of left side of bars are passed along with heights of bars.
  • you can also give some name to x-axis coordinates by defining tick_labels

2. Histogram

import matplotlib.pyplot as plt

# frequencies
ages = [2,5,70,40,30,45,50,45,43,40,44,
        60,7,13,57,18,90,77,32,21,20,40]

# setting the ranges and no. of intervals
range = (0, 100)
bins = 10  

# plotting a histogram
plt.hist(ages, bins, range, color = 'red',
        histtype = 'bar', rwidth = 0.8)

# x-axis label
plt.xlabel('age')
# frequency label
plt.ylabel('No. of people')
# plot title
plt.title('My histogram')

# function to show the plot
plt.show()

Output of above program looks like this:

mp5

  • Here, we use plt.hist() function to plot a histogram.
  • frequencies are passed as the ages list.
  • Range could be set by defining a tuple containing min and max value.
  • Next step is to “bin” the range of values—that is, divide the entire range of values into a series of intervals—and then count how many values fall into each interval. Here we have defined bins = 10. So, there are a total of 100/10 = 10 intervals.

3. Scatter plot

import matplotlib.pyplot as plt

# x-axis values
x = [1,2,3,4,5,6,7,8,9,10]
# y-axis values
y = [2,4,5,7,6,8,9,11,12,12]

# plotting points as a scatter plot
plt.scatter(x, y, label= "stars", color= "m",
            marker= "*", s=30)

# x-axis label
plt.xlabel('x - axis')
# frequency label
plt.ylabel('y - axis')
# plot title
plt.title('My scatter plot!')
# showing legend
plt.legend()

# function to show the plot
plt.show()

Output of above program looks like this:

mp6

  • Here, we use plt.scatter() function to plot a scatter plot.
  • Like a line, we define x and corresponding y – axis values here as well.
  • marker argument is used to set the character to use as marker. Its size can be defined using s parameter.

4. Pie-chart

import matplotlib.pyplot as plt

# defining labels
activities = ['eat', 'sleep', 'work', 'play']

# portion covered by each label
slices = [3, 7, 8, 6]

# color for each label
colors = ['r', 'm', 'g', 'b']

# plotting the pie chart
plt.pie(slices, labels = activities, colors=colors,
        startangle=90, shadow = True, explode = (0, 0, 0.1, 0),
        radius = 1.2, autopct = '%1.1f%%')

# plotting legend
plt.legend()

# showing the plot
plt.show()

Output of above program looks like this:

mp7

  • Here, we plot a pie chart by using plt.pie() method.
  • First of all, we define the labels using a list called activities.
  • Then, portion of each label can be defined using another list called slices.
  • Color for each label is defined using a list called colors.
  • shadow = True will show a shadow beneath each label in pie-chart.
  • startangle rotates the start of the pie chart by given degrees counterclockwise from the x-axis.
  • explode is used to set the fraction of radius with which we offset each wedge.
  • autopct is used to format the value of each label. Here, we have set it to show the percentage value only upto 1 decimal place.

5. Plotting curves of given equation

# importing the required modules
import matplotlib.pyplot as plt
import numpy as np

# setting the x - coordinates
x = np.arange(0, 2*(np.pi), 0.1)
# setting the corresponding y - coordinates
y = np.sin(x)

# potting the points
plt.plot(x, y)

# function to show the plot
plt.show()

Output of above program looks like this:
mp8

Here, we use NumPy which is a general-purpose array-processing package in python.

  • To set the x – axis values, we use np.arange() method in which first two arguments are for range and third one for step-wise increment. The result is a numpy array.
  • To get corresponding y-axis values, we simply use predefined np.sin() method on the numpy array.
  • Finally, we plot the points by passing x and y arrays to the plt.plot() function.

So, in this part, we discussed various types of plots we can create in matplotlib. There are many more inbuilt plots which haven’t been covered but the most significant ones have been discussed here.

Next parts will cover subplots, styles, basemaps, 3D plotting, etc.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s