Skip to content

Instantly share code, notes, and snippets.

@hrz6976
Last active January 16, 2022 13:43
Show Gist options
  • Save hrz6976/f1eae01634e4fd30561c6975e0d9d16c to your computer and use it in GitHub Desktop.
Save hrz6976/f1eae01634e4fd30561c6975e0d9d16c to your computer and use it in GitHub Desktop.
Plot a minimalist histogram
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
def plot_mini_hist(data: pd.Series, log_scale=False, xlim=None):
"""
:param data: a pandas series containing the `original` data (not to be confused with stats)
:param log_scale: plot x-axis in log scale (optional, default=False)
:param xlim: limit the range of x-axis, e.g. [-100, 300] (optional, default=None)
"""
fig, ax = plt.subplots(figsize=(3, 0.5))
sns.kdeplot(data, ax=ax, log_scale=log_scale, fill=True, color="black")
if log_scale:
x_major = matplotlib.ticker.LogLocator(base = 10.0, numticks = 5)
ax.xaxis.set_major_locator(x_major)
x_minor = matplotlib.ticker.LogLocator(base = 10.0, subs = np.arange(1.0, 10.0) * 0.1, numticks = 10)
ax.xaxis.set_minor_locator(x_minor)
ax.set_xlabel("")
ax.set_xlim(min(data) if xlim is None else xlim)
ax.get_yaxis().set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
return fig
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment