您好,我刚刚在
bokeh
中完成了这个情节,所以我想还有很多事情可以改进。尽管如此,最让我困扰的是我无法弄清楚如何在 UMAP 图中包含我的八个种群的所有条目......legend_lable
进行操作。
我实际上想展示的是一个包含所有八个群体(EUR、SIB、AFR、SAS、CEA、OCE、MENA 和 AME)及其相关颜色的图例。 请参阅下面的,了解我使用的代码和绘图示例。如有任何帮助,我们将不胜感激!
import numpy as np
import pandas as pd
import plotly.express as px
import bokeh.plotting as bp
from bokeh.plotting import ColumnDataSource, figure, show
from umap import UMAP
umap = pd.read_csv("SGDP_download/SGDP_bi_snps_norm-2.eigenvec", sep="\t")
umap.rename(columns={"#IID": "#ID"}, inplace=True)
loc = pd.read_csv("SGDP_download/pca_loc_fix_python-order.txt")
colors = pd.read_csv("SGDP_download/bokeh_colors.txt")
eigenval = pd.read_csv("SGDP_download/SGDP_bi_snps_norm-2.eigenval", header=None)
pve = round(eigenval / (eigenval.sum(axis=0))*100, 2)
pve.head()
umap.sort_values('#ID', inplace=True)
umap.insert(loc=1, column='#LOC', value=loc)
umap.rename(columns={'#ID': 'ID', '#LOC': 'LOC'}, inplace=True)
regions_umap = umap.iloc[:, 2:12]
umap_plot = UMAP(n_components=2, init="random", random_state=15)
umap_proj = umap_plot.fit_transform(regions_umap)
#umap_proj.view()
#umap_proj.shape
df = pd.DataFrame(umap_proj, columns=['UMAP1', 'UMAP2'])
df.insert(loc=0, column='population', value=loc)
df.insert(loc=1, column='color', value=colors)
df.index = umap["ID"]
source=ColumnDataSource(df)
#source
df
TOOLS="hover,crosshair,pan,wheel_zoom,zoom_in,zoom_out,box_zoom,undo,redo,reset,tap,save,box_select,poly_select,lasso_select,examine,help"
fig = figure(tools=TOOLS, x_axis_label='UMAP1', y_axis_label='UMAP2')
fig.scatter(x=df['UMAP1'], y=df['UMAP2'], color=df['color'], size=5, legend_label='population',
fill_alpha=0.6, line_color=None)
fig.legend.location = "top_left"
fig.legend.title = "metapopulations"
show(fig)
附注作为旁注,可以将图例放在图的底部,图例标题居中吗?
尝试使用
legend_field
参数而不是 legend_label
,如下所示:
fig.scatter(x=df['UMAP1'], y=df['UMAP2'], color=df['color'], size=5, legend_field='population', fill_alpha=0.6, line_color=None)
我认为您可以使用
fig.legend.location = "bottom_center"
之类的东西将图例向下移动并将其居中。