Created
September 16, 2023 07:23
-
-
Save twang2218/4ac3b7c75296e650647a69d12efc62b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from joypy import joyplot | |
import seaborn as sns | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import math | |
def ridge_plot(data, by, columns, figsize=(12,8), colormap="Spectral", overlap=0.5, alpha=0.9, linecolor='white'): | |
fig = plt.figure(figsize=figsize) | |
classes = sorted(data[by].unique()) | |
num_of_classes = len(classes) | |
sqrt_num_of_plots = int(np.sqrt(num_of_classes)) | |
ncols = sqrt_num_of_plots | |
nrows = math.ceil(num_of_classes / ncols) | |
subfigs = fig.subfigures(nrows, ncols, wspace=0.2, hspace=0.2) | |
for i in range(num_of_classes): | |
row = i // ncols | |
col = i % ncols | |
subfig = subfigs[row][col] | |
axes = subfig.subplots(num_classes, 1, gridspec_kw={'hspace': -overlap}) | |
# print(f"row: {row}, col: {col}") | |
if isinstance(columns[i], str): | |
columns[i] = [columns[i]] | |
num_of_columns = len(columns[i]) | |
color = sns.color_palette(colormap, num_of_classes*num_of_columns) | |
# display(color) | |
try: | |
joyplot( | |
data=data, | |
by=by, | |
column=columns[i], | |
ax=axes, | |
alpha=alpha, | |
title=','.join(columns[i]), | |
color=color, | |
linecolor='white', | |
) | |
except Exception as e: | |
if 'tight_layout' in str(e): | |
# as expected | |
pass | |
else: | |
print(e) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Demo:
Plot: