title: How to change bar label color based on background
body: <p>I'm new to using python for data, but by putting together answers found <a href="https://stackoverflow.com/questions/21397549/stack-bar-plot-in-matplotlib-and-add-label-to-each-section">here</a> and on other sites, I've managed to plot my dataset into a stacked bar chart with bar labels.</p>
  <p>However, I would like the bar labels to have different text colors based on the bar color, to aid in visibility (I'm restricted to grayscale colors for the bars) I've found some examples that manage this but they interfere with other parts of my code and I'm not sure how to reconcile this. Can anyone help?</p>
  <pre class="lang-py prettyprint-override"><code>import pandas as pd
  import matplotlib.pyplot as plt
  
  plt.style.use('ggplot')
  ax = df_new2.plot(kind='barh', stacked=True, figsize=(12, 10), color = ['black','dimgray','darkgray','silver'])
  
  for c in ax.containers:
      
      # customize the label to account for cases when there might not be a bar section
      labels = [f'{w:.0f}%' if (w := v.get_width()) &gt; 0.49 else '' for v in c ] 
      
      # set the bar label
      
      ax.bar_label(c, labels=labels, label_type='center',color='snow',padding=3,fontsize=8)
  
  
  ax.legend(bbox_to_anchor=(1.025, 1), loc='upper left', borderaxespad=0.)
  ax.set_xlim(right=103)
  
  </code></pre>
  <p>update: out of luck with this one still. please let me know if anyone can help.</p>
  <p><a href="https://i.stack.imgur.com/gP0Yp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gP0Yp.png" alt="enter image description here" /></a></p>