python bokeh图例超出了图的大小

时间:2018-07-18 06:05:07

标签: python pandas bokeh bioinformatics qiime

我是python的新手,有人用此代码帮助我,但我想更改一些参数:

首先从图例中找出图例的大小,有些时候图例变大(例如:D_0__细菌; D_1__Firmicutes; D_2__Clostridia; D_3__Clostridiales; D_4__Peptostreptococcaceae; D_5__Acetoanaerobium),有时则很短(Acetoanaerob想要使图例自动修复大小(图中的图例不完整)!!

其次,当指针悬停在条形区域时显示的标签,显示名称和对应数据的值,(hover.tooltips = [('Taxon','example:Acetoanaerobium'), (“值”,“相应的值示例: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://bokeh.pydata.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所说的%,这些值仅是示例!

enter image description here

非常感谢!!!

1 个答案:

答案 0 :(得分:1)

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

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

p.legend.label_text_font_size = "8px"