Search code examples
pythonplotlyviolin-plotplotly-python

Violin plots do not work with negative values in python-plotly


I am trying to plot an interactive violin plot using plotly in python, but it does not work with negative values.

If you run this code, the output (out.html) does not show anything. If you remove all the "-" characters (or even just a couple of them), the violin plot will appear in the output showing the positive values.

import plotly
import numpy as np

outputFile = 'out.html'

data = [-2.728,-2.974,-1.824,-1.885,-2.646,-2.982,-2.538,-3.166,-2.585,-3.131,\
-2.949,-2.943,-2.485,-2.176,-2.240,-2.918,-2.342,-2.934,-1.506,-2.387,-2.899,\
-1.669,-2.840,-1.719,-2.388,-3.255,-2.785,-2.147,-1.800,-2.125,-3.082,-1.594,\
-2.368,-2.870,-4.473,-1.939,-2.819,-3.970,-2.635,-2.803,-2.362,-1.808,-1.562,\
-2.026,-2.682,-2.316,-3.689,-2.073,-2.534,-3.369,-2.398,-1.876,-2.264,-1.823,\
-2.905,-3.032,-2.139,-2.205,-2.468,-3.291,-2.259,-2.599,-1.368,-2.758,-1.734,\
-2.127,-3.392,-2.505,-2.828,-2.354,-3.611,-2.173,-1.964,-2.573,-2.872,-1.920,\
-1.587,-2.963,-3.073,-2.280,-3.292,-2.434,-1.905,-2.417,-3.977,-2.612,-2.770,\
-2.868,-2.318,-2.320,-2.327,-2.595,-1.942,-2.385,-2.203,-2.314,-2.403,-1.781,\
-2.570,-2.677,-2.692,-2.313,-2.859,-2.576,-2.827,-2.250,-1.668,-2.821,-2.291,\
-2.100,-3.346,-1.820,-1.533,-1.992,-1.835,-3.755,-2.940,-3.012,-2.608,-3.479,\
-2.557,-1.579,-1.587,-1.572,-1.844,-2.146,-3.172,-1.940,-1.633,-2.241,-2.029,\
-2.305,-2.689,-2.136,-2.181,-3.437,-2.520,-2.150,-1.709,-1.760,-2.447,-2.333,\
-2.496,-2.654,-3.280,-2.868,-1.831,-2.179,-1.931,-2.362,-2.306,-1.972,-2.471,\
-2.307,-2.362,-3.411,-2.375,-3.119,-1.818,-1.898,-2.099,-2.227,-1.969,-2.096]

dataD = np.array(data)
name = 'NAME'

colorbar_title = ''

final_data=[]

conf = {
    "type": 'violin',
    "x": name,
    "y": dataD,
    "legendgroup": name,
    "scalegroup": name,
    "name": name,
    "side": 'negative',
    "box": {
        "visible" : True
    },
    "points": 'all',
    "pointpos": 0,
    "jitter": 0,
    "scalemode": 'count',
    "meanline": {
        "visible": True
    },
    "line": {
        "color": '#7cc2b6'
    },
    "marker": {
        "size" : 1,
        "line": {
            "width": 0.5,
            "color": '#7cc2b6'
        }
    },
    "span": [
        0
    ],
    "showlegend": False
}
final_data.append(conf)
randomC = {
    "type": 'violin',
    "x": name,
    "y": dataD,
    "legendgroup": name,
    "scalegroup": name,
    "name": name,
    "side": 'positive',
    "box": {
        "visible": True
    },
    "points": 'all',
    "pointpos": 0,
    "jitter": 0,
    "scalemode": 'count',
    "meanline": {
        "visible": True
    },
    "line": {
        "color": '#ada9c9'
    },
    "marker": {
        "size" : 1,
        "line": {
            "width": 0.5,
            "color": '#ada9c9'
        }
    },
    "span": [
        0
    ],
    "showlegend": False
}
final_data.append(randomC)

fig = {
    "data": final_data,
    "layout" : {
        "title": "Change title",
        "yaxis": {
            "rangemode": "normal",
            "autorange": True
        },
        "violingap": 0,
        "violingroupgap": 0,
        "violinmode": "overlay"
    }
}
plotly.offline.plot(fig, filename=outputFile, validate = False)

No error messages. I expected an out.html showing a violin plot with the negative values.


Solution

  • I'm unsure why you've opted to disable validation, but if you enable it, the errors that follow give you an idea of what's wrong with your configuration.

    Specifically:

    • span is incorrectly configured, so remove it or fix it up
    • x should be x0 instead