如何在Python中为UpSet图添加标题并更改其他绘图美学?

huangapple go评论83阅读模式
英文:

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:

  • 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 color:

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]})

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)

I create and view the UpSet plot successfully:

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

To add a title and modify other plot aesthetics, you can use the following code:

# Adding a title
plt.title('My Title Here')

# Modifying other aesthetics
upset.ax.set_xlabel('X Label')
upset.ax.set_ylabel('Y Label')

This code will add a title to your UpSet plot and allow you to modify other plot aesthetics.

英文:

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]})

如何在Python中为UpSet图添加标题并更改其他绘图美学?

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)

如何在Python中为UpSet图添加标题并更改其他绘图美学?

I create and view the UpSet plot successfully:

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

如何在Python中为UpSet图添加标题并更改其他绘图美学?

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.

答案1

得分: 1

你正在影子化plt模块,而不是使用:

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

这将分配给d(而不是plt!)一个带有以下轴的字典:

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

然后您仍然可以使用plt,但也可以访问这些轴:

plt.title('我的标题在这里')
d['totals'].set_title('标题')

如何在Python中为UpSet图添加标题并更改其他绘图美学?

英文:

You are shadowing the plt module, instead use:

d = UpSet(from_contents(contents), 
          subset_size=&#39;count&#39;, 
          facecolor=plot_colour).plot()

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

{&#39;matrix&#39;: &lt;AxesSubplot: &gt;,
 &#39;shading&#39;: &lt;AxesSubplot: &gt;,
 &#39;totals&#39;: &lt;AxesSubplot: &gt;,
 &#39;intersections&#39;: &lt;AxesSubplot: ylabel=&#39;Intersection size&#39;&gt;}

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

plt.title(&#39;my title here&#39;)
d[&#39;totals&#39;].set_title(&#39;TITLE&#39;)

如何在Python中为UpSet图添加标题并更改其他绘图美学?

huangapple
  • 本文由 发表于 2023年1月9日 16:10:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054569.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定