matplotlib 散点图未使用正确的标记和颜色条修复

yvgpqqbh  于 2023-06-06  发布在  其他
关注(0)|答案(1)|浏览(157)

我正在尝试可视化UMAP,并已经有以下代码和绘图,我的目标是在我的数据集中有两个不同的标记,并为我的数据集中的每个组(组是VP XXX,参见图像中的颜色条)都有一个颜色,实际上已经以某种方式解决了。
问题是,标记不是那些我试图得到和颜色条是不是很准确地告诉我哪个颜色是哪个组。

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

#### prepare lists ####
VP_with_col = []
m=[]
col = []
embedding = [[0,0.8],[0.5,0.5], [0.9,0.5],[0.2,0.9],[0.4,0.4],[0.6,0.5],[0.77,0.59],[0.8,0.1]]
EXAMPLE_VP = ["VP124","VP124","VP125", "VP125", "VP203", "VP203","VP258","VP258"]
EXAMPLE_LABELS = [0,1,0,1,0,1,0,1]
dataframe = pd.DataFrame({"VP": EXAMPLE_VP, "label": EXAMPLE_LABELS})
VP_list = dataframe.VP.unique()
# add color/value to each unique VP
for idx,vp in enumerate(VP_list): 
    VP_with_col.append([1+idx, vp]) #somehow this gives me a different color for each group which is great

#create color array of length len(dataframe.VP) with a color for each group
for idx, vp in enumerate(dataframe.VP):
    for vp_col in VP_with_col:
        if(vp_col[1] == vp):
         col.append(vp_col[0])   

#### create marker list ####
for elem in dataframe.label:
  if(elem == 0):
        m.append("o")
  else:
        m.append("^")

########################## relevant part for question ############################

#### create plot dataframe from lists and UMAP embedding ####
plot_df = pd.DataFrame(data={"x":embedding[:,0], "y": embedding[:,1], "color":col, "marker": m })

plt.style.use("seaborn")   
plt.figure()

#### Plot ####
ax= sns.scatterplot(data=plot_df, x="x",y="y",style= "marker" , c= col, cmap='Spectral', s=5 )
ax.set(xlabel = None, ylabel = None)
plt.gca().set_aspect('equal', 'datalim')

#### Colorbar ####
norm = plt.Normalize(min(col), max(col))
sm = plt.cm.ScalarMappable(cmap="Spectral", norm=norm)
sm.set_array([])

# Remove the legend(marker legend) , add colorbar
ax.get_legend().remove()
cb = ax.figure.colorbar(sm)  

cb.set_ticks(np.arange(len(VP_list)))
cb.set_ticklabels(VP_list)

##### save
plt.title('UMAP projection of feature space', fontsize=12) 
plt.savefig("./umap_plot",dpi=1200)

用标准标记和“x”标记给我绘制了这幅图。在style = "marker"中,dataframe的标记列类似于["^", "o","^","^","^","o"...]

是否也可以更清楚地显示颜色条中的哪个颜色属于哪个类别?

gijlo24d

gijlo24d1#

你做了很多matplotlib在没有Seaborn的情况下所需要的操作。对于Seaborn,大多数情况下是自动的。下面是测试数据的情况:

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

embedding = np.array([[0, 0.8], [0.5, 0.5], [0.9, 0.5], [0.2, 0.9], [0.4, 0.4], [0.6, 0.5], [0.77, 0.59], [0.8, 0.1]])
EXAMPLE_VP = ["VP124", "VP124", "VP125", "VP125", "VP203", "VP203", "VP258", "VP258"]
EXAMPLE_LABELS = [0, 1, 0, 1, 0, 1, 0, 1]
plot_df = pd.DataFrame({"x": embedding[:, 0], "y": embedding[:, 1], "VP": EXAMPLE_VP, "label": EXAMPLE_LABELS})

plt.figure()
plt.style.use("seaborn")

ax = sns.scatterplot(data=plot_df, x="x", y="y",
                     hue='VP', palette='Spectral',
                     style="label", markers=['^', 'o'], s=100)
ax.set(xlabel=None, ylabel=None)
ax.set_aspect('equal', 'datalim')
# sns.move_legend(ax, bbox_to_anchor=(1.01, 1.01), loc='upper left')

plt.tight_layout()
plt.show()

请注意,'Spectral'颜色Map表为“VP203”指定了一个浅黄色,这在默认背景下很难看到。您可能需要使用e.g. palette='Set2'的颜色。

相关问题