<p dir="auto">How can I force color mapping of hue to be qualitative instead of sequential when the data is numeric?</p>
<p dir="auto">Below is example of what I am trying to graph. I want the <code class="notranslate">Stage </code>data to be plotted with the <code class="notranslate">hue</code>. However, the seaborn interprets the Stage data as numeric thus resulting in sequential color mapping. I want the colors to be mapped to qualitative.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="
import matplotlib as mpl 
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
%matplotlib widget

# https://elitedatascience.com/wp-content/uploads/2017/04/Pokemon.csv
# https://elitedatascience.com/python-seaborn-tutorial 
df = pd.read_csv(u'Pokemon.csv')

# Drop any pokemon without type 2
df.dropna(axis='rows', subset=['Type 2'], how='any', inplace=True)

# Drop few type 2 for example because when using style='Type 2', there aren't enough markers 
df = df.loc[ ( (df['Type 2'] != 'Ice') &amp;  (df['Type 2'] != 'Water') &amp;  (df['Type 2'] != 'Rock') )]

df.head()

sns.relplot(x='HP', y='Attack', hue='Stage', style='Type 2', legend='full', data=df)
"><pre class="notranslate"><code class="notranslate">
import matplotlib as mpl 
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
%matplotlib widget

# https://elitedatascience.com/wp-content/uploads/2017/04/Pokemon.csv
# https://elitedatascience.com/python-seaborn-tutorial 
df = pd.read_csv(u'Pokemon.csv')

# Drop any pokemon without type 2
df.dropna(axis='rows', subset=['Type 2'], how='any', inplace=True)

# Drop few type 2 for example because when using style='Type 2', there aren't enough markers 
df = df.loc[ ( (df['Type 2'] != 'Ice') &amp;  (df['Type 2'] != 'Water') &amp;  (df['Type 2'] != 'Rock') )]

df.head()

sns.relplot(x='HP', y='Attack', hue='Stage', style='Type 2', legend='full', data=df)

</code></pre></div>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/7905f4e008c2c22b07838934c9c072a06e6be0a4ae646e2d0326a143337e5c10/68747470733a2f2f692e696d6775722e636f6d2f3539494874696c2e706e67"><img src="https://camo.githubusercontent.com/7905f4e008c2c22b07838934c9c072a06e6be0a4ae646e2d0326a143337e5c10/68747470733a2f2f692e696d6775722e636f6d2f3539494874696c2e706e67" alt="alt text" data-canonical-src="https://i.imgur.com/59IHtil.png" style="max-width: 100%;"></a></p>
<p dir="auto">If I try to convert the Stage data to string before plotting, I get an error:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="df[&quot;Stage&quot;] = df[&quot;Stage&quot;].astype(str)
sns.relplot(x='HP', y='Attack', hue='Stage', style='Type 2', legend='full', data=df)
"><pre class="notranslate"><code class="notranslate">df["Stage"] = df["Stage"].astype(str)
sns.relplot(x='HP', y='Attack', hue='Stage', style='Type 2', legend='full', data=df)

