Category Archives: numpy

How to apply gradient color to matplotlib graphs with colormap?

Matplotlib in python offers some useful tools for plotting with gradient colors. Below is a script that plot a sine wave with gradient color based on its y-value. It shows the use of matplotlib.cm.get_cmap to obtain a color map and the use of matplotlib.colors.Normalize to convert a value to the gradient index used for cmap.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import Normalize


X = np.linspace(0, 10, 101)
Y = 10*np.sin(x)

cmap = cm.get_cmap('Blues', len(y))
norm = Normalize(vmin=np.min(Y),vmax=np.max(Y))

for i in range(len(X)-1):
    plt.plot(X[i:i+2], Y[i:i+2], color=cmap(norm(Y[i])))

plt.legend()
plt.grid()
plt.xlabel("x")
plt.ylabel("y")

A more comprehensive list of colormaps can be found in the matplotlib site: https://matplotlib.org/3.3.2/tutorials/colors/colormaps.html

How To: Do Conditional AND on Pandas Filters

In pandas, sometimes we may want to have multiple conditional clause on dataframe selection. To do so, use “&” symbol for AND and put the each conditional statement into a curly bracket. Below is a sample code:

import pandas as pd

df = pd.DataFrame({'col_1':[0,1,2,3], 'col_2':[0,0,0,0]})
df.loc[(df['col_1'] > 0) & (df['col_1'] < 3), 'col_2'] = 1

print(df)

#    col_1  col_2
# 0      0      0
# 1      1      1
# 2      2      1
# 3      3      0

How to conditionally assign value to Pandas dataframes

import pandas as pd

df = pd.DataFrame({'col_1':[0,1,2,3], 'col_2':[0,0,0,0]})
df.loc[df['col_1'] < 2, 'col_2'] = 1
df.loc[df['col_1'] >= 2, 'col_2'] = -1

print(df)

#    col_1  col_2
# 0      0      1
# 1      1      1
# 2      2     -1
# 3      3     -1

The code above creates a dataframe with two columns and assign values to column 2 based on conditional statement on column 1.

How to normalize vectors in numpy

import numpy as np

# Normalizing a vector in Numpy
v = np.array([1, 2, 3]).T
v_normalized = v / np.linalg.norm(v)

# Normalizing multiple vectors stored in Matrix in Numpy
M = np.array([[1, 2, 3], [2, 3, 4], [5, 6, 7]]).T 
M_normalized = M / np.linalg.norm(M, axis=0)

Numpy offers some easy way to normalize vectors into unit vectors. The codes above use numpy.linalg.norm method to compute the L2 norm of the vector.

Source: https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.norm.html

Related post:
How to normalize vectors in Matlab

How To Fix: “The ordinal 242 could not be located in the dynamic link library mkl_intel_thread.dll”

One day after updating spyder in anaconda, I got the following error message when opening Spyder:
“The ordinal 242 could not be located in the dynamic link library mkl_intel_thread.dll”

Cause:
Another software installed MKL or Intel OpenMP (libiomp5md.dll) files into the C:\Windows\System32 folder (which is a bad practice) and the libiomp5md.dll installed is not compatible with numpy.

Temporary Fix:
Enter the following command in anaconda prompt before starting spyder

set CONDA_DLL_SEARCH_MODIFICATION_ENABLE=1
spyder

Permanent Fix:
Remove libiomp5md.dll in C:\Windows\System32 or rename it to libiomp5md.dll.bak.

Source: https://conda.io/projects/conda/en/latest/user-guide/troubleshooting.html#numpy-mkl-library-load-failed