Moe's Techblog

sort array 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 differnet 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.


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



Einträge in der Legende verhindern

Mit dem Eintrag '_nologend_' kann man mit der Python Matplotlib verhindern, dass ein Plot in der Legende aufgeführt wird


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) #Kein Label, also auch nicht drin 
plt.plot(x3, y3, label='_nolegend_') # Aktiv unterdrückt
plt.plot(x4, y4, label='Test 4') 

plt.legend()  # Legende mit Einträgen des ersten und  vierten Plots



Colored python output

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')