<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: João Lucas Hernandes Detogni</title>
    <description>The latest articles on DEV Community by João Lucas Hernandes Detogni (@liso1201).</description>
    <link>https://dev.to/liso1201</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1148453%2F8c93e346-9954-4492-b7e4-b99daaa36a37.png</url>
      <title>DEV Community: João Lucas Hernandes Detogni</title>
      <link>https://dev.to/liso1201</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/liso1201"/>
    <language>en</language>
    <item>
      <title>Parallel Coordinates function</title>
      <dc:creator>João Lucas Hernandes Detogni</dc:creator>
      <pubDate>Wed, 30 Aug 2023 02:09:45 +0000</pubDate>
      <link>https://dev.to/liso1201/parallel-coordinates-function-4jlb</link>
      <guid>https://dev.to/liso1201/parallel-coordinates-function-4jlb</guid>
      <description>&lt;p&gt;Hi there, this is my first post here, and i would like to introduce an function to a parallel coordinates plot. &lt;/p&gt;

&lt;p&gt;`def _Paralel_plot(&lt;br&gt;
dataframe:DataFrame, &lt;br&gt;
cols:list[str], &lt;br&gt;
axes_names:list|None=None,&lt;br&gt;
cmap_style:str|None=None, &lt;br&gt;
alpha:float|None=None,&lt;br&gt;
figsize:tuple|None=None,&lt;br&gt;
invert_axes:list[int]=None,&lt;br&gt;
savePath:str|None=None,) -&amp;gt; Figure|None:&lt;/p&gt;

&lt;p&gt;'''&lt;br&gt;
Dataframe: pandas dataframe&lt;br&gt;
cols: dataframe cols to plot&lt;br&gt;
axes_names: change the cols names&lt;br&gt;
cmap_style: cmap style from matplolib cmaps. Default = None, viridis&lt;br&gt;
alpha: line opacity. Default = 0.8&lt;br&gt;
figsize: figure size. Default = (10,4)&lt;br&gt;
invert_axes: list with axes_ids (as type integer) to invert. Default = None (coming soon)&lt;/p&gt;

&lt;p&gt;BASE CODE CREDITS: &lt;br&gt;
JohanC (user:12046409) encontrado em &lt;a href="https://stackoverflow.com/questions/8230638/parallel-coordinates-plot-in-matplotlib"&gt;https://stackoverflow.com/questions/8230638/parallel-coordinates-plot-in-matplotlib&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;INTRODUCE:&lt;/p&gt;

&lt;p&gt;categorical axes&lt;br&gt;
cmap colors and cbar&lt;/p&gt;

&lt;p&gt;'''&lt;/p&gt;

&lt;p&gt;data = dataframe[cols].copy()&lt;/p&gt;

&lt;h1&gt;
  
  
  convert categoricals to numerics and save the encoder
&lt;/h1&gt;

&lt;p&gt;encoders = {}&lt;br&gt;
for name in cols:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if data[name].dtype == 'object':
    label_encoder = LabelEncoder()
    data[name] = label_encoder.fit_transform(data[name])
    encoders[name] = label_encoder
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
  
  
  JohanC starts here
&lt;/h1&gt;

&lt;p&gt;ys = data[cols].values&lt;br&gt;
n_rown, n_cols = ys.shape&lt;/p&gt;
&lt;h1&gt;
  
  
  Ajust the axes
&lt;/h1&gt;

&lt;p&gt;scaler = MinMaxScaler(feature_range=(0,1))&lt;br&gt;
zs = scaler.fit_transform(ys)&lt;/p&gt;
&lt;h1&gt;
  
  
  create figura and host axes
&lt;/h1&gt;

&lt;p&gt;fig, host = plt.subplots(figsize=figsize or (10, 4))&lt;/p&gt;
&lt;h1&gt;
  
  
  cbar its always the last one column
&lt;/h1&gt;

&lt;p&gt;cbar_col = zs[:, -1]&lt;br&gt;
norm = mcolors.Normalize(vmin = cbar_col.min(), vmax = cbar_col.max())&lt;br&gt;
cmap = get_cmap(cmap_style or 'viridis')&lt;br&gt;
cbar:Colorbar = plt.colorbar(ScalarMappable(norm, cmap), ax=host, pad=0)&lt;/p&gt;
&lt;h1&gt;
  
  
  axes creation
&lt;/h1&gt;

&lt;p&gt;axes = [host] + [host.twinx() for i in range(n_cols - 2)] + [cbar.ax]&lt;/p&gt;

&lt;p&gt;for i, ax in enumerate(axes):&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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)      )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
  
  
  define yticks
&lt;/h1&gt;

&lt;p&gt;old_yticks:list[np.ndarray] = [x.get_yticks() for x in axes]&lt;br&gt;
new_yticklabels = scaler.inverse_transform(np.array(old_yticks).T)&lt;/p&gt;

&lt;p&gt;for i, ax in enumerate(axes):&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
  
  
  define xticks
&lt;/h1&gt;

&lt;p&gt;host.set_xlim(0, n_cols - 1) &lt;br&gt;
host.set_xticks(range(n_cols), axes_names or cols)&lt;br&gt;&lt;br&gt;
host.tick_params(axis='x', which='major', pad=7)&lt;br&gt;
host.spines['right'].set_visible(False)&lt;br&gt;
host.xaxis.tick_top()  &lt;/p&gt;
&lt;h1&gt;
  
  
  generation of smoothed lines (see johanC)
&lt;/h1&gt;

&lt;p&gt;for row in range(n_rown):&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;plt.tight_layout()&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;`&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This plot allows for both categorical and numerical axes. It's similar to a Weights and Biases plot (&lt;a href="https://wandb.ai/site"&gt;https://wandb.ai/site&lt;/a&gt;), featuring smooth curves, with the colorbar axis indicating the metric. Many ideas can be developed within this script, such as an axis inversion subroutine, y-axis label customization, or the removal of transparent squares at the colorbar limits (which is highly desirable). &lt;br&gt;
I would like to give credit to JohanC (user:12046409), who provided the code base in this post: &lt;a href="https://stackoverflow.com/questions/8230638/parallel-coordinates-plot-in-matplotlib"&gt;https://stackoverflow.com/questions/8230638/parallel-coordinates-plot-in-matplotlib&lt;/a&gt;. Additionally, I hope that this script I am sharing can be further improved.&lt;/p&gt;

</description>
      <category>matplotlib</category>
      <category>python</category>
      <category>machinelearning</category>
      <category>sweep</category>
    </item>
  </channel>
</rss>
