Skip to content

Instantly share code, notes, and snippets.

@mfkasim1
Last active July 7, 2020 14:56
Show Gist options
  • Save mfkasim1/4aef7279e8eedc948adc657761059a70 to your computer and use it in GitHub Desktop.
Save mfkasim1/4aef7279e8eedc948adc657761059a70 to your computer and use it in GitHub Desktop.
Various python/numpy/matplotlib/scipy tricks
# plot histogram with logscale in the x-axis
import numpy as np
import matplotlib.pyplot as plt
def plot_loghist(x, bins=30, **hist_kwargs):
hist, bins = np.histogram(x, bins=bins)
logbins = np.logspace(np.log10(bins[0]),np.log10(bins[-1]),len(bins))
plt.hist(x, bins=logbins, **hist_kwargs)
plt.xscale('log')
# silencing loud third-party library
# usage:
# >>> with silence():
# ... noisy_func()
import os
class silence:
def __enter__(self):
devnull = open('/dev/null', 'w')
oldstdout_fno = os.dup(sys.stdout.fileno())
os.dup2(devnull.fileno(), 1)
def __exit__(self, type, value, traceback):
os.dup2(oldstdout_fno, 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment