简体   繁体   English

主y_range在主轴上的vbar_stacked图和副轴上的线图的绘图中不更新

[英]Primary y_range does not update in Plot of a vbar_stacked plot on primary axis and a line plot on secondary axis

I´m plotting a vbar_stacked and a line plot simultaneous on Bokeh, with bars on the primary y axxis and line on the secondary. 我在Bokeh上同时绘制了vbar_stacked和线图,主要的y轴上有条形,次要的线上有条形。 The plot works fine and when updating data with a option button, the data changes accordingly, the secondary axis aswell, but the primary axis remains the same. 该图工作正常,并且当使用选项按钮更新数据时,数据也会相应更改,辅助轴也将更改,但主轴保持不变。

######## 1. Importing 
import pandas as pd
from bokeh.io import curdoc
from bokeh.layouts import row
from bokeh.palettes import Category20c
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, Select, LinearAxis, Range1d

######## 2. A poor fictional data example
df=pd.DataFrame({'Col1':[45101.023283,41381.466914,7668.987818,
          4682.412413,4677.391936, 4668.987818],
    'Col2':[35101.023283,51381.466914,5668.987818,
          3682.412413,4677.391936, 3668.987818]})

df['Col3']=3*df['Col1']

df['ID_group']='A'
df['ID_group'][3:]='B'

df['ID_sim']=['a','b','c','d','e','f']

df=df.set_index(['ID_group','ID_sim'])

# Defining the col for bar plotting
col_bars=['Col1','Col2']

#Defining the cols for line plotting
col_line='Col3'

######## 3. The excerpt extracted from my original code

### Segmentar o DF para Transformacao em Dados de Torta
df_plot=df.loc['A']

### Convertendo o df_plot em ColumnDataSource
source=ColumnDataSource(df_plot)

### Criacao da Figura para Plot, com coluna_categoria como eixo x
p = figure(x_range=list(df_plot.index))

### ColorMap para Cores da Legenda
cmap=Category20c[20][:len(col_bars)]

### Plot das Colunas
barra = p.vbar_stack(col_bars, x='ID_sim', width=0.9,source=source, 
                 color=cmap, line_color='black', line_width=0.6)

### Impondo o limite minimo do eixo y
p.y_range.start=0

### Impondo o limite máxximo do eixo y
p.y_range.end=max(df_plot[col_bars].sum(axis=1))*1.1  

### Caso coluna_linha nao seja vazio,inserir plot secundario
if list(col_line):
    ### Definir eixo secundario
    p.extra_y_ranges={'y_secundario':Range1d(start=0,
                                                end=float(df_plot[col_line].sum().max())*1.1)}
    ### Adicionar o eixo ao layout
    p.add_layout(LinearAxis(y_range_name='y_secundario'),'right')

    ### Plotar a coluna_linha
    linha = p.line(x='ID_sim',y=col_line,source=source,
               color='firebrick',y_range_name='y_secundario')

    ### Plotando o marcador da linha
    circulo = p.circle(x='ID_sim',y=col_line,source=source,color='firebrick',
                 fill_color='white',size=10,y_range_name='y_secundario')  

### Criacao do Dropbox Menu para Selecao do Conjunto de Analise
select_option = Select(title='ID',
                             options=['A','B'], value='A')


### Criacao de Callback que atualiza o plot conforme selecao do menu select_conjunto         
def update_plot(attr,old,new):
    ### Coleta do valor selecionado no menu select_conjunto
    conjunto = select_option.value

    ### Resegmentar o DF para Transformacao em Dados de Torta
    df_plot=df.loc[conjunto]

    ### Atualizando os dados do plot
    source.data={i:df_plot.reset_index()[i] for i in df_plot.reset_index().columns}

    ### Atualizando o eixo de categorias
    p.x_range.factors=list(df_plot.index)

    ### Atualizando o eixo y principal
    p.y_range.end=max(df_plot[col_bars].sum(axis=1))*1.1

    ### Atualizando eixo y secundário
    p.extra_y_ranges['y_secundario'].end=max(df_plot[col_line])*1.1

select_option.on_change('value',update_plot)

### Criacao do Layout com o Grafico e os Menus    
layout=row(p,select_option,sizing_mode='stretch_width')
curdoc().add_root(layout)


Everything is working fine, except the change of y_range in the screen... when I print the p.y_range.end value it is what was supposed to be... 一切工作正常,除了屏幕上y_range的更改...当我打印p.y_range.end值时,它应该是...

So, I think there is a weird interaction of some sort with the default y-range, owing to the fact that it is an auto-ranging DataRange1d . 因此,我认为默认的y范围存在某种奇怪的交互作用,这是因为它是一个自动调整范围的DataRange1d If you set a plain Range1d instead: 如果改为设置普通的Range1d

### Impondo o limite minimo do eixo y
#p.y_range.start=0

### Impondo o limite máxximo do eixo y
#p.y_range.end=max(df_plot[col_bars].sum(axis=1))*1.1

# Do this instead of the above two lines
p.y_range = Range1d(start=0, end=max(df_plot[col_bars].sum(axis=1))*1.1)

Then things work for me. 然后事情为我工作。

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

相关问题 Plot 在主要和次要 x 和 y 轴上,y 轴反转 - Plot on primary and secondary x and y axis with a reversed y axis 如何将次轴的网格线放在主图后面? - How to put grid lines from the secondary axis behind the primary plot? 使用堆积条形图在辅助轴上绘制线条 - matplotlib - Plot line on secondary axis with stacked bar chart - matplotlib 使用Pandas DF和Matplotlib绘制主轴线和副轴,但图例将一起显示。 - Using Pandas DF and Matplotlib to plot primary(bar) and secondary(line) axis but legends to be shown together..? Python使用辅助y轴制作条形图和折线图 - Python making combined bar and line plot with secondary y-axis Python 散景 plot static 次 y 轴 - Python Bokeh plot static secondary y axis 带有辅助 y 轴的 Pandas 条形图:在图下方隐藏网格线 - Pandas bar plot with secondary y-axis: hide grid line below plot 在热图顶部的辅助 y 轴上创建线 plot 在错误的 x 轴上绘制线 - Create line plot on secondary y axis on top of heatmap draw line on wrong x-axis 分类y轴和日期时间x轴与Bokeh vbar图 - categorical y-axis and datetime x-axis with Bokeh vbar plot 如何在matplotlib条形图中用希腊符号替换主轴和副轴的图例标签? - How to replace legend labels with Greek symbols for primary and secondary axis in a matplotlib bar plot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM