Various Parameters for matplotlib Python

Matplotlib can be used to plot various type of plots. In this article we will look ta various parameters that can be used to display required information, adjust scale, adding labels etc.

Different type of plots can be plotted using following primary commands

plt.plot(x,y,color='green',alpha=0.5,)
plt.bar(np.arange(len(drinks)), sales, width=0.5, yerr=salesyerr, label="Jagur", color='r',edgecolor='b', fill=True, hatch='*',linestyle='--',align='center')
plt.scatter(x,y,marker='s',color='green', edgecolors='red',alpha=0.7,)
plt.pie(payment_method_freqs,autopct="%0.1f%%")

To add various point, we can use various options, most commonly used options are as below:

and here is the output

How to use subplots in matplotlib DataVisualization using python – part 2

I have already written about using subplots using matplot lib in this article. While working further on matplotlib, I found more convenient and easy to understand way for doing the same thing hence I decide to put part of of this port.

Here we are going to use subplot2grid() . It is a helper function that is similar to subplot() but uses 0-based indexing and let subplot to occupy multiple cells. The grid is specified by shape, at location of loc, spanning rowspan, colspan cells in each direction.

subplot2grid(shape, loc, rowspan=1, colspan=1,sharex=ax1)

Let us get our hands dirty. Below is simple code to plot three charts on the same plot.

Here is the output

Here a bit complex subplots

Here is the output

 

Code for generating plot is as below

ax5 = plt.subplot2grid((5,3), (3, 0), colspan=3,rowspan=2,sharex=ax1)
  • First bracket (5,3) indicates the number of rows and columns on the graph
  • second bracket (3,0) indicates location by row, column. Since this is zero based indexing, (0,0) indicates first row and first column
  • Plot can cover multiple rows and columns and can be specided by using colspan and rowspan
  • Axis can be shared between two plots and its indicated by sharex

 

 

 

 

 

 

What is correlation and how to find correlation using python

When two sets of data are strongly linked together we say they have a High Correlation.

Correlation is Positive when the values increase together, and
Correlation is Negative when one value decreases as the other increases

In common usage it most often refers to how close two variables are to having a linear relationship with each other. Here is sample values and shape for correlation

 

 

Pearson’s correlation coefficient

This is most commonly used correlation coefficient

The population correlation coefficient ρX,Y between two random variables X and Y with expected values μX and μY and standard deviations σX and σY is defined as

Pearson’s correlation coefficient using Python

When calculated using scipy, it returns pearson’s correlation coefficient and 2-tailed p-value

When calculated using numpy, it returns The correlation coefficient matrix of the variables.

Spearman’s rank correlation coefficient

Spearman’s rank correlation coefficient or Spearman’s rho, named after Charles Spearman and often denoted by the Greek letter rho. The Spearman correlation coefficient is defined as the Pearson correlation coefficient between the ranked variables.

 

bn
Spearman’s rank correlation coefficient

 

Kendall rank correlation coefficient

the Kendall rank correlation coefficient, commonly referred to as Kendall’s tau coefficient (after the Greek letter τ), is a statistic used to measure the ordinal association between two measured quantities. A tau test is a non-parametric hypothesis test for statistical dependence based on the tau coefficient.

 

Python code for calculating Person’s, Spearman’s and Kendall’s coefficient.

Correlation can have a value:

  • 1 is a perfect positive correlation
  • 0 is no correlation (the values don’t seem linked at all)
  • -1 is a perfect negative correlation

Important points to be noted

  • Correlation is not causation
  • Person’s coefficient works only if there is linear relationship between two variables.

How to Find Mean, Median and Mode Using Python

Before calculating mean, median and mode, let us look at types of data and characteristics of the data. At a very high level data can be classified as categorical and quantitative data. Both can be further classified as below

Difference Order Similar Interval Meaningful Zero
Categorical Nominal (Cities) Yes
Categorical Ordinal (Temp.) Yes Yes
Quantitative Interval Yes Yes Yes
Quantitative Ration Yes Yes Yes Yes

Now all of these types of data do not have all characteritics

Mode Median Mean
Nominal Yes
Ordinal Yes
interval Yes Yes Yes
ratio Yes Yes Yes

Mean:

Mean is nothing but average. It can be calculated in python or by using numpy

Median

Middle value of observation when ordered from low to high

Mode

Mots commonly occurring observation

 

 

Python NumPy Tutorial : Getting started with NumPy

NumPy is BSD licensed fundamental package for scientific computing with Python.  Most important feature is a powerful N-dimensional array object and sophisticated (broadcasting) functions. It also has useful linear algebra, Fourier transform, and random number capabilities

NumPy can also be used as an efficient multi-dimensional container of generic data. One of the most important feature is Arbitrary data-types.

Importing numpy and creating, accessing and modifying array

>>> import numpy as np
>>> a=np.array([1,2,3,4,5,6])
>>> a
array([1, 2, 3, 4, 5, 6])
>>> type(a)
<class 'numpy.ndarray'>
>>> a[1]
2
>>> a[1]=9
>>> a
array([1, 9, 3, 4, 5, 6])
>>> b = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
>>> b
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])

Numpy Properties

>>> a.shape
(6,)
>>> b.shape
(3, 4)
>>> a.size
6
>>> b.size
12
>>> a.data
<memory at 0x7faf92bb0dc8>
>>> b.data
<memory at 0x7faf92bbba68>
>>> a.dtype
dtype('int64')
>>> b.dtype
dtype('int64')


Mathematical operations on numpy numpyarray

>>> a
array([1, 2, 3, 4, 5, 6])
>>> b
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
>>> c
array([ 3, 6, 9, 12, 15, 18])
>>> a+c
array([ 4, 8, 12, 16, 20, 24])
>>> a-c
array([ -2, -4, -6, -8, -10, -12])
>>> a*c
array([ 3, 12, 27, 48, 75, 108])
>>> a/c
array([0.33333333, 0.33333333, 0.33333333, 0.33333333, 0.33333333,
0.33333333])
>>> np.sqrt(a)
array([1. , 1.41421356, 1.73205081, 2. , 2.23606798,
2.44948974])
>>> np.sum(b,axis=0)
array([15, 18, 21, 24])
>>> np.sum(b,axis=1)
array([10, 26, 42])
>>> b
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
>>> b.T
array([[ 1,  5,  9],
       [ 2,  6, 10],
       [ 3,  7, 11],
       [ 4,  8, 12]])

Working on the arrays

>>> b
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
>>> b.reshape(4,3)
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
>>> b
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
>>> b.resize(4,3)
>>> b
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
>>> b[0:2,1:2] #[row range,column range]
array([[2],
       [5]])

Python Dictionary

Python’s efficient key/value hash table structure is called a “dictionary” or “dict”. Major difference between list and dictionary is that index is always numeric in list whereas in dictionary it can be of any data type.The contents of a dict can be written as a series of key:value pairs within braces { }, e.g. dict = {key1:value1, key2:value2, … }. The “empty dict” is just an empty pair of curly braces {}.

 

>>> dict={}
>>> dict
{}
>>> dict['a']="blue"
>>> dict['b']="sky"
>>> dict['c']="earth"
>>> dict
{'a': 'blue', 'b': 'sky', 'c': 'earth'}
>>> dict['a']
'blue'
>>> type(dict)
<class 'dict'>
>>> dict={'a':'sky','b':'blue'} #other way to create dictionary
>>> dict
{'a': 'sky', 'b': 'blue'}

How to display key and values

>>> for k, v in dict.items(): print(k,'>',v) 
... 
a > blue
b > sky
c > earth
>>> for a in dict.values(): print(a)
... 
blue
sky
earth
>>> for b in dict.keys(): print(b)
... 
a
b
c

changing values and removing values

>>> dict
{'a': 'blue', 'b': 'sky', 'c': 'earth'}
>>> dict['a']="black" 
>>> dict
{'a': 'black', 'b': 'sky', 'c': 'earth'}
>>> dict.pop('a')
'black'
>>> dict
{'b': 'sky', 'c': 'earth'}
>>> del dict['b']
>>> dict
{'c': 'earth'}
>>> dict.clear()
>>> dict
{}

Python Lists

Lists are just like the arrays in other languages. Lists need not be homogeneous A single list may contain different data types such as Integers, Strings, as well as Objects.  List literals are written within square brackets [ ]. Lists work similarly to strings — use the len() function and square brackets [ ] to access data, with the first element at index 0. manjor difference being list is mutable however string is not.

>>> a=[]
>>> type(a)
<class 'list'>
>>> colors = ['red', 'blue', 'green']
>>> colors[0]
'red'
>>> len(colors)
3
>>> colors.append('yellow')
>>> colors
['red', 'blue', 'green', 'yellow']
>>> colors.insert(0,'black')
>>> colors
['black', 'red', 'blue', 'green', 'yellow']
>>> colors.extend(['white','gre'])
>>> colors
['black', 'red', 'blue', 'green', 'yellow', 'white', 'gre']
>>> colors.pop()
'gre'
>>> colors
['black', 'red', 'blue', 'green', 'yellow', 'white']
>>> colors.append('grey')
>>> colors
['black', 'red', 'blue', 'green', 'yellow', 'white', 'grey']
>>>

Slicing

>>> colors[:]
['black', 'red', 'blue', 'green', 'yellow', 'white', 'grey']
>>> colors[1:1]
[]
>>> colors[1:3]
['red', 'blue']
>>> colors[-1:]
['grey']
>>> colors[-1:-1]
[]
>>> colors[-1:-3]
[]
>>> colors[-1:2]
[]
>>> colors[-5:6]
['blue', 'green', 'yellow', 'white']
>>> colors[-5:-1]
['blue', 'green', 'yellow', 'white']
>>> colors[-5::-1]
['blue', 'red', 'black']

You can iterate thru list using following

>>> 'red' in colors
True
for col in colors:
     print(col)
#Output
black
red
blue
green
yellow
white
grey
['sky blue', 'snow white']

To check if element exists in List

>>> 'red' in colors
True

List Methods

Here are some other common list methods.

  • list.append(elem) — adds a single element to the end of the list. Common error: does not return the new list, just modifies the original.
  • list.insert(index, elem) — inserts the element at the given index, shifting elements to the right.
  • list.extend(list2) adds the elements in list2 to the end of the list. Using + or += on a list is similar to using extend().
  • list.index(elem) — searches for the given element from the start of the list and returns its index. Throws a ValueError if the element does not appear (use “in” to check without a ValueError).
  • list.remove(elem) — searches for the first instance of the given element and removes it (throws ValueError if not present)
  • list.sort() — sorts the list in place (does not return it). (The sorted() function shown later is preferred.)
  • list.reverse() — reverses the list in place (does not return it)
  • list.pop(index) — removes and returns the element at the given index. Returns the rightmost element if index is omitted (roughly the opposite of append()).

List Comprehension

 

How to install packages using snap Ubuntu

Snap is Canonical’s attempt to refine the app packaging and delivery mechanism on the Linux platform.  The Snap packages enable developers to bring much newer versions of apps to Ubuntu 16.04 LTS. Currently, the list of available snap packages is short, but soon we’ll be able to install more packages through the new snap package manager.

It is possible to install snap packages alongside traditional deb packages. These two packaging formats live comfortably next to one another. ( Read more about it here)

If you are on Ubuntu 16.04 LTS or later, you would already have snap installed on your machine, if you are on earlier version, install snap using following command.

$sudo apt install snapd

If you want to cross check if snap is installed or not, run following command.

~$ sudo apt list snapd
[sudo] password for conquistador:
Listing... Done
snapd/xenial-updates,now 2.34.2 amd64 [installed,automatic]
N: There is 1 additional version. Please use the '-a' switch to see it

Check the snap version

$ snap version
snap 2.36.1
snapd 2.36.1
series 16
ubuntu 16.04
kernel 4.15.0-39-generic

Now let us see how to find a package and install it.

Search package by name

$ snap find vscode
Name Version Publisher Notes Summary
vscode 1.29.1-1542309157 snapcrafters classic Code editing. Redefined.
ampareinvertcolor 1.0.0 juthawong - Simply Invert CSS Color - Made For Web Designer

You can search for keywords as well. If keyword has multiple words, please enclose it in single or double inverted commas.

$ snap find "image editor"
Name             Version         Publisher          Notes  Summary
pencilsheep      5               pencilsheep        -      Free professional image editor with full GPU acceleration
amparepngtoico   1.0.1           juthawong          -      Convert PNG Image To Windows Icon File in Clicks
nomacs           3.11.7          diemmarkus         -      nomacs is a free, open source image viewer.
imeditor         0.7             huluti             -      Simple & versatile image editor.
anituner         2.0             mmtrt              -      AniTuner lets you create, edit and convert Windows animated cursors ani files.
gimp             2.10.8          snapcrafters       -      GNU Image Manipulation Program
paintsupreme-3d  1.0.41          braindistrict      -      PaintSupreme 3D
goxel            0+git.5cabc00   guillaume          -      Goxel. Free and Open Source 3D Voxel Editor
carnet           0.9.0           alexandre-roux-m   -      Powerful note taking app with sync, online editor and android app
anifx            1.0             mmtrt              -      AniFX is a free cursor editor with many features.
tiled            1.1.6           bjorn              -      Your free, easy to use and flexible tile map editor.
polarr           5.2.1           polarrco*          -      Powerful and easy-to-use photo editor.
skrifa-lite      0.1.0           hyuchia            -      A simple word processor built with web technologies
skrifa           0.2.6           hyuchia            -      A simple word processor built with web technologies
photoscape       latest          merlijn-sebrechts  -      PhotoScape is a fun and easy photo editing software that enables you to fix and enhance photos.
kstars           master+38b187e  kde*               -      KStars is a desktop planetarium for amateur and professional astronomers.
gitkraken        4.1.1           gitkraken*         -      For repo management, in-app code editing & issue tracking.

Installation.

sudo snap install <package>

For some application you might need to use word –classic.  I got an error which installing visual studio code (vscode) using snap e.g.

$ sudo snap install vscode
[sudo] password for conquistador: 
error: This revision of snap "vscode" was published using classic confinement and thus may perform
arbitrary system changes outside of the security sandbox that snaps are usually confined to,
which may put your system at risk.

If you understand and want to proceed repeat the command including --classic.
$ sudo snap install --classic vscode
vscode 1.29.1-1542309157 from Snapcrafters installed

Snap packages are automatically updated, if you want to update any package manually, use following command.

sudo snap refresh <package>

Check list of packages installed on your computer

$ snap list
Name           Version            Rev   Tracking  Publisher     Notes
core           16-2.36.1          5897  stable    canonical*    core
vscode         1.29.1-1542309157  69    stable    snapcrafters  classic
wine-platform  3.0.3-3.21.0       36    stable    mmtrt         -

Removing the application

sudo snap remove <package>

One the application is installed, it can be run as any other application.

If you want to go into details, please refer to official tutorial and documentation

How to install Visual Studio Code on Ubuntu

Visual studio has been one of the leading code editor. I mostly use sublime text for its simplicity but many a times, Visual studio code feel more comfortable considering many add on features like git integration, debugging tools, autocomplete and not to forget inbuilt terminal.

Method#1 Install Visual Studio Code using .deb package

Step#1 Download Visual Studio code .deb/ *.rem package from https://code.visualstudio.com

Step#2 Install downloaded package using following command

sudo dpkg -i DEB_PACKAGE

Alternatively, you can simply double click on *.deb package. It will open software center page as below. Simply click on install. One you click on install, it will prompt you for password. Install the password and hit enter / click Authenticate.

Method#2 Install Visual Studio code from Ubuntu Software

Got to Ubuntu Software. ( by searching Ubuntu software in search box). Type Visual Studio in the Ubuntu Software sear box and you will see the Visual Studio Code, click on “install” button and you are good to go.

One you click on install, it will prompt you for password. Install the password and hit enter / click Authenticate.

Method#3 Install Visual Studio Code using snap

This is easiest method. Simple run following command and visual studio code will be installed immediately.

$ sudo snap install --classic vscode
vscode 1.29.1-1542309157 from Snapcrafters installed

If you want to know more about what is snap and how to use it ? please read this article

How to remove Visual Studio from Ubuntu

Method#1  Using Ubuntu Software

Got to Ubuntu Software. ( by searching Ubuntu software in search box). Type Visual Studio in the Ubuntu Software sear box and you will see the Visual Studio Code, click on “remove” button and you are good to go.

One you click on remove, it will prompt you for password. Install the password and hit enter / click Authenticate.

Method#2  Using Command Prompt

Just to check if Visual Studio Code is installed, run following command.

$ sudo apt list code
Listing... Done
code/stable,now 1.29.1-1542309157 amd64 [installed]

As you can see, name of the package is code. Now run following command

$ sudo apt-get remove code
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be REMOVED:
code
0 upgraded, 0 newly installed, 1 to remove and 197 not upgraded.
After this operation, 193 MB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 323562 files and directories currently installed.)
Removing code (1.29.1-1542309157) ...
Processing triggers for bamfdaemon (0.5.3~bzr0+16.04.20180209-0ubuntu1) ...
Rebuilding /usr/share/applications/bamf-2.index...
Processing triggers for gnome-menus (3.13.3-6ubuntu3.1) ...
Processing triggers for desktop-file-utils (0.22-1ubuntu5.1) ...
Processing triggers for mime-support (3.59ubuntu1) ...

Method#3  Using snap

Use this only if you have installed If you have installed Visual Studio code using snap then use following command

$ sudo snap remove  vscode
vscode removed

Note: You can use snap command to uninstall if you have installed using snap or installed from Ubuntu software center, however, if you have installed using .deb file, snap will not be able to remove the package

Upgrading Visual Studio code
Following command will upgrade all packages install on Ubuntu to their latest version

sudo apt update && sudo apt upgrade