Sorting arrays by column

With argsort, you get the index of the sorted array, which you can use to access the original array in sorted order. By selecting a single column for the sort algorithmn you can sort the array by the different columns

Assume an array with 3 columns and N rows

arr_org = np.random.rand((20,3))

idx=arr_org[:,3].argsort() arr_sort =arr_org[idx]

Multiple conditions with numpy.where

Using numpy and the numpy.where function is very powerfull. If you want to use several conditions with np.where you have to use the bitwise-AND-operator to combine to different conditions in one query. Of course you can also use the bitwise OR-operator too.

daten = np.random.rand(50,5)
sel = np.where((daten[:,0]==5) & (daten[:,1]< 17))[0]
plt.scatter(daten[sel,3], daten[sel,4])

Disabeling entries in the legend

The keyword '_nologend_' prevents a plot to show up in the legend of matplotlib figure

import numpy as np 
import matplotlib.pyplot as plt 

x1,y1 = np.random.rand(2,10) 
x2,y2 = np.random.rand(2,10) 
x3,y3 = np.random.rand(2,10) 
x4,y4 = np.random.rand(2,10) 

plt.plot(x1, y1, label='Test 1') 
plt.plot(x2, y2) #No label, so  no legend text 
plt.plot(x3, y3, label='_nolegend_') # Activ no legend text
plt.plot(x4, y4, label='Test 4') 

plt.legend()  # legend with entries from the first and fourth plot

Colored python output in iypthon

For a clear representation of some (verbose) text output from python program, it is nice to have some colors to highlight some (important) results. Just by adding some ANSI coding, you can easily color your text.


print('\x1b[31mRed Text \x1b[0m') 
print('\x1b[32mGreen Text \x1b[0m')