2

The Problem

When I plot a swarmplot with Seaborn on a linear y-axis, everything works fine. However, when I change the scale to logarithmic I see "ghost data" located at y=10 in the plot. These ghosts seem to be present for all "hued" swarms that are "missing" a swarm, due to NaNs in the data. See a fig of the plot and you will understand what I mean.

As suggested by Stef, moving ax.set_yscale('log') until after making the plot will get rid of the ghots. However, this introduces this bug, and the fix then becomes - you guessed it - moving the scaling statement before the plot statement...

Code (the dataframe is named 'data')

I added a boxplot to better illustrate the ghosts and missing swarms.

import seaborn as sns
import pandas as pd
import numpy as np

fig, ax = plt.subplots(figsize=(15, 10))
ax.set_yscale('log')
    
sns.swarmplot(ax=ax, x='Precision', y='Error', hue='Functional', data=data, dodge=True, color='black', s=7)
sns.boxplot(ax=ax, data=data, x='Precision', y='Error', hue='Functional', whis=np.inf)
    
ax.set_ylim(top=1e2)
ax.get_legend().remove()

enter image description here

A fix

Not scaling the y-axis, but rather scaling the data, fixes the issue. One could then format the y-axis to change the

import seaborn as sns
import pandas as pd
import numpy as np

fig, ax = plt.subplots(figsize=(15, 10))
    
sns.swarmplot(ax=ax, x=data.Precision, y=np.log10(data.Error), hue='Functional', data=data, dodge=True, color='black', s=7)

sns.boxplot(ax=ax, data=data, x='Precision', y=np.log10(data.Error), hue='Functional', whis=np.inf)
    
ax.set_ylim(top=1e2)
ax.get_legend().remove()

enter image description here

Data for reproduction

        Reaction Functional Precision     Error    Delta_E
