Search code examples
bar-chartaltair

altair bar chart text issue


this is my code ...please correct so that label will show correct count. tool tip is showing correct count.

bars =alt.Chart(r).transform_fold(
   ['Reservation_count', 'ON_ACCOUNT'],
  as_=['column', 'value']
).mark_bar().encode( 
  x='month:N',
  y='value:Q', 
  color=alt.Color('column:N', scale=alt.Scale(range=["#f50520", "#bab6b7"])),  
  tooltip=alt.Tooltip(['ON_ACCOUNT','Reservation_count']),
   )
text = bars.mark_text(
    align='left',
    color='black',
    baseline='middle',
    dx=0,dy=-8  # Nudges text to right so it doesn't appear on top of the bar
).encode( 
    text=alt.Text('ON_ACCOUNT:Q', format='.0f') 
   ) 

rule = alt.Chart(r).mark_rule(color='red').encode(
    y='mean(Reservation_count):Q'
)   

(bars+text+rule).properties(width=490,height=310)

below is my data:

month   ON_ACCOUNT  Reservation_count
0   1   22          134
1   2   32          137
2   3   22          135
3   4   21          113
4   5   18          120
5   6   17          90
6   7   26          83
7   8   11          86
8   9   11          102
9   10  2           68

please help me with this altair bar chart..two columns are there Reservation_count and ON_ACCOUNT LABEL IS showing the same number for both ...

Thanks

for example last bar showing 2,2 both labels it should be top 2 and bottom 68


Solution

  • The short version answer to your question is to use text=alt.Text('value:Q', format='.0f').

    But there are a few more issues with your code:

    1. You are expanding your text layer from the bar plot, which already has the color parameter set. This will ignore the color setting of the text layer. You can use a data encoding free from any color specification. You can then use this encoding to build your bar and text layers with their color settings.

    2. I encountered the text ordering issue discussed here, and none of the given solutions worked out. However, changing the plot from vertical bar to horizontal bars seem to resolve the issue. This is potentially a bug.

    3. Since you are plotting the mean of Reservation_count parameter on a stacked plot, the information might be misleading. I would suggest a layered bar instead of a stacked one. Surprisingly, this seems to solve the issue of text order.

    Here is the code:

    base =alt.Chart(r).transform_fold(
       ['Reservation_count', 'ON_ACCOUNT'],
      as_=['column', 'value']
    )
    bars = base.mark_bar().encode( 
      x='month:N',
      y=alt.X('value:Q', stack=None), # stack =None enables layered bar
      color=alt.Color('column:N', scale=alt.Scale(range=["#f50520", "#bab6b7"])),  
      tooltip=alt.Tooltip(['ON_ACCOUNT','Reservation_count']),
      #order=alt.Order('color_Category_sort_index:Q'),
       )
    text = base.mark_text(
        
        align='center',
        color='black',
        baseline='middle',
        dx=0,dy=-8,  # Nudges text to right so it doesn't appear on top of the bar
        #order=alt.Order('color_Category_sort_index:Q'),
    ).encode( 
        x='month:N',
        y='value:Q', 
        text=alt.Text('value:Q', format='.0f') 
       ) 
    
    rule = alt.Chart(r).mark_rule(color='red').encode(
        y='mean(Reservation_count):Q'
    )   
    
    (bars+text+rule).properties(width=490,height=310)
    

    enter image description here