</code></pre></div>
<p dir="auto">Error I get is: <code class="notranslate">AttributeError: 'str' object has no attribute 'view'</code></p>
<p dir="auto">However, if I append the Stage after converting to string with another string, I have no issues:<br>
`</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="df[&quot;Stage&quot;] = df[&quot;Stage&quot;].astype(str)
df[&quot;Stage&quot;] = df[&quot;Stage&quot;] + ' | '"><pre class="notranslate"><code class="notranslate">df["Stage"] = df["Stage"].astype(str)
df["Stage"] = df["Stage"] + ' | '
</code></pre></div>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/63e022a95423569f23f0c113d6c231097f6d7fba1d7ddf3684c1b1ca9e6c4aa5/68747470733a2f2f696d6775722e636f6d2f7645784f79697a2e706e67"><img src="https://camo.githubusercontent.com/63e022a95423569f23f0c113d6c231097f6d7fba1d7ddf3684c1b1ca9e6c4aa5/68747470733a2f2f696d6775722e636f6d2f7645784f79697a2e706e67" alt="alt text" data-canonical-src="https://imgur.com/vExOyiz.png" style="max-width: 100%;"></a></p>
<p dir="auto">Related topics:<br>
<a href="https://stackoverflow.com/questions/52884736/why-is-seaborn-creating-an-extra-category-in-my-data" rel="nofollow">https://stackoverflow.com/questions/52884736/why-is-seaborn-creating-an-extra-category-in-my-data</a><br>
<a href="https://stackoverflow.com/questions/51525284/the-hue-parameter-in-seaborn-relplot-skips-an-integer-when-given-numerical-d" rel="nofollow">https://stackoverflow.com/questions/51525284/the-hue-parameter-in-seaborn-relplot-skips-an-integer-when-given-numerical-d</a></p>
<p dir="auto">I brought this proposal to <code class="notranslate">matplotlib</code> earlier (<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="713993499" data-permission-text="Title is private" data-url="https://github.com/matplotlib/matplotlib/issues/18643" data-hovercard-type="issue" data-hovercard-url="/matplotlib/matplotlib/issues/18643/hovercard" href="https://github.com/matplotlib/matplotlib/issues/18643">matplotlib/matplotlib#18643</a>) but it didn't sound like a good fit. The proposed enhancement was more of a re-casting of one plot type (<code class="notranslate">hist2d</code>) to be used in scenarios where a different one (<code class="notranslate">plot</code>) is the standard, rather than a new fundamental plot type, and therefore not general enough for inclusion in the library. One contributor recommended seeing if it might fit here in <code class="notranslate">seaborn</code> since the API is a little more high-level and aims at building off <code class="notranslate">matplotlib</code> without having to slog through the details. I will just copy/paste the proposal here:</p>
<h3 dir="auto">Problem</h3>
<p dir="auto">A while back I was working on a project that involves a bit of time series segmentation/clustering, and found it difficult to visualize my clusters in the "standard" way, which involved simply using <code class="notranslate">plt.plot</code> for each series in my data set and setting <code class="notranslate">alpha</code> to some really low number. I had tens of thousands of such series, and besides taking a long time to run through the loop of plotting some small subsample (plotting everything this way takes an agonizing amount of time), the other major drawback was that apparently there is a lower limit to <code class="notranslate">alpha</code> of <code class="notranslate">1/256</code>, below which nothing would show up. Even with the setting this low, layering large numbers of time series on top of each other just leads to a solid slab of color that makes it difficult to visualize. Yet another factor is that time series with large slopes (both positive and negative) contribute more heavily than series with small slopes to the color of the plot.</p>
<h3 dir="auto">Proposed Solution</h3>
<p dir="auto">I came up with what I think is a fairly nice solution, which is instead of plotting the time series as stacked line plots, to plot them as 2D histograms where each bin counts the number of (potentially <em>interpolated</em>) points that fall in that region. This has a number of advantages:</p>
<ol dir="auto">
<li>way faster than stacking line plots because there is only a single artist being drawn</li>
<li>much more flexible color scheme; colors are automatically normalized or can be changed to represent a log scale, etc.</li>
<li>color scheme is actually indicative of the density of points and does not suffer from the bias of time series with high/low slopes</li>
</ol>
<p dir="auto">I managed to save the code and kind of forgot about it until now, I have it in a repo <a href="https://github.com/ecotner/visualizations/blob/master/timeseries_heatmap/timeseries_heatmap.py">here</a>. There is probably a good deal of improvement that can be done, especially with regards to interpolation techniques.</p>
<h3 dir="auto">Additional context and prior art</h3>
<p dir="auto">These are some old plots from over a year ago, so I don't have access to the data anymore, but you can probably recreate something similar with the code from the repo linked above. Here are some comparisons of the heat map method (left) and standard stacked line plot (right) on some randomly generated toy data:</p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/10482181/94981910-22653c00-0504-11eb-9e75-01507bae1b3c.png"><img src="https://user-images.githubusercontent.com/10482181/94981910-22653c00-0504-11eb-9e75-01507bae1b3c.png" alt="ts_heatmap_comparison_log_scale" style="max-width: 100%;"></a></p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/10482181/94981912-25f8c300-0504-11eb-8c3e-7e5f7174e1c9.png"><img src="https://user-images.githubusercontent.com/10482181/94981912-25f8c300-0504-11eb-8c3e-7e5f7174e1c9.png" alt="ts_heatmap_comparison" style="max-width: 100%;"></a></p>
<p dir="auto">An example of same data with different color schemes:</p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/10482181/94981941-8851c380-0504-11eb-9f6d-6dbe2f0769c6.png"><img src="https://user-images.githubusercontent.com/10482181/94981941-8851c380-0504-11eb-9f6d-6dbe2f0769c6.png" alt="ts_density_color_schemes" style="max-width: 100%;"></a></p>
<p dir="auto">And here are some of the clusters that I was able to find in my data set at the time:</p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/10482181/94982072-948a5080-0505-11eb-9f67-31f6b99584c2.png"><img src="https://user-images.githubusercontent.com/10482181/94982072-948a5080-0505-11eb-9f67-31f6b99584c2.png" alt="2019-05-21_cluster_time_series_heatmap" style="max-width: 100%;"></a></p>
<p dir="auto">Unfortunately I don't think I'm at liberty to divulge what the data actually represents, but it's monthly data, and you can see that there is a bit of interpolation going on between months in order to give it a more "continuous" look.</p>
<p dir="auto">So, what do you think? Would this be a helpful new plot type? It's basically built on top of pre-existing plot types, so it is probably not too difficult to implement!</p>