0    Cr-Alkene-1        PBE       MW4       NaN        NaN
1    Cr-Alkene-2        PBE       MW4       NaN        NaN
2    Cr-Alkene-3        PBE       MW4       NaN        NaN
3    Cr-Alkene-4        PBE       MW4       NaN        NaN
4    Cr-Alkene-5        PBE       MW4       NaN        NaN
5    Cr-Alkene-6        PBE       MW4       NaN        NaN
6          Cr-CO        PBE       MW4       NaN        NaN
7          Cr-H2        PBE       MW4       NaN        NaN
8        Cr-MeCN        PBE       MW4       NaN        NaN
9        Cr-MeOH        PBE       MW4       NaN        NaN
10        Cr-THF        PBE       MW4       NaN        NaN
11      Cr-Water        PBE       MW4       NaN        NaN
12       Fe-MeOH        PBE       MW4       NaN        NaN
13   Ni-Alkene-1        PBE       MW4       NaN        NaN
14   Ni-Alkene-2        PBE       MW4       NaN        NaN
15   Ni-Alkene-3        PBE       MW4       NaN        NaN
16   Ni-Alkene-4        PBE       MW4       NaN        NaN
17   Ni-Alkene-5        PBE       MW4       NaN        NaN
18   Ni-Alkene-6        PBE       MW4       NaN        NaN
19         Ni-CO        PBE       MW4       NaN        NaN
20       Ni-MeCN        PBE       MW4       NaN        NaN
21       Ni-MeOH        PBE       MW4       NaN        NaN
22      Ni-NHC-1        PBE       MW4       NaN        NaN
23      Ni-NHC-2        PBE       MW4       NaN        NaN
24        Ni-THF        PBE       MW4       NaN        NaN
25      Ni-Water        PBE       MW4       NaN        NaN
26   Cr-Alkene-1        PBE       MW5  0.085030 -25.070010
27   Cr-Alkene-2        PBE       MW5  0.038967 -20.737682
28   Cr-Alkene-3        PBE       MW5  0.057310  -9.563396
29   Cr-Alkene-4        PBE       MW5  0.048781 -15.849713
30   Cr-Alkene-5        PBE       MW5  0.174664  -9.809423
31   Cr-Alkene-6        PBE       MW5  0.174896 -16.175515
32         Cr-CO        PBE       MW5  0.027669 -43.785952
33         Cr-H2        PBE       MW5  0.000686 -19.092996
34       Cr-MeCN        PBE       MW5  0.003447 -29.433058
35       Cr-MeOH        PBE       MW5  0.003311 -18.162772
36        Cr-THF        PBE       MW5  0.012966 -19.554907
37      Cr-Water        PBE       MW5  0.201408 -16.410925
38       Fe-MeOH        PBE       MW5  0.061022 -14.408578
39   Ni-Alkene-1        PBE       MW5  0.029701 -16.356436
40   Ni-Alkene-2        PBE       MW5  0.046377 -11.909371
41   Ni-Alkene-3        PBE       MW5  0.048144  -6.596558
42   Ni-Alkene-4        PBE       MW5  0.068907  -8.854051
43   Ni-Alkene-5        PBE       MW5  0.394542  -5.928632
44   Ni-Alkene-6        PBE       MW5  0.088156  -9.083179
45         Ni-CO        PBE       MW5  0.021366 -29.214852
46       Ni-MeCN        PBE       MW5  0.026660 -15.914766
47       Ni-MeOH        PBE       MW5  0.031490  -7.118279
48      Ni-NHC-1        PBE       MW5  0.040673 -36.541574
49      Ni-NHC-2        PBE       MW5  0.275247 -35.818340
50        Ni-THF        PBE       MW5  0.011605  -8.083501
51      Ni-Water        PBE       MW5  0.082603  -5.843024
52   Cr-Alkene-1        PBE       MW6       NaN        NaN
53   Cr-Alkene-2        PBE       MW6       NaN        NaN
54   Cr-Alkene-3        PBE       MW6       NaN        NaN
55   Cr-Alkene-4        PBE       MW6       NaN        NaN
56   Cr-Alkene-5        PBE       MW6       NaN        NaN
57   Cr-Alkene-6        PBE       MW6       NaN        NaN
58         Cr-CO        PBE       MW6       NaN        NaN
59         Cr-H2        PBE       MW6       NaN        NaN
60       Cr-MeCN        PBE       MW6       NaN        NaN
61       Cr-MeOH        PBE       MW6       NaN        NaN
62        Cr-THF        PBE       MW6       NaN        NaN
63      Cr-Water        PBE       MW6       NaN        NaN
64       Fe-MeOH        PBE       MW6       NaN        NaN
65   Ni-Alkene-1        PBE       MW6       NaN        NaN
66   Ni-Alkene-2        PBE       MW6       NaN        NaN
67   Ni-Alkene-3        PBE       MW6       NaN        NaN
68   Ni-Alkene-4        PBE       MW6       NaN        NaN
69   Ni-Alkene-5        PBE       MW6       NaN        NaN
70   Ni-Alkene-6        PBE       MW6       NaN        NaN
71         Ni-CO        PBE       MW6       NaN        NaN
72       Ni-MeCN        PBE       MW6       NaN        NaN
73       Ni-MeOH        PBE       MW6       NaN        NaN
74      Ni-NHC-1        PBE       MW6       NaN        NaN
75      Ni-NHC-2        PBE       MW6       NaN        NaN
76        Ni-THF        PBE       MW6       NaN        NaN
77      Ni-Water        PBE       MW6       NaN        NaN
78   Cr-Alkene-1        PBE       MW7       NaN -24.984980
79   Cr-Alkene-2        PBE       MW7       NaN -20.698715
80   Cr-Alkene-3        PBE       MW7       NaN  -9.620706
81   Cr-Alkene-4        PBE       MW7       NaN -15.898494
82   Cr-Alkene-5        PBE       MW7       NaN  -9.984087
83   Cr-Alkene-6        PBE       MW7       NaN -16.350411
84         Cr-CO        PBE       MW7       NaN -43.758283
85         Cr-H2        PBE       MW7       NaN -19.092310
86       Cr-MeCN        PBE       MW7       NaN -29.429611
87       Cr-MeOH        PBE       MW7       NaN -18.159461
88        Cr-THF        PBE       MW7       NaN -19.541941
89      Cr-Water        PBE       MW7       NaN -16.612333
90       Fe-MeOH        PBE       MW7       NaN -14.469599
91   Ni-Alkene-1        PBE       MW7       NaN -16.326735
92   Ni-Alkene-2        PBE       MW7       NaN -11.955749
93   Ni-Alkene-3        PBE       MW7       NaN  -6.644702
94   Ni-Alkene-4        PBE       MW7       NaN  -8.922958
95   Ni-Alkene-5        PBE       MW7       NaN  -6.323173
96   Ni-Alkene-6        PBE       MW7       NaN  -9.171335
97         Ni-CO        PBE       MW7       NaN -29.236219
98       Ni-MeCN        PBE       MW7       NaN -15.941426
99       Ni-MeOH        PBE       MW7       NaN  -7.149769
100     Ni-NHC-1        PBE       MW7       NaN -36.582247
101     Ni-NHC-2        PBE       MW7       NaN -36.093587
102       Ni-THF        PBE       MW7       NaN  -8.095105
103     Ni-Water        PBE       MW7       NaN  -5.925627
104  Cr-Alkene-1       BP86       MW4  0.907786 -21.462559
105  Cr-Alkene-2       BP86       MW4  1.805265 -15.507719
106  Cr-Alkene-3       BP86       MW4  2.244011  -2.752602
107  Cr-Alkene-4       BP86       MW4  0.214146 -11.449143
108  Cr-Alkene-5       BP86       MW4  5.606964   0.963705
109  Cr-Alkene-6       BP86       MW4  2.078543  -9.815522
110        Cr-CO       BP86       MW4  4.100960 -37.676426
111        Cr-H2       BP86       MW4  1.001208 -16.662864
112      Cr-MeCN       BP86       MW4  1.113604 -26.845561
113      Cr-MeOH       BP86       MW4  1.152023 -15.230304
114       Cr-THF       BP86       MW4  2.439999 -15.047674
115     Cr-Water       BP86       MW4  1.780814 -13.430491
116      Fe-MeOH       BP86       MW4  1.970118  -9.871991
117  Ni-Alkene-1       BP86       MW4  1.132585 -15.362171
118  Ni-Alkene-2       BP86       MW4  1.692244  -7.819692
119  Ni-Alkene-3       BP86       MW4  0.060380  -3.397968
120  Ni-Alkene-4       BP86       MW4  1.503492  -4.246551
121  Ni-Alkene-5       BP86       MW4  2.676600   0.472417
122  Ni-Alkene-6       BP86       MW4  0.911283  -6.691389
123        Ni-CO       BP86       MW4  0.003763 -27.690305
124      Ni-MeCN       BP86       MW4  0.459474 -15.213239
125      Ni-MeOH       BP86       MW4  1.046198  -6.766774
126     Ni-NHC-1       BP86       MW4  0.665764 -33.520115
127     Ni-NHC-2       BP86       MW4  3.266622 -28.420468
128       Ni-THF       BP86       MW4  1.142781  -5.285512
129     Ni-Water       BP86       MW4  1.117142  -5.893955
130  Cr-Alkene-1       BP86       MW5  0.024005 -22.346340
131  Cr-Alkene-2       BP86       MW5  0.035879 -17.277105
132  Cr-Alkene-3       BP86       MW5  0.153085  -4.843528
133  Cr-Alkene-4       BP86       MW5  0.056981 -11.606308
134  Cr-Alkene-5       BP86       MW5  0.235894  -4.407365
135  Cr-Alkene-6       BP86       MW5  0.157669 -11.736396
136        Cr-CO       BP86       MW5  0.080506 -41.696881
137        Cr-H2       BP86       MW5  0.041245 -17.622827
138      Cr-MeCN       BP86       MW5  0.074431 -27.884734
139      Cr-MeOH       BP86       MW5  0.010779 -16.371547
140       Cr-THF       BP86       MW5  0.097629 -17.390044
141     Cr-Water       BP86       MW5  0.041219 -15.170086
142      Fe-MeOH       BP86       MW5  0.136802 -11.978912
143  Ni-Alkene-1       BP86       MW5  0.014608 -14.244193
144  Ni-Alkene-2       BP86       MW5  0.061697  -9.450239
145  Ni-Alkene-3       BP86       MW5  0.081280  -3.256308
146  Ni-Alkene-4       BP86       MW5  0.140095  -5.609949
147  Ni-Alkene-5       BP86       MW5  0.127059  -2.077123
148  Ni-Alkene-6       BP86       MW5  0.061069  -5.719037
149        Ni-CO       BP86       MW5  0.015383 -27.671159
150      Ni-MeCN       BP86       MW5  0.000104 -14.753661
151      Ni-MeOH       BP86       MW5  0.013193  -5.733769
152     Ni-NHC-1       BP86       MW5  0.067696 -34.118182
153     Ni-NHC-2       BP86       MW5  0.483487 -31.203604
154       Ni-THF       BP86       MW5  0.045612  -6.382681
155     Ni-Water       BP86       MW5  0.004972  -4.781785
156  Cr-Alkene-1       BP86       MW6  0.001064 -22.369281
157  Cr-Alkene-2       BP86       MW6  0.002135 -17.310849
158  Cr-Alkene-3       BP86       MW6  0.004392  -4.992222
159  Cr-Alkene-4       BP86       MW6  0.005083 -11.658206
160  Cr-Alkene-5       BP86       MW6  0.008021  -4.635238
161  Cr-Alkene-6       BP86       MW6  0.012484 -11.881580
162        Cr-CO       BP86       MW6  0.003708 -41.773679
163        Cr-H2       BP86       MW6  0.002095 -17.661977
164      Cr-MeCN       BP86       MW6  0.000891 -27.958275
165      Cr-MeOH       BP86       MW6  0.000473 -16.382800
166       Cr-THF       BP86       MW6  0.003316 -17.484357
167     Cr-Water       BP86       MW6  0.000972 -15.210333
168      Fe-MeOH       BP86       MW6  0.002275 -11.844385
169  Ni-Alkene-1       BP86       MW6  0.000860 -14.230445
170  Ni-Alkene-2       BP86       MW6  0.002075  -9.509861
171  Ni-Alkene-3       BP86       MW6  0.013707  -3.323881
172  Ni-Alkene-4       BP86       MW6  0.002953  -5.747091
173  Ni-Alkene-5       BP86       MW6  0.006445  -2.197738
174  Ni-Alkene-6       BP86       MW6  0.002723  -5.777383
175        Ni-CO       BP86       MW6  0.000041 -27.686582
176      Ni-MeCN       BP86       MW6  0.000208 -14.753556
177      Ni-MeOH       BP86       MW6  0.000481  -5.721058
178     Ni-NHC-1       BP86       MW6  0.003489 -34.182390
179     Ni-NHC-2       BP86       MW6  0.009814 -31.677277
180       Ni-THF       BP86       MW6  0.002915  -6.425378
181     Ni-Water       BP86       MW6  0.000449  -4.776363
182  Cr-Alkene-1       BP86       MW7  0.000072 -22.370345
183  Cr-Alkene-2       BP86       MW7  0.000031 -17.312984
184  Cr-Alkene-3       BP86       MW7       NaN  -4.996614
185  Cr-Alkene-4       BP86       MW7       NaN -11.663289
186  Cr-Alkene-5       BP86       MW7       NaN  -4.643259
187  Cr-Alkene-6       BP86       MW7       NaN -11.894064
188        Cr-CO       BP86       MW7       NaN -41.777387
189        Cr-H2       BP86       MW7       NaN -17.664072
190      Cr-MeCN       BP86       MW7       NaN -27.959166
191      Cr-MeOH       BP86       MW7       NaN -16.382327
192       Cr-THF       BP86       MW7       NaN -17.487673
193     Cr-Water       BP86       MW7       NaN -15.211305
194      Fe-MeOH       BP86       MW7       NaN -11.842110
195  Ni-Alkene-1       BP86       MW7  0.000115 -14.229585
196  Ni-Alkene-2       BP86       MW7  0.000045  -9.511936
197  Ni-Alkene-3       BP86       MW7       NaN  -3.337588
198  Ni-Alkene-4       BP86       MW7  0.000015  -5.750044
199  Ni-Alkene-5       BP86       MW7       NaN  -2.204183
200  Ni-Alkene-6       BP86       MW7       NaN  -5.780106
201        Ni-CO       BP86       MW7       NaN -27.686542
202      Ni-MeCN       BP86       MW7       NaN -14.753764
203      Ni-MeOH       BP86       MW7  0.000172  -5.720577
204     Ni-NHC-1       BP86       MW7       NaN -34.185879
205     Ni-NHC-2       BP86       MW7       NaN -31.687090
206       Ni-THF       BP86       MW7       NaN  -6.428293
207     Ni-Water       BP86       MW7  0.000033  -4.776813
Yoda
  • 574
  • 1
  • 9
  • 21
  • I can't explain what exactly leads to this bug but you can suppress the "ghost points" by moving `ax.set_yscale('log')` **after** the plotting functions. – Stef Feb 13 '21 at 20:29
  • You are right, but that introduces this bug: https://stackoverflow.com/questions/40511376/python-seaborn-logarithmic-swarmplot-has-unexpected-gaps-in-the-swarm – Yoda Feb 13 '21 at 20:38
  • Scaling the data directly seems to get rid of the ghosts. – Yoda Feb 13 '21 at 20:47
  • See also [Violin Plot troubles in Python on log scale](https://stackoverflow.com/questions/60131839/violin-plot-troubles-in-python-on-log-scale/60132262#60132262) – JohanC Feb 13 '21 at 21:03

1 Answers1

0

The solution was to scale the data directly (not use the matplotlib API to set axis scale). Then format the y-axis tick labels via the FormatStrFormatter class.

import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter
import numpy as np
import pandas as pd
import seaborn as sns

fig, ax = plt.subplots(figsize=(15, 10))

ax.yaxis.set_major_formatter(FormatStrFormatter(r'$10^%.0f$'))

sns.swarmplot(ax=ax, x='Precision', y=np.log10(data.Error), hue='Functional', data=data, dodge=True, color='black', s=7)
sns.boxplot(ax=ax, data=data, x='Precision', y=np.log10(data.Error), hue='Functional', whis=np.inf)

ax.get_legend().remove()
Yoda
  • 574
  • 1
  • 9
  • 21