Violins plot as vertical lines


#1

I am having trouble with the python violin plotting. The output plot (below) plots vertical lines instead of violins. The code I’ve included was correctly plotting violins earlier this week for me, but in case I inadvertently changed something to cause this, I thought I’d post this on here.

I have been using the example "Multiple Traces’ from here: https://plot.ly/python/violin/

expression is a pandas dataframe with columns raw_value and cell_type:

import pandas as pd
import plotly.offline as py

expression = pd.DataFrame({'raw_value': [25.0, 16.0, 98.0, 14.0, 14.0, 99.0, 6.0, 15.0, 32.0, 27.0, 93.0, 57.0, 37.0, 50.0, 40.0, 70.0, 33.0, 97.0, 9.0, 0.0, 53.0, 69.0, 48.0, 10.0, 75.0], 'cell_type': ['Inner Hair Cells', 'Outer Hair Cells', 'Outer Hair Cells', 'Outer Hair Cells', 'Inner Hair Cells', 'Inner Hair Cells', 'Outer Hair Cells', 'Outer Hair Cells', 'Outer Hair Cells', 'Outer Hair Cells', 'Inner Hair Cells', 'Inner Hair Cells', 'Outer Hair Cells', 'Outer Hair Cells', 'Outer Hair Cells', 'Inner Hair Cells', 'Inner Hair Cells', 'Outer Hair Cells', 'Outer Hair Cells', 'Inner Hair Cells', 'Outer Hair Cells', 'Outer Hair Cells', 'Outer Hair Cells', 'Outer Hair Cells', 'Outer Hair Cells']})


celltypes = expression['cell_type'].unique()
celltype_count = len(celltypes)
data = []
for i in range(0, celltype_count):
    trace = {
        "type": 'violin',
        "x": expression['cell_type'][expression['cell_type'] == celltypes[i]],
        "y": expression['raw_value'][expression['cell_type'] == celltypes[i]],
        "name": celltypes[i],
        "box": {
            "visible": True
        },
        "meanline": {
            "visible": True
        }
    }
    data.append(trace)

fig = {
    "data": data,
    "layout" : {
        "title": "",
        "yaxis": {
            "zeroline": False,
        },
        "legend": {
            "orientation": "h"
        },
        "height": 350,
        "width": 400,
        "margin": {
            "l": 50,
            "r": 50,
            "b": 75,
            "t": 25,
            "pad": 4
        }
    }
}
html = py.plot(fig, output_type='div', validate=False, include_plotlyjs=False, show_link=False)

Output:
image

Thank you in advance!


#2

Hi @dolleyj,

Could you update your code with a literal definition of the expression data frame? It’s much easier for people to help out if they can copy your entire code cell, paste it into a Jupyter Notebook cell, run it, and see what you’re seeing.

One way to do this is to print out the data frame as a dict as follows. e.g.

>>> print(expression.to_dict(orient='list'))
{'a': [2, 3, 4], 'b': [4, 3, 2]}

And then copy the result and paste it into the DataFrame constructor.

expression = pd.DataFrame({'a': [2, 3, 4], 'b': [4, 3, 2]})

Thanks!


#3

Thank you @jmmease. I updated my post.


#4

Hi @dolleyj,

If I run your example as-is nothing is plotted in the notebook because the html div text is just assigned to the html variable.

I wasn’t able to reproduce the image in your original post. But, if I replace your plot command with:

py.plot(fig, validate=False, include_plotlyjs=True, show_link=False)

My browser opens a standalone html page with this image:

plot%20from%20API%20(6)

If you don’t see the same behavior could you check what version of plotly.py you have installed?

import plotly
plotly.__version__