Category Archives: python

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: split PDFs into pages with pyPDF2 (writing your own PDF splitter)

The function below uses pyPDF2 to split pages in a pdf file into separate PDFs by given a path to the master PDF file.

import os
from PyPDF2 import PdfFileReader, PdfFileWriter

def pdf_splitter(path):
    fname = os.path.splitext(os.path.basename(path))[0]
 
    pdf = PdfFileReader(path)
    for page in range(pdf.getNumPages()):
        pdf_writer = PdfFileWriter()
        pdf_writer.addPage(pdf.getPage(page))
 
        output_filename = '{}_page_{}.pdf'.format(
            fname, page+1)
 
        with open(output_filename, 'wb') as out:
            pdf_writer.write(out)
 
        print('Created: {}'.format(output_filename))

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: Merge PDF Files with Python (PyPDF2)

PyPDF2 is a convenient library in Python to manipulate PDF files. Below is a piece of code that merge PDF files in a folder into one PDF:

import os
from PyPDF2 import PdfFileReader, PdfFileWriter

def merger(output_path, input_paths):
    pdf_writer = PdfFileWriter()
 
    for path in input_paths:
        pdf_reader = PdfFileReader(path, strict=False)

        for page in range(pdf_reader.getNumPages()):
            pdf_writer.addPage(pdf_reader.getPage(page))
            
            if page == 0:
                pdf_writer.addBookmark(os.path.basename(path), pdf_writer.getNumPages()-1, parent=None)
                

folder = r"PATH_TO_FOLDER_CONTAINING_PDFS"
output_path = os.path.join(folder, "out.pdf") # merged PDF
input_paths = []

for root, directory, fn_L in os.walk(folder):
    for fn in fn_L:
        fp = os.path.join(root, fn)
        input_paths.append(fp)
    break

merger(output_path, input_paths)

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

How to style your code on WordPress

WordPress supports styling of your code. To do so, simply wrap your code in tags [code language=”YOUR_LANGUAGE”] and [/code] in the HTML editor.

Here is an example for python:

print("Hello World!")
for i in range(10):
    print(i)

Plain text:

[code language=”python”]
print(“Hello World!”)
for i in range(10):
print(i)
[/code]

Quite a number of languages are supported:
actionscript3
bash
clojure
coldfusion
cpp
csharp
css
delphi
diff
erlang
fsharp
groovy
html
java
javafx
javascript
latex
matlab (keywords only)
objc
perl
php
powershell
python
r
ruby
scala
sql
text
vb
xml

 

Source: https://en.support.wordpress.com/wordpress-editor/blocks/syntax-highlighter-code-block/2/

How to roll back conda updates

Few days ago I updated my conda packages and I am getting the following errors:

Intel MKL FATAL ERROR: Cannot load mkl_intel_thread.dll.

The ordinal could not be located in the dynamic link library C:\Users\username\Anaconda3\Library\bin\mkl_intel_thread.dll.

(detail of the issue: https://github.com/ContinuumIO/anaconda-issues/issues/10213)

Apparently if the “conda-forge” channel is above the “defaults” channel on the .condarc list (located in home directory), this problem may occur.

 

Then I learnt some useful commands in anaconda:

  • conda list –revisions
  • conda install –revision REVNUM

The first command lists out the revision history of the conda environment, each with a revision number.

The second command will restore your conda environment to the revision you specify.

 

Reference:

https://support.anaconda.com/hc/en-us/articles/360023863914-Restoring-environment-to-a-previous-revision