Module 3.1
Module 3.1
Arrays in Python
Arrays are used to store multiple values in one single variable:
Example: Create an array containing car names:
cars = ["Ford", "Volvo", "BMW"]
print(cars)
Output:
["Ford", "Volvo", "BMW"]
An array is a special variable, which can hold more than one value at a time. If you have a list of items (a
list of car names, for example), storing the cars in single variables could look like this:
car1 = "Ford"
car2 = "Volvo"
car3 = "BMW"
However, what if you want to loop through the cars and find a specific one? And what if you had not 3
cars, but 300?
The solution is an array!
An array can hold many values under a single name, and you can access the values by referring to an index
number.
Array Methods
Python has a set of built-in methods that you can use on lists/arrays.
Numpy Module
NumPy is a Python library that is the core library for scientific computing in Python. It contains a
collection of tools and techniques that can be used to solve on a computer mathematical models of
problems in Science and Engineering. One of these tools is a high-performance multidimensional array
object that is a powerful data structure for efficient computation of arrays and matrices. To work with
these arrays, there’s a vast amount of high-level mathematical functions operate on these matrices and
arrays.
NumPy’s main object is the homogeneous multidimensional array. It is a table of elements (usually
numbers), all of the same type, indexed by a tuple of non-negative integers. In NumPy dimensions are
called axes.
For example, the coordinates of a point in 3D space [1, 2, 3] has one axis. That axis has 3 elements in it, so
we say it has a length of 3. In the example pictured below, the array has 2 axes.
The first axis has a length of 2, the second axis has a length of 3.
NumPy’s array class is called ndarray. It is also known by the alias array. Note that numpy.array is not the
same as the Standard Python Library class array.array, which only handles one-dimensional arrays and
offers less functionality.
ndarray.ndim
The number of axes (dimensions) of the array.
ndarray.shape
The dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension.
For a matrix with n rows and m columns, shape will be (n,m). The length of the shape tuple is therefore the
number of axes, ndim.
ndarray.size
The total number of elements of the array. This is equal to the product of the elements of shape.
ndarray.dtype
An object describing the type of the elements in the array. One can create or specify dtype’s using standard
Python types. Additionally NumPy provides types of its own. numpy.int32, numpy.int16, and
numpy.float64 are some examples.
ndarray.itemsize
The size in bytes of each element of the array. For example, an array of elements of type float64 has
itemsize 8 (=64/8), while one of type complex32 has itemsize 4 (=32/8). It is equivalent to
ndarray.dtype.itemsize.
ndarray.data
The buffer containing the actual elements of the array. Normally, we won’t need to use this attribute
because we will access the elements in an array using indexing facilities.
Example
import numpy as np
a = np.array([2, 3, 4, 5, 6, 7, 8])
print(a.dtype)
print(a.ndim)
print(a.shape)
print(a.itemsize)
print(a.data)
Output
int64
1
(7,)
8
<memory at 0x7f600c5cf400>
Array Creation
We can create an array from a regular Python list or tuple using the array function.
import numpy as np
a = np.array([1,2, 5, 6, 7])
print(a.dtype)
b=np.array([1.1, 2.1, 3.2])
print(b.dtype)
Output
int64
float64
A frequent error consists in calling array with multiple arguments, rather than providing a single sequence
as an argument.
a = np.array(1, 2, 3, 4) # WRONG
a = np.array([1, 2, 3, 4]) # RIGHT
array transforms sequences of sequences into two-dimensional arrays, sequences of sequences of sequences
into three-dimensional arrays, and so on.
import numpy as np
b = np.array([(1.5, 2, 3), (4, 5, 6)])
print(b)
Output
[[1.5 2. 3. ]
[4. 5. 6. ]]
The type of the array can also be explicitly specified at creation time:
import numpy as np
c = np.array([[1, 2], [3, 4]], dtype=complex)
print(c)
Output
[[1.+0.j 2.+0.j]
[3.+0.j 4.+0.j]]
Often, the elements of an array are originally unknown, but its size is known. Hence, NumPy offers several
functions to create arrays with initial placeholder content. These minimize the necessity of growing arrays,
an expensive operation.
The function zeros creates an array full of zeros, the function ones creates an array full of ones, and the
function empty creates an array whose initial content is random and depends on the state of the memory.
By default, the dtype of the created array is float64, but it can be specified via the key word argument
dtype.
import numpy as np
a=np.zeros((3, 4))
print(a)
print(a.dtype)
print()
b=np.ones((3, 4),dtype=np.int16)
print(b)
print(b.dtype)
print()
c=np.empty((3, 4),dtype=np.int16)
print(c)
Output:
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
float64
[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
int16
To create sequences of numbers, NumPy provides the arange function which is analogous to the Python
built-in range, but returns an array.
import numpy as np
a=np.arange(10, 30, 5)
print(a)
b=np.arange(0, 2, 0.3) # it accepts float arguments
print(b)
Output
[10 15 20 25]
[0. 0.3 0.6 0.9 1.2 1.5 1.8]
When arange is used with floating point arguments, it is generally not possible to predict the number of
elements obtained, due to the finite floating point precision. For this reason, it is usually better to use the
function linspace that receives as an argument the number of elements that we want, instead of the step:
import numpy as np
print(np.linspace(0, 2, 9) ) # 9 numbers from 0 to 2
Output
[0. 0.25 0.5 0.75 1. 1.25 1.5 1.75 2. ]
Printing Arrays
When you print an array, NumPy displays it in a similar way to nested lists. One-dimensional arrays are
then printed as rows, bidimensionals as matrices and tridimensionals as lists of matrices.
Example
import numpy as np
a = np.arange(6) # 1d array
print("One Dimensional Array:\n ", a)
b = np.arange(12)
print("Array:\n ",b)
c = np.arange(12).reshape(4, 3) # 2d array
print("Two Dimensional Array: \n",c)
d = np.arange(24).reshape(2, 3, 4) # 3d array
print("Three Dimensional Array:\n ",d)
Output
One Dimensional Array:
[0 1 2 3 4 5]
Array:
[ 0 1 2 3 4 5 6 7 8 9 10 11]
Two Dimensional Array:
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
Three Dimensional Array:
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
reshape()
Reshaping means changing the shape of an array. The shape of an array is the number of elements in each
dimension. By reshaping we can add or remove dimensions or change number of elements in each
dimension.
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
print(arr)
newarr = arr.reshape(4, 3)
print(newarr)
Output
[ 1 2 3 4 5 6 7 8 9 10 11 12]
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
If an array is too large to be printed, NumPy automatically skips the central part of the array and only prints
the corners:
import numpy as np
print(np.arange(10000))
print()
print(np.arange(10000).reshape(100, 100))
Output
[ 0 1 2 ... 9997 9998 9999]
[[ 0 1 2 ... 97 98 99]
[ 100 101 102 ... 197 198 199]
[ 200 201 202 ... 297 298 299]
...
[9700 9701 9702 ... 9797 9798 9799]
[9800 9801 9802 ... 9897 9898 9899]
[9900 9901 9902 ... 9997 9998 9999]]
To disable this behaviour and force NumPy to print the entire array, you can change the printing options
using set_printoptions.
np.set_printoptions(threshold=sys.maxsize) # sys module should be imported
Example:
import numpy as np
import sys
np.set_printoptions(threshold=sys.maxsize)
print(np.arange(500).reshape(5, 100))
Output
[[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
99]
[100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
191 192 193 194 195 196 197 198 199]
[200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
291 292 293 294 295 296 297 298 299]
[300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
391 392 393 394 395 396 397 398 399]
[400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
491 492 493 494 495 496 497 498 499]]
Basic Operations
Arithmetic operators on arrays apply element wise. A new array is created and filled with the result.
Unlike in many matrix languages, the product operator * operates element wise in NumPy arrays. The
matrix product can be performed using the @ operator (in python >=3.5) or the dot function or method:
Example
import numpy as np
a = np.array([20, 30, 40, 50])
b=np.arange(4)
print(a)
print(b)
print("Sum : " , a+b)
print("Subtraction : ", a-b)
print("Product :", a*b)
print("Product :", a@b)
print("Product :", a.dot(b))
print("Square of array b : ", b**2)
print("Sine of a : ", 10 * np.sin(a))
print(a>30)
Output
[20 30 40 50]
[0 1 2 3]
Sum : [20 31 42 53]
Subtraction : [20 29 38 47]
Product : [ 0 30 80 150]
Product : 260
Product : 260
Square of array b : [0 1 4 9]
Sine of a : [ 9.12945251 -9.88031624 7.4511316 -2.62374854]
[False False True True]
Random in NumPy
Random number does NOT mean a different number every time. Random means something that can not be
predicted logically. NumPy offers the random module to work with random numbers. The random
module's rand() method returns a random float between 0 and 1.
Example
from numpy import random
x = random.randint(100)
y = random.rand()
print(x)
print(y)
Output
52
0.7985497296519793
Some operations, such as += and *=, act in place to modify an existing array rather than create a new one.
Example
import numpy as np
rg = np.random.default_rng(1) # create instance of default random number generator
a = np.ones((2, 3), dtype=int)
print(a)
b = rg.random((2, 3))
print(b)
a *= 3
print(a)
b += a
print(b)
a += b # b is not automatically converted to integer type
print(a)
Output
[[1 1 1]
[1 1 1]]
[[0.51182162 0.9504637 0.14415961]
[0.94864945 0.31183145 0.42332645]]
[[3 3 3]
[3 3 3]]
[[3.51182162 3.9504637 3.14415961]
[3.94864945 3.31183145 3.42332645]]
Traceback (most recent call last):
File "main.py", line 11, in <module>
a += b # b is not automatically converted to integer type
numpy.core._exceptions.UFuncTypeError: Cannot cast ufunc 'add' output from dtype('float64') to
dtype('int64') with casting rule 'same_kind'
When operating with arrays of different types, the type of the resulting array corresponds to the more
general or precise one (a behavior known as upcasting).
Example
import numpy as np
from math import pi
a = np.ones(3, dtype=np.int32)
b = np.linspace(0, pi, 3)
print(b.dtype.name)
c=a+b
print(c)
print(c.dtype.name)
Output
float64
[1. 2.57079633 4.14159265]
float64
Many unary operations, such as computing the sum of all the elements in the array, are implemented as
methods of the ndarray class.
Example
import numpy as np
rg = np.random
a = rg.random((2, 3))
print(a)
print("Sum of elements :", a.sum())
print("Smallest element: " ,a.min())
print("Largest element : ", a.max())
Output
[[0.67519628 0.81432613 0.63528654]
[0.12096685 0.05783348 0.94834857]]
Sum of elements : 3.2519578460730845
Smallest element: 0.05783348255247289
Largest element : 0.9483485678855282
By default, these operations apply to the array as though it were a list of numbers, regardless of its shape.
However, by specifying the axis parameter you can apply an operation along the specified axis of an array:
Example
import numpy as np
rg = np.random
b = np.arange(12).reshape(3, 4)
print(b)
print(" Sum of each column: ", b.sum(axis=0)) # sum of each column
print("Smallest of each row : ", b.min(axis=1)) # min of each row
print("Cumilaive sum in each row: ", b.cumsum(axis=1) ) # cumulative sum along each row
Output
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Sum of each column: [12 15 18 21]
Smallest of each row : [0 4 8]
Cumilaive sum in each row:
[[ 0 1 3 6]
[ 4 9 15 22]
[ 8 17 27 38]]
[Cummulative sum means partially adding the elements in array.
E.g. The partial sum of [1, 2, 3, 4] would be [1, 1+2, 1+2+3, 1+2+3+4] = [1, 3, 6, 10]. ]
Power
The power() function rises the values from the first array to the power of the values of the second array,
and return the results in a new array.
Example: Raise the valules in arr1 to the power of values in arr2:
import numpy as np
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([3, 5, 6, 8, 2, 33])
newarr = np.power(arr1, arr2)
print(newarr)
Output
[1000 3200000 729000000 6553600000000 2500 0]
The example above will return [1000 3200000 729000000 6553600000000 2500 0] which is the result of
10*10*10, 20*20*20*20*20, 30*30*30*30*30*30 etc.
Remainder
Both the mod() and the remainder() functions return the remainder of the values in the first array
corresponding to the values in the second array, and return the results in a new array.
Example: Return the remainders:
import numpy as np
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([3, 7, 9, 8, 2, 33])
newarr = np.mod(arr1, arr2)
print(newarr)
Output
[ 1 6 3 0 0 27]
The example above will return [1 6 3 0 0 27] which is the remainders when you divide 10 with 3 (10%3),
20 with 7 (20%7) 30 with 9 (30%9) etc.
You get the same result when using the remainder() function:
Example: Return the remainders:
import numpy as np
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([3, 7, 9, 8, 2, 33])
newarr = np.remainder(arr1, arr2)
print(newarr)
Output
[ 1 6 3 0 0 27]
Quotient and Mod
The divmod() function return both the quotient and the the mod. The return value is two arrays, the first
array contains the quotient and second array contains the mod.
Example: Return the quotient and mod:
import numpy as np
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([3, 7, 9, 8, 2, 33])
newarr = np.divmod(arr1, arr2)
print(newarr)
Output
(array([ 3, 2, 3, 5, 25, 1]), array([ 1, 6, 3, 0, 0, 27]))
The example above will return:
(array([3, 2, 3, 5, 25, 1]), array([1, 6, 3, 0, 0, 27]))
The first array represents the quotients, (the integer value when you divide 10 with 3, 20 with 7, 30 with 9
etc. The second array represents the remainders of the same divisions.
Absolute Values
Both the absolute() and the abs() functions do the same absolute operation element-wise but we should use
absolute() to avoid confusion with python's inbuilt math.abs()
Example: Return the quotient and mod:
import numpy as np
arr = np.array([-1, -2, 1, 2, 3, -4])
newarr = np.absolute(arr)
print(newarr)
Output
[1 2 1 2 3 4]
The example above will return [1 2 1 2 3 4].
Trigonometric functions:
These functions work on radians, so angles need to be converted to radians by multiplying by pi/180.
Only then we can call trigonometric functions. They take an array as input arguments. It includes
functions like-
Function Description
sin, cos, tan Compute sine, cosine and tangent of angles
arcsin, arccos, arctan Calculate inverse sine, cosine and tangent
hypot Calculate hypotenuse of given right triangle
sinh, cosh, tanh Compute hyperbolic sine, cosine and tangent
arcsinh, arccosh, arctanh Compute inverse hyperbolic sine, cosine and tangent
deg2rad Convert degree into radians
rad2deg Convert radians into degree
Bit-twiddling functions:
These functions accept integer values as input arguments and perform bitwise operations on binary
representations of those integers. It include functions like-
Function Description
bitwise_and performs bitwise and operation on two array elements
bitwies_or performs bitwise or operation on two array elements
bitwise_xor performs bitwise xor operation on two array elements
invert performs bitwise inversion of an array elements
left_shift shift the bits of elements to left
right_shift shift the bits of elements to right
import numpy as np
a = np.arange(10)
print(a)
print("Second position element ; ", a[2])
print("Elements from 2 to 5 position : ", a[2:5])
a[0:6:2] = 1000 # from start to position 6, exclusive, set every 2nd element to 1000
print(a)
print("Reverse of a ; ", a[::-1]) # reversed a
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print("Last element from 2nd dim: ", arr[1, -1])
Output
[0 1 2 3 4 5 6 7 8 9]
Second position element ; 2
Elements from 2 to 5 position : [2 3 4]
[1000 1 1000 3 1000 5 6 7 8 9]
Reverse of a ; [ 9 8 7 6 5 1000 3 1000 1 1000]
Last element from 2nd dim: 10
When fewer indices are provided than the number of axes, the missing indices are considered complete
slices:
import numpy as np
b=np.array([[1,2,3],[4,5,6]])
print(b)
print("Last row: ",b[-1] ) # the last row. Equivalent to b[-1, :]
print("Last row: ",b[-1,:])
Output
[[1 2 3]
[4 5 6]]
Last row: [4 5 6]
Last row: [4 5 6]
Iterating over multidimensional arrays is done with respect to the first axis:
import numpy as np
b=np.array([[1,2,3],[4,5,6]])
print("Elements are: ")
for row in b:
print(row)
Output
Elements are:
[1 2 3]
[4 5 6]
However, if one wants to perform an operation on each element in the array, one can use the flat attribute
which is an iterator over all the elements of the array:
import numpy as np
b=np.array([[1,2,3],[4,5,6]])
print(b)
print("Elements +10 are: ")
for row in b.flat:
print(row+10)
Output
[[1 2 3]
[4 5 6]]
Elements +10 are:
11
12
13
14
15
16
Example: flat
import numpy as np
b=np.array([[1,2,3],[4,5,6],[7,8,9]])
print(b)
print(b.flat[0:10])
Output
[[1 2 3]
[4 5 6]
[7 8 9]]
[1 2 3 4 5 6 7 8 9]
Shape Manipulation
Changing the shape of an array
An array has a shape given by the number of elements along each axis:
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
print("Elements are : \n", a)
print("Shape:", a.shape)
print("Flat: ",a.ravel())
print("Shape changed :\n", a.reshape(3,2))
print("Transpose : \n", a.T)
Output
Elements are :
[[1 2 3]
[4 5 6]]
Shape: (2, 3)
Flat: [1 2 3 4 5 6]
Shape changed :
[[1 2]
[3 4]
[5 6]]
Transpose :
[[1 4]
[2 5]
[3 6]]
The order of the elements in the array resulting from ravel()[which is used to change a 2-dimensional array
or a multi-dimensional array into a contiguous flattened array] is normally “C-style”, that is, the rightmost
index “changes the fastest”, so the element after a[0, 0] is a[0, 1].
We can also use the ndarray.resize method modifies the shape of the array.
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
print("Elements are : \n", a)
print("Shape:", a.shape)
print(a.reshape(6,1))
a.resize(2,3)
print(a)
Output
Elements are :
[[1 2 3]
[4 5 6]]
Shape: (2, 3)
[[1]
[2]
[3]
[4]
[5]
[6]]
[[1 2 3]
[4 5 6]]
If a dimension is given as -1 in a reshaping operation, the other dimensions are automatically calculated:
import numpy as np
a = np.array([[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]])
print("Elements are : \n", a)
print("Shape:", a.shape)
print(a.reshape(2,6))
print(a.reshape(3,-1))
Output
Elements are :
[[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]]
Shape: (1, 4, 3)
[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
Output
id of arr 139932570884496
id of v 139932570884576
original array- [12 4 6 8 10]
view- [12 4 6 8 10]
Copy
Copy: This is also known as Deep Copy. The copy is completely a new array and copy owns the data.
When we make changes to the copy it does not affect the original array, and when changes are made to the
original array it does not affect the copy.
import numpy as np
arr = np.array([2, 4, 6, 8, 10])
# creating copy of array
c = arr.copy()
# both arr and c have different id
print("id of arr", id(arr))
print("id of c", id(c))
# changing original array this will not effect copy
arr[0] = 12
# printing array and copy
print("original array- ", arr)
print("copy- ", c)
Output
id of arr 140212025436640
id of c 140212025436720
original array- [12 4 6 8 10]
copy- [ 2 4 6 8 10]
Data visualization
A graph or chart is used to present numerical data in visual form. A graph is one of the easiest
ways to compare numbers. They should be used to make facts clearer and more understandable.
Results of mathematical computations are often presented in graphical format. In this chapter,
we will explore the Python modules used for generating two and three dimensional graphs of
various types.
The Matplotlib Module
Matplotlib is a python package that produces quality figures. It also provides many functions
for matrix manipulation. You can generate plots, histograms, bar charts etc, with just a few
lines of code and have full control of line styles, font properties, axes properties, etc.
The data points to the plotting functions are supplied as Python lists or Numpy arrays.
If you import matplotlib, the plotting functions from the submodules pyplot will be available as
local functions. Pylab also imports Numpy for you. Let us start with some simple plots to
become familiar with matplotlib.
Example plot1.py
import matplotlib as pylab
from pylab import *
data = [1,2,5]
plot(data)
show()
x = [1,2,5]
y = [4,5,6]
plot(x,y)
show()
By default, the color is blue and the line style is continuous. This can be changed by an optional
argument after the coordinate data, which is the format string that indicates the color and line
type of the plot. The default format string is 'b-' (blue, continuous line). Let us rewrite the
above example to plot using red circles. We will also set the ranges for x and y axes and label
them.
Example plot3.py
import matplotlib as pylab
from pylab import *
x = [1,2,5]
y = [4,5,6]
plot(x,y,'ro')
xlabel('x-axis')
ylabel('y-axis')
axis([0,6,1,7])
show()
Pyplot
Most of the Matplotlib utilities lies under the pyplot submodule, and are usually imported under
the plt alias:
import matplotlib.pyplot as plt
Now the Pyplot package can be referred to
as plt.
Plot
The plot() function is used to draw points (markers) in a diagram. By default, the plot() function draws
a line from point to point. The function takes parameters for specifying points in the diagram.
Parameter 1 is an array containing the points on the x-axis.
Parameter 2 is an array containing the points on the y-axis.
If we need to plot a line from (1, 3) to (8, 10), we have to pass two arrays [1, 8] and [3, 10] to the plot
function.
Draw a line in a diagram from position (1, 3) to position (8, 10):
import matplotlib.pyplot as plt
import numpy as np
xpoints=np.array([1, 8])
ypoints=np.array([3, 10])
plt.plot(xpoints,ypoints)
plt.show()
To plot only the markers, you can use shortcut string notation parameter 'o', which means 'rings'.
Draw two points in the diagram, one at position (1, 3) and one in
position (8, 10):
import matplotlib.pyplot as plt
import numpy as np
xpoints=np.array([1, 8])
ypoints=np.array([3, 10])
plt.plot(xpoints,ypoints, 'o')
plt.show()
Multiple Points
You can plot as many points as you like, just make sure you have the same number of points in both axis.
Draw a line in a diagram from position (1, 3) to (2, 8) then to (6, 1) and finally to position (8, 10):
Default X-Points
If we do not specify the points in the x-axis, they will get the
default values 0, 1, 2, 3, (etc. depending on the length of the y-
points.
So, if we take the same example as above, and leave out the x-
points, the diagram will look like this:
Plotting without x-points:
Markers
You can use the keyword argument marker to emphasize each point
with a specified marker:
Mark each point with a circle:
Marker Reference
'+' Plus
You can use also use the shortcut string notation parameter to specify the marker. This parameter is also
called fmt, and is written with this syntax:
marker|line|color
Line Reference
Color Reference
Marker Size
Marker Color
You can use the keyword argument linestyle, or shorter ls, to change the style of the plotted line:
Use a dotted line:
Shorter Syntax
You can use the fontdict parameter in xlabel(), ylabel(), and title() to set font properties for the
title and labels.
You can use the loc parameter in title() to position the title.
Legal values are: 'left', 'right', and 'center'. Default value is
'center'.
import matplotlib.pyplot as plt
import numpy as np
font1 = {'family':'serif','color':'blue','size':20}
font2 = {'family':'serif','color':'darkred','size':15}
xpoints = np.array([0, 6])
ypoints = np.array([0, 250])
plt.plot(xpoints, ypoints)
plt.title('Sample Graph',fontdict = font1, loc = 'left')
plt.xlabel('X axis', fontdict = font2)
plt.ylabel('Y axis', fontdict = font2)
plt.show()
Subplots
With the subplots() function you can draw multiple plots in one figure. The subplots() function takes
three arguments that describes the layout of the figure. The layout is organized in rows and columns,
which are represented by the first and second argument. The third argument represents the index of the
current plot.
plt.subplot(1, 2, 1) #the figure has 1 row, 2 columns, and this plot is the first plot.
plt.subplot(1, 2, 2) #the figure has 1 row, 2 columns, and this plot is the second plot.
plt.subplot(1, 2, 1)
plt.plot(x,y)
plt.title("SALES")
#plot2:
x=np.array([0, 1, 2, 3])
y=np.array([10, 20, 30, 40])
plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.title("INCOME")
plt.suptitle("MYSHOP")
plt.show()
Scatter
With Pyplot, you can use the scatter() function to draw a scatter plot. The scatter() function plots one
dot for each observation. It needs two arrays of the same length, one
for the values of the x-axis, and one for values on the y-axis:
A simple scatter plot:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2,17,2])
y = np.array([99,86,87,88,111,86,103])
plt.scatter(x, y)
plt.show()
Colors
You can set your own color for each scatter plot with the color or the c argument:
Set your own color of the markers:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2])
y = np.array([1,8,2,4,9])
plt.scatter(x, y, color = 'hotpink')
x = np.array([2,2,8,1,15])
y = np.array([3,1,7,3,13])
plt.scatter(x, y, color = '#88c999')
plt.show()
Size
You can change the size of the dots with the s argument.
Just like colors, make sure the array for sizes has the same
length as the arrays for the x- and y-axis:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([5,7,8,7,2])
y = np.array([99,86,87,88,75])
sizes = np.array([20,50,100,200,500])
plt.scatter(x, y, s=sizes)
plt.show()
Bars
With Pyplot, you can use the bar() function to draw bar graphs:
Horizontal Bars
Bar Color
The bar() and barh() takes the keyword argument color to set the color
of the bars:
import matplotlib.pyplot as plt
import numpy as np
x=np.array(["A", "B", "C", "D"])
y=np.array([3, 8, 1, 10])
plt.bar(x,y,color= "red")
plt.show()
Bar Width
The bar() takes the keyword argument width to set the width of the bars:
import matplotlib.pyplot as plt
import numpy as np
x=np.array(["A", "B", "C", "D"])
y=np.array([3, 8, 1, 10])
plt.bar(x,y,width= 0.1)
plt.show()
Bar Height
The barh() takes the keyword argument height to set the height
of the bars:
import matplotlib.pyplot as plt
import numpy as np
x=np.array(["A", "B", "C", "D"])
y=np.array([3, 8, 1, 10])
plt.barh(x,y,height= 0.1)
plt.show()
Pie Charts
With Pyplot, you can use the pie() function to draw pie charts:
Labels
Explode
Maybe you want one of the wedges to stand out? The explode parameter allows you to do that.
The explode parameter, if specified, and not None, must be an
array with one value for each wedge.
Each value represents how far from the center each wedge is
displayed:
Pull the "Apples" wedge 0.2 from the center of the pie:
import matplotlib.pyplot as plt
import numpy as np
y = np.array([35, 25, 25, 15])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
myexplode = [0.2, 0, 0, 0]
plt.pie(y, labels = mylabels, explode = myexplode)
plt.show()
Shadow
Add a shadow to the pie chart by setting the shadows parameter to True:
import matplotlib.pyplot as plt
import numpy as np
Colors
You can set the color of each wedge with the colors parameter.
The colors parameter, if specified, must be an array with one value for
each wedge:
import matplotlib.pyplot as plt
import numpy as np
Legend
To add a list of explanation for each wedge, use the legend() function. To add a header to the legend, add
the title parameter to the legend function.
import matplotlib.pyplot as plt
import numpy as np
Histogram
plt.hist(x)
plt.show()
Sin(x)
import numpy as np
x = np.arange(0,4*np.pi,0.1)
sin = np.sin(x)
plt.plot(x, sin)
plt.ylabel("sin(x)")
plt.xlabel("0° to 360°")
plt.show()
sin(x2)
import numpy as np
x = np.arange(0,4*np.pi,0.1)
sin = np.sin(x*x)
plt.plot(x,sin)
plt.ylabel("sin(x2)")
plt.xlabel("0° to 360°")
plt.show()
x2
import numpy as np
x = np.array([1,2,3,4,5,6])
plt.plot(x*x,'o-')
plt.title("Plot of (x*x)")
plt.show()