繁体   English   中英

python bokeh图例出图大小

[英]python bokeh legend out of the plot size

我是 python 新手,有人帮我写了这段代码,但我想更改一些参数:

首先是图例外的图例大小,有时图例很大(例如:D_0__Bacteria;D_1__Firmicutes;D_2__Clostridia;D_3__Clostridiales;D_4__Peptostreptococcaceae;D_5__Acetoanaerobium),有时它只是短(Acetoanaerium)(Acetoanaer,Acetoana)图例自动修复大小(图中图例不完整)!!!。

其次,当指针悬停在条形区域时出现的标签,显示对应的数据的名称和值,(hover.tooltips = [('Taxon','example: Acetoanaerobium'),('Value ','对应的值例如:99')])

第三:情节(图)的位置,在中间

#!/usr/bin/env python

import pandas as pd
from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.core.properties import value
from bokeh.palettes import Spectral
from bokeh.models import HoverTool
#from bokeh.plotting import figure, output_file, show, ColumnDataSource
import itertools
import sys

data_in = sys.argv[1]
data_out = sys.argv[2]

output_file(data_out + ".html")

df = pd.read_csv(data_in, sep='\t')
df.set_index('#OTU_ID', inplace=True)

#print(df)
s_data = df.columns.values # linia de samples !!!
t_data = df.index.values    #columna de datos

#print(s_data)
#print(t_data)

# You have two rows with 'uncultured' data. I added these together.
# This may or may not be what you want.
df = df.groupby('#OTU_ID')[s_data].transform('sum')

#grouped = df.groupby(["columnA", "columnB"], as_index=False).count()

#print(grouped)

# create a color iterator
# See https://stackoverflow.com/q/39839409/50065
# choose an appropriate pallete from
# https://docs.bokeh.org/en/latest/docs/reference/palettes.html
# if you have a large number of organisms
color_iter = itertools.cycle(Spectral[5])    
colors = [next(color_iter) for organism in t_data]

# create a ColumnDataSource
data = {'xs': list(s_data)}

for organism in t_data:
    data[organism] = list(df.loc[organism])
source = ColumnDataSource(data=data)


#print(organism)
# create our plot
plotX = figure(x_range=s_data, plot_height=500, title="Relative Abundance",
           toolbar_location=None, tools="hover")

plotX.vbar_stack(t_data, x='xs', width=0.93, source=source,
            legend=[value(x) for x in t_data], color=colors)

plotX.xaxis.axis_label = 'Sample'
plotX.yaxis.axis_label = 'Percent (%)'
plotX.legend.location = "bottom_left"
plotX.legend.orientation = "vertical"

# Position the legend outside the plot area
# https://stackoverflow.com/questions/48240867/how-can-i-make-legend-outside-plot-area-with-stacked-bar
new_legend = plotX.legend[0]
plotX.legend[0].plot = None
plotX.add_layout(new_legend, 'below')

hover = plotX.select(dict(type=HoverTool))
hover.tooltips = [('Taxon','unknow_var'),('Value','unknow_var')]
# I don't know what variable to addd in unknow_var

show(plotX)

in 文件是一个 file.txt,制表符分隔的文件,如:

#OTU_ID columnA columnB columnC columnD columnN
D_0__Bacteria;D_1__Actinobacteria;D_2__Acidimicrobiia;D_3__Acidimicrobiales;D_4__uncultured;D_5__uncultured_bacterium   1   3   7   0.9 2
D_0__Bacteria;D_1__Acidobacteria;D_2__Subgroup_25;D_3__uncultured_Acidobacteria_bacterium;D_0__Bacteria;D_1__Actinobacteria;D_2__Actinobacteria;D_3__Streptomycetales;D_4__Streptomycetaceae;D_5__Kitasatospora 5   3   13  7   5
D_0__Bacteria;D_1__Firmicutes;D_2__Bacilli;D_3__Bacillales;D_4__Bacillaceae;D_5__Anoxybacillus  0.1 0.8 7   1   0.4
D_0__Bacteria;D_1__Firmicutes;D_2__Bacilli;D_3__Lactobacillales;D_4__Carnobacteriaceae;D_5__Carnobacterium  3   7   9   16  11
D_0__Bacteria;D_1__Firmicutes;D_2__Bacilli;D_3__Bacillales;D_4__Bacillaceae;D_5__Oceanobacillus 5   2   15  1   7
D_0__Bacteria;D_1__Firmicutes;D_2__Clostridia;D_3__Clostridiales;D_4__Family_XII;D_5__Fusibacter    8   9   0   11  22
D_0__Bacteria;D_1__Firmicutes;D_2__Clostridia;D_3__Clostridiales;D_4__Peptostreptococcaceae;D_5__Acetoanaerobium    99  3   12  1   3
D_4__Clostridiaceae_2;D_5__Alkaliphilus 33  45  1   0   9
D_4__Peptococcaceae;D_5__uncultured 0   3   9   10  11

在这个例子中,值不在 % 中,正如 y-legend 所说,这些值只是一个例子!!!

在此处输入图片说明

非常感谢 !!!

散景图例不会自动调整大小(没有选项可以让他们这样做)。 您需要将图例宽度设置为足够宽以覆盖您可能拥有的任何标签。 此外,由于它们与绘图在同一画布上绘制,因此您需要使绘图更宽,以适应您在图例上设置的宽度。 如果您不希望中央绘图区域变大,您可以在绘图上设置各种min_bordermin_border_left值,以在内部绘图区域周围腾出更多空间。

或者,您可以考虑将图例字体变小,而不是调整绘图和图例的大小。

p.legend.label_text_font_size = "8px"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM