<p dir="auto">Using the UCI HAR Dataset from this paper:<br>
[1] Davide Anguita, Alessandro Ghio, Luca Oneto, Xavier Parra and Jorge L. Reyes-Ortiz. A Public Domain Dataset for Human Activity Recognition Using Smartphones. 21th European Symposium on Artificial Neural Networks, Computational Intelligence and Machine Learning, ESANN 2013. Bruges, Belgium 24-26 April 2013.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import seaborn as sns
import pandas as pd
train_X = pd.read_csv(&quot;./train/X_train.txt&quot;, delim_whitespace=True, header=None)
train_y = pd.read_csv(&quot;./train/y_train.txt&quot;, delim_whitespace=True, header=None)
with open('./features.txt') as file: 
    header = []
    for line in file:
        header.append(line.strip().split()[1])
train_X.columns = header
train_X['labels'] = train_y
train_X['labels'] = train_X['labels'].astype('category')
sns.set_style(&quot;whitegrid&quot;)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">seaborn</span> <span class="pl-k">as</span> <span class="pl-s1">sns</span>
<span class="pl-k">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>
<span class="pl-s1">train_X</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-en">read_csv</span>(<span class="pl-s">"./train/X_train.txt"</span>, <span class="pl-s1">delim_whitespace</span><span class="pl-c1">=</span><span class="pl-c1">True</span>, <span class="pl-s1">header</span><span class="pl-c1">=</span><span class="pl-c1">None</span>)
<span class="pl-s1">train_y</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-en">read_csv</span>(<span class="pl-s">"./train/y_train.txt"</span>, <span class="pl-s1">delim_whitespace</span><span class="pl-c1">=</span><span class="pl-c1">True</span>, <span class="pl-s1">header</span><span class="pl-c1">=</span><span class="pl-c1">None</span>)
<span class="pl-k">with</span> <span class="pl-en">open</span>(<span class="pl-s">'./features.txt'</span>) <span class="pl-k">as</span> <span class="pl-s1">file</span>: 
    <span class="pl-s1">header</span> <span class="pl-c1">=</span> []
    <span class="pl-k">for</span> <span class="pl-s1">line</span> <span class="pl-c1">in</span> <span class="pl-s1">file</span>:
        <span class="pl-s1">header</span>.<span class="pl-en">append</span>(<span class="pl-s1">line</span>.<span class="pl-en">strip</span>().<span class="pl-en">split</span>()[<span class="pl-c1">1</span>])
<span class="pl-s1">train_X</span>.<span class="pl-s1">columns</span> <span class="pl-c1">=</span> <span class="pl-s1">header</span>
<span class="pl-s1">train_X</span>[<span class="pl-s">'labels'</span>] <span class="pl-c1">=</span> <span class="pl-s1">train_y</span>
<span class="pl-s1">train_X</span>[<span class="pl-s">'labels'</span>] <span class="pl-c1">=</span> <span class="pl-s1">train_X</span>[<span class="pl-s">'labels'</span>].<span class="pl-en">astype</span>(<span class="pl-s">'category'</span>)
<span class="pl-s1">sns</span>.<span class="pl-en">set_style</span>(<span class="pl-s">"whitegrid"</span>)</pre></div>
<p dir="auto">then this fails with a <code class="notranslate">cannot label index with a null key</code> message from pandas</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="ax = sns.boxplot(x=&quot;labels&quot;, y=&quot;tBodyAcc-mean()-X&quot;, data=train_X)"><pre class="notranslate"><span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">sns</span>.<span class="pl-en">boxplot</span>(<span class="pl-s1">x</span><span class="pl-c1">=</span><span class="pl-s">"labels"</span>, <span class="pl-s1">y</span><span class="pl-c1">=</span><span class="pl-s">"tBodyAcc-mean()-X"</span>, <span class="pl-s1">data</span><span class="pl-c1">=</span><span class="pl-s1">train_X</span>)</pre></div>
<p dir="auto">while this works</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="ax = sns.boxplot(x=train_X[&quot;labels&quot;], y=train_X[&quot;tBodyAcc-mean()-X&quot;])"><pre class="notranslate"><span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">sns</span>.<span class="pl-en">boxplot</span>(<span class="pl-s1">x</span><span class="pl-c1">=</span><span class="pl-s1">train_X</span>[<span class="pl-s">"labels"</span>], <span class="pl-s1">y</span><span class="pl-c1">=</span><span class="pl-s1">train_X</span>[<span class="pl-s">"tBodyAcc-mean()-X"</span>])</pre></div>
<p dir="auto">The failing code is the same as the one written in the documentation and if I load the "tips" dataset everything works exactly the same as in the documentation. But I can not for the life of me see what the difference is between my DataFrame and "tips" loaded from seaborn.</p>
<p dir="auto">Similar to <a href="https://stackoverflow.com/q/65206970/10447904" rel="nofollow">https://stackoverflow.com/q/65206970/10447904</a>:</p>
<p dir="auto">(searched through GH issues with terms "None y axis inverted" as well as throwing in a "None"/"object" term in there, before opening this issue)</p>
<p dir="auto">The simplest reproducing example I could find was:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="df = pd.DataFrame({'x': [0.0, 0.0, 0.3], 'y': [0.0, None, 0.5]}, dtype='object')
sns.scatterplot(x='x', y='y', data=df)  # y-axis inverted"><pre class="notranslate"><span class="pl-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>({<span class="pl-s">'x'</span>: [<span class="pl-c1">0.0</span>, <span class="pl-c1">0.0</span>, <span class="pl-c1">0.3</span>], <span class="pl-s">'y'</span>: [<span class="pl-c1">0.0</span>, <span class="pl-c1">None</span>, <span class="pl-c1">0.5</span>]}, <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s">'object'</span>)
<span class="pl-s1">sns</span>.<span class="pl-en">scatterplot</span>(<span class="pl-s1">x</span><span class="pl-c1">=</span><span class="pl-s">'x'</span>, <span class="pl-s1">y</span><span class="pl-c1">=</span><span class="pl-s">'y'</span>, <span class="pl-s1">data</span><span class="pl-c1">=</span><span class="pl-s1">df</span>)  <span class="pl-c"># y-axis inverted</span></pre></div>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/13983198/176271810-597c2777-b2a6-4ee6-931f-e56d8f326bfc.png"><img src="https://user-images.githubusercontent.com/13983198/176271810-597c2777-b2a6-4ee6-931f-e56d8f326bfc.png" alt="sns.scatter y inverted" style="max-width: 100%;"></a><br>
^y axis upside down</p>
<p dir="auto">Comparing with plt.scatter (expected, no inverting of y-axis):</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="plt.scatter(x=df['x'], y=df['y'])"><pre class="notranslate"><span class="pl-s1">plt</span>.<span class="pl-en">scatter</span>(<span class="pl-s1">x</span><span class="pl-c1">=</span><span class="pl-s1">df</span>[<span class="pl-s">'x'</span>], <span class="pl-s1">y</span><span class="pl-c1">=</span><span class="pl-s1">df</span>[<span class="pl-s">'y'</span>])</pre></div>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/13983198/176271946-bde8c259-98f2-481d-b0e9-f591c1c88673.png"><img src="https://user-images.githubusercontent.com/13983198/176271946-bde8c259-98f2-481d-b0e9-f591c1c88673.png" alt="matplotlib expected" style="max-width: 100%;"></a></p>
<p dir="auto">According to the stackoverflow post, the problem stems from the <strong>y-axis containing Nones</strong> (and of dtype <code class="notranslate">object</code>).<br>
In my original dataframe, only y is of <code class="notranslate">dtype object</code> (because of the <code class="notranslate">None</code>s, I should have had it as <code class="notranslate">float64</code>), but for this reproducing example I had to cast both to <code class="notranslate">object</code> (in first line) to get it to work - I am confident I can find a reproducing example where x is <code class="notranslate">float64</code> and y <code class="notranslate">object</code> though if necessary.</p>
<p dir="auto">Is this expected behaviour? Given that matplotlib doesn't flip the y axis.<br>
In this case I forgot to cast my column to <code class="notranslate">float64</code> (or to use <code class="notranslate">np.nan</code>s) but I figured <code class="notranslate">None</code>s would be okay (/more or less equivalent) at the time :)</p>