pythonmatplotlibupsetplot

Python UpSetPlot returning error: 'RangeIndex' object has no attribute 'levels'


I have been trying to get an UpSetPlot to work and have not been having much luck.

My code is quite simple, I have already shaped the data in a CSV. Below is what I have been trying to run.

import pandas as pd
from matplotlib import pyplot as plt
from upsetplot import UpSet
upset = pd.read_csv ("upset.csv")
plot(upset)
pyplot.show()

The data looks like this:

cat0 cat1 cat2 value
FALSE FALSE FALSE 56
FALSE FALSE TRUE 283
FALSE TRUE FALSE 1279
FALSE TRUE TRUE 5882

I based my data off the example used in the UpSetPlot so it should work - Not quite sure where I have gone wrong.

Any feedback would be greatly appreciated!


Solution

  • I think you are missing a step here. After reading the data, you need to convert it to a Upset-compatible format. You can read more about it here.

    Based on the steps mentioned, you can to use from_memberships() to convert it before plotting. This will convert the data to a series with multiple indices which is required. Updated code below. I saved your data in excel instead of csv, but should result in same plot.

    import pandas as pd
    from matplotlib import pyplot as plt
    from upsetplot import UpSet, plot, from_memberships
    upsetraw = pd.read_excel("input.xlsx", 'Sheet17')
    upset = from_memberships(upsetraw)
    plot(upset)
    plt.show()
    

    Plot

    enter image description here