Python matplotlib – 2

In this article:

  1. Plotting more sets of data in the same graph
  2. A graph with categorical variable

1. Plotting more sets of data in the same graph

Lets plot two functions
y1=x2 + 1, y2= x3+2*t
X values are between 0 and 10

Code which solve the problem:

import numpy as np
import matplotlib.pyplot as myplot
t = np.arange(0., 10., 0.25)
myplot.plot( t, t**2+1, 'r>') myplot.plot( t, t**3+2*t, 'b*')
myplot.show()

Code explanation:
t = np.arange(0., 10., 0.25)
this generate x coordinate values. First argument 0. is the start value.
Second argument 10. is the end value. Third argument is the step between value.
Means generated values by np.arange are 0., 0.25, 0.50, 0.75, 1, 1.25 etc.

Plot function have 3 arguments this time, first two was explained in previous post.
Third argument is plot "format string", which describe colour used for draw and character.
Thus for myplot.plot( t, t**2+1, 'r>')
third argument 'r>' will draw plot in red colour ('r') using triangle_right marker ('>).
Documentation about all markers supported by plot are in matplotlib.pyplot.plot, section "Format Strings".

Is important to retain that in plot "format string" first character is plot colour and the second character is type of marker.

For myplot.plot( t, t*3+2t, 'b') third argument 'b' will draw plot in blue ('b') and the marker is '*'

Output from code is in below image:

Plotting more sets of data in the same graph

2. A graph with categorical variables

Sample problem for this case: we have population
spread in 4 group ages:
years_18_30, medium income is 2800
years_31_45, medium income is 4100
years_46_60, medium income is 4500
years_60_plus, medium income is 3800
We need to plot categorical variable age/income for this.

Code is:

import matplotlib.pyplot as myplot
ages=['year_18_30', 'year_31_45', 'year_46_60', 'year_60_plus']
income = [2800, 4100, 4500, 3800]
myplot.figure(figsize=(5, 5))
myplot.subplot(111)
myplot.bar(ages, income)
myplot.show()

This time graph is draw in a figure, created by
myplot.figure(figsize=(5, 5))
Size of figure is width 5 inches, heigh 5 inches.

Graph is a bar graph type, draw with
myplot.bar(ages, income)

In code Similar output we used figure and subplot, similar output is obtained
without those with code:

import matplotlib.pyplot as myplot
ages=['year_18_30', 'year_31_45', 'year_46_60', 'year_60_plus']
income = [2800, 4100, 4500, 3800]
myplot.bar(ages, income)
myplot.show()

Python matplotlib – 1

In this article:

  1. A simple graphic with pyplot
  2. Using lists for graphic coordinates
  3. A graphic with many lines
  4. Using tuples for graphic coordinates
  5. What happen when lists or tuples for coordinates have different size?
  6. Save graphic in a file

1. A simple graphic with pyplot

Most simple graphic with matplotlib is plotting with pyplot interface:

import matplotlib.pyplot as myplot
myplot.plot([1, 5], [2, 12])
myplot.show()

This show graphic like below:

Simple graphic with mathplotlib pyplot.

From image result that pyplot draw a line between two points (1,2) and (5,12)

2. Using lists for graphic coordinates

Arguments for "plot" in myplot.plot([1, 5], [2, 12]) are 2 lists:
l1 = [1, 5]
l2 = [2, 12]

The same graphic is obtained using:

import matplotlib.pyplot as myplot
l1 = [1, 5]
l2 = [2, 12]
myplot.plot(l1, l2)
myplot.show()

In this case line draw is between
point 1 with coordinates:
x=first element of list l1, or l1[0]
y=first element of list l2, or l2[0]
point 2 with coordinates:
x=second element of list l1, or l1[1]
y=second element of list l2, or l2[1]

3. A graphic with many lines

If lists l1 and l2 have more elements, plot will draw line(es) between appropriate points. For example:

import matplotlib.pyplot as myplot
l1 = [1, 3, 5]
l2 = [2, 4, 12]
myplot.plot(l1, l2)
myplot.show()

Code will draw:

A pyplot graphic with many lines

In this case lines are between points that have coordinates:
(1,2), (3,4), (5,12)
or
(l1[0],l2[0]), (l1[0],l2[0]), (l1[0],l2[0])

4. Using tuples for graphic coordinates

The same graphic is obtained if we use tuples, i.e. code:

import matplotlib.pyplot as myplot
t1 = (1, 3, 5)
t2 = (2, 4, 12)
myplot.plot(t1, t2)
myplot.show()

Lists or tuples can have more elements, it is important that those to have
the same number of elements.

5. What happen when lists or tuples for coordinates have different size?

If we try with different elements, for example code:

import matplotlib.pyplot as myplot
l1 = [1, 3, 5]
l2 = [2, 4, 7, 12]
myplot.plot(l1, l2)
myplot.show()

this will fail with error:
ValueError: x and y must have same first dimension, but have shapes (3,) and (4,)

6. Save graphic in a file

Command myplot.show() display effectively resulted graphic.
Using savefig() graphic is saved in a file for example:

import matplotlib.pyplot as myplot
l1 = [1, 3, 5]
l2 = [2, 4, 12]
myplot.plot(l1, l2)
myplot.savefig("C:\tmp\fig1.png")

Here savefig method is used with only one parameter, the file path.
Method savefig have many more parameters, like here.
Above code will save graphic in file C:\tmp\fig1.png