pythonpandasmatplotlibvisualizationupsetplot

How can I add a title and change other plot aesthetics for an UpSet plot in python?


I have installed and imported the following (using Google Colab):

!pip install upsetplot

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt  
import upsetplot
from upsetplot import generate_data, plot
from upsetplot import UpSet
from upsetplot import from_contents

Versions:

...and defined a plot colour:

plot_colour = "#4F84B9"

I have the following pandas dataframe:

df = pd.DataFrame({'File':['File_1', 'File_2', 'File_3'], 
                   'A':[1,1,0],
                   'B':[0,1,1],
                   'C':[1,0,1]})

view of dataframe

I re-shape it to prepare it for an UpSet plot:

files_labelled_A = set(df.loc[df["A"]==1, "File"])
files_labelled_B = set(df.loc[df["B"]==1, "File"])
files_labelled_C = set(df.loc[df["C"]==1, "File"])

contents = {'A': files_labelled_A,
            'B': files_labelled_B,
            'C': files_labelled_C}

from_contents(contents)

view of updated data

I create and view the UpSet plot successfully:

plt = UpSet(from_contents(contents), 
            subset_size='count', 
            facecolor=plot_colour).plot()

view of UpSet plot

How do I add a title and change other plot aesthetics as I usually do with matplotlib plots? When I try adding:

plt.title('my title here')

I get an error:

AttributeError: 'dict' object has no attribute 'title'

I've found some guidance at https://upsetplot.readthedocs.io/en/latest/auto_examples/plot_sizing.html which creates the plot using a different method:

example = generate_counts()
print(example)

plot(example)
plt.suptitle('Defaults')
plt.show()

...and then successfully modifies the aesthetics in the typical matplotlib way, e.g.:

fig = plt.figure(figsize=(10, 3))
plot(example, fig=fig, element_size=None)
plt.suptitle('Setting figsize explicitly')
plt.show()

...but I can't follow this same approach as I don't know how the 'example' data was created using generate_counts(). I don't know how to use this same approach with my data.

Can anyone help me to figure out either how to:

(1) use the approach that uses generate_counts(), or (2) modify my approach so that I can change the matplotlib aesthetics (for example adding a title)?

Full code examples using my data would be appreciated, rather than just descriptions of what to do.


Solution

  • You are shadowing the plt module, instead use:

    d = UpSet(from_contents(contents), 
              subset_size='count', 
              facecolor=plot_colour).plot()
    

    Which assigns to d (not plt!) a dictionary with the axes:

    {'matrix': <AxesSubplot: >,
     'shading': <AxesSubplot: >,
     'totals': <AxesSubplot: >,
     'intersections': <AxesSubplot: ylabel='Intersection size'>}
    

    You can then still use plt, but also access the axes with:

    plt.title('my title here')
    d['totals'].set_title('TITLE')
    

    enter image description here