def _Paralel_plot( dataframe:DataFrame, cols:list[str], axes_names:list|None=None, cmap_style:str|None=None, alpha:float|None=None, figsize:tuple|None=None, invert_axes:list[int]=None, savePath:str|None=None,) -> Figure|None:
''' Dataframe: pandas dataframe cols: dataframe cols to plot axes_names: change the cols names cmap_style: cmap style from matplolib cmaps. Default = None, viridis alpha: line opacity. Default = 0.8 figsize: figure size. Default = (10,4) invert_axes: list with axes_ids (as type integer) to invert. Default = None (coming soon)
BASE CODE CREDITS: JohanC (user:12046409) encontrado em stackoverflow.com/questions/823063...
INTRODUCE:
categorical axes cmap colors and cbar
'''
data = dataframe[cols].copy()
encoders = {} for name in cols:
if data[name].dtype == 'object': label_encoder = LabelEncoder() data[name] = label_encoder.fit_transform(data[name]) encoders[name] = label_encoder
ys = data[cols].values n_rown, n_cols = ys.shape
scaler = MinMaxScaler(feature_range=(0,1)) zs = scaler.fit_transform(ys)
fig, host = plt.subplots(figsize=figsize or (10, 4))
cbar_col = zs[:, -1] norm = mcolors.Normalize(vmin = cbar_col.min(), vmax = cbar_col.max()) cmap = get_cmap(cmap_style or 'viridis') cbar:Colorbar = plt.colorbar(ScalarMappable(norm, cmap), ax=host, pad=0)
axes = [host] + [host.twinx() for i in range(n_cols - 2)] + [cbar.ax]
for i, ax in enumerate(axes):
ax.set_ylim(-0.05, 1.05) ax.spines['top'].set_visible(False) ax.spines['bottom'].set_visible(False) if ax != host: ax.spines['left'].set_visible(False) ax.yaxis.set_ticks_position('right') position_func = i / (n_cols - 1) ax.spines["right"].set_position( ("axes", position_func) )
old_yticks:list[np.ndarray] = [x.get_yticks() for x in axes] new_yticklabels = scaler.inverse_transform(np.array(old_yticks).T)
ax:plt.Axes if (k := data.columns[i]) in encoders.keys(): k_encoder:LabelEncoder = encoders[k] ax.set_yticks(np.unique(ys[:,i]), k_encoder.classes_) else: ax.set_yticklabels(np.round(new_yticklabels[:,i], 2))
host.set_xlim(0, n_cols - 1) host.set_xticks(range(n_cols), axes_names or cols) host.tick_params(axis='x', which='major', pad=7) host.spines['right'].set_visible(False) host.xaxis.tick_top()
for row in range(n_rown):
row:str serie1 = [x for x in np.linspace(0, len(ys) - 1, len(ys) * 3 - 2, endpoint=True)] serie2 = np.repeat(zs[row, :], 3)[1:-1] verts = list(zip(serie1, serie2)) color = cmap(norm(cbar_col[row].astype(float)), alpha = alpha or 0.8) #cbar color codes = [Path.MOVETO] + [Path.CURVE4 for _ in range(len(verts) - 1)] path = Path(verts, codes) patch = patches.PathPatch(path, facecolor='none', lw=1, edgecolor=color) host.add_patch(patch)
plt.tight_layout()
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
def _Paralel_plot(
dataframe:DataFrame,
cols:list[str],
axes_names:list|None=None,
cmap_style:str|None=None,
alpha:float|None=None,
figsize:tuple|None=None,
invert_axes:list[int]=None,
savePath:str|None=None,) -> Figure|None:
'''
Dataframe: pandas dataframe
cols: dataframe cols to plot
axes_names: change the cols names
cmap_style: cmap style from matplolib cmaps. Default = None, viridis
alpha: line opacity. Default = 0.8
figsize: figure size. Default = (10,4)
invert_axes: list with axes_ids (as type integer) to invert. Default = None (coming soon)
BASE CODE CREDITS:
JohanC (user:12046409) encontrado em stackoverflow.com/questions/823063...
INTRODUCE:
categorical axes
cmap colors and cbar
'''
data = dataframe[cols].copy()
convert categoricals to numerics and save the encoder
encoders = {}
for name in cols:
JohanC starts here
ys = data[cols].values
n_rown, n_cols = ys.shape
Ajust the axes
scaler = MinMaxScaler(feature_range=(0,1))
zs = scaler.fit_transform(ys)
create figura and host axes
fig, host = plt.subplots(figsize=figsize or (10, 4))
cbar its always the last one column
cbar_col = zs[:, -1]
norm = mcolors.Normalize(vmin = cbar_col.min(), vmax = cbar_col.max())
cmap = get_cmap(cmap_style or 'viridis')
cbar:Colorbar = plt.colorbar(ScalarMappable(norm, cmap), ax=host, pad=0)
axes creation
axes = [host] + [host.twinx() for i in range(n_cols - 2)] + [cbar.ax]
for i, ax in enumerate(axes):
define yticks
old_yticks:list[np.ndarray] = [x.get_yticks() for x in axes]
new_yticklabels = scaler.inverse_transform(np.array(old_yticks).T)
for i, ax in enumerate(axes):
define xticks
host.set_xlim(0, n_cols - 1)
host.set_xticks(range(n_cols), axes_names or cols)
host.tick_params(axis='x', which='major', pad=7)
host.spines['right'].set_visible(False)
host.xaxis.tick_top()
generation of smoothed lines (see johanC)
for row in range(n_rown):
plt.tight_layout()