英文:
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]})
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:
plt = UpSet(from_contents(contents),
subset_size='count',
facecolor=plot_colour).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.
答案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('标题')
英文:
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')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论