Tag 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: 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 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

Opensees Awesome List

Hanlin-Dong (董翰林) created an awesome list for opensees (https://github.com/Hanlin-Dong/awesome-opensees).

Some of the tools on the list are really great, and I recommend the followings:

  1. OpenSees Navigator: a very easy to use mat-lab built-in GUI tool to create opensees models. Perfect for mat-lab users and new opensees users.
  2. OpenSeesAPI: Python API to write .tcl script.
  3. OpenSees Model View: Python script to visualize model built in .tcl (doesn’t support .tcl with comments yet, but the project has great potential)

I am so glad to find this list when I started learning opensees. These tools provide a board coverage on what opensees can do and I am totally impressed.