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

Time:01-09

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:

  • Python 3.8.16
  • Numpy version: 1.21.6
  • Pandas version: 1.3.5
  • matplotlib version: 3.2.2
  • upsetplot 0.8.0

...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 enter image description here

  • Related