带错误条的python altair堆叠条形图

2024-09-30 06:11:23 发布

您现在位置:Python中文网/ 问答频道 /正文

在python altair(版本4.1.0)中,是否可以使用错误条进行堆叠条形图绘制? 与此matplotlib示例类似:https://matplotlib.org/3.2.1/gallery/lines_bars_and_markers/bar_stacked.html

即使知道这是不可能的,因为这是不可能做到这一点与织女星ligt版本2.X将是有用的知道

以下是我迄今为止的尝试:

# library                                                                                                
import altair as alt                                                                                     
import pandas as pd                                                                                      
                                                                                                         
# generate data                                                                                          
df = pd.DataFrame(                                                                                       
    [                                                                                                    
        ['pertu1', 'pertu1', 'pertu1', 'pertu1', 'pertu2', 'pertu2', 'pertu2', 'pertu2'],  # perturbation
        ['state1', 'state2', 'state1', 'state2', 'state1', 'state2', 'state1', 'state2'],  # state
        [0.5, 0.5, 0.6, 0.4, 0.2, 0.8, 0.4, 0.6],  # fraction
    ],                                                                                                   
    index = ['perturbation','state','fraction']                                                          
).T                                                                                                      
                                                                                                         
                                                                                                         
# plot                                                                                                   
o_chart_bar = alt.Chart(df).mark_bar().encode(                                                           
    x='perturbation',                                                                                    
    y=alt.Y(                                                                                             
        'fraction',                                                                                      
        aggregate='mean',                                                                                
    ),                                                                                                   
    color=alt.Color(
        'state', 
        scale=alt.Scale(domain=['state1','state2'], range=['orange','red']),
    ),       
)                                                                                                        
                                                                                                         
o_chart_error = alt.Chart(df).mark_errorbar(extent='ci').encode(                                       
    x='perturbation',                                                                                      
    y='fraction',                                                                                          
    color=alt.Color(
        'state', 
        scale=alt.Scale(domain=['state1','state2'], range=['maroon','black']),
    ),        
)                                                                                             

o_chart_error.save('error.png')                                                                          

o_chart_bar.save('stackedbar.png')

o_chart = o_chart_bar + o_chart_error                                                                    
o_chart.save('stackedbar_error.png')

stacked bar plot"stacked" error confidence intervalstacked bar and error confidence interval overlay

正如你所看到的,它并不是这样工作的。 我不确定这是否可能


Tags: dfpngsavechartbarerroraltstate

热门问题