Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7988541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:29:03+00:00 2026-06-04T12:29:03+00:00

I want to annotate the bars in a graph with some text but if

  • 0

I want to annotate the bars in a graph with some text but if the bars are close together and have comparable height, the annotations are above ea. other and thus hard to read (the coordinates for the annotations were taken from the bar position and height).

Is there a way to shift one of them if there is a collision?

Edit: The bars are very thin and very close sometimes so just aligning vertically doesn’t solve the problem…

A picture might clarify things:
bar pattern

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:29:06+00:00Added an answer on June 4, 2026 at 12:29 pm

    I’ve written a quick solution, which checks each annotation position against default bounding boxes for all the other annotations. If there is a collision it changes its position to the next available collision free place. It also puts in nice arrows.

    For a fairly extreme example, it will produce this (none of the numbers overlap):
    enter image description here

    Instead of this:
    enter image description here

    Here is the code:

    import numpy as np
    import matplotlib.pyplot as plt
    from numpy.random import *
    
    def get_text_positions(x_data, y_data, txt_width, txt_height):
        a = zip(y_data, x_data)
        text_positions = y_data.copy()
        for index, (y, x) in enumerate(a):
            local_text_positions = [i for i in a if i[0] > (y - txt_height) 
                                and (abs(i[1] - x) < txt_width * 2) and i != (y,x)]
            if local_text_positions:
                sorted_ltp = sorted(local_text_positions)
                if abs(sorted_ltp[0][0] - y) < txt_height: #True == collision
                    differ = np.diff(sorted_ltp, axis=0)
                    a[index] = (sorted_ltp[-1][0] + txt_height, a[index][1])
                    text_positions[index] = sorted_ltp[-1][0] + txt_height
                    for k, (j, m) in enumerate(differ):
                        #j is the vertical distance between words
                        if j > txt_height * 2: #if True then room to fit a word in
                            a[index] = (sorted_ltp[k][0] + txt_height, a[index][1])
                            text_positions[index] = sorted_ltp[k][0] + txt_height
                            break
        return text_positions
    
    def text_plotter(x_data, y_data, text_positions, axis,txt_width,txt_height):
        for x,y,t in zip(x_data, y_data, text_positions):
            axis.text(x - txt_width, 1.01*t, '%d'%int(y),rotation=0, color='blue')
            if y != t:
                axis.arrow(x, t,0,y-t, color='red',alpha=0.3, width=txt_width*0.1, 
                           head_width=txt_width, head_length=txt_height*0.5, 
                           zorder=0,length_includes_head=True)
    

    Here is the code producing these plots, showing the usage:

    #random test data:
    x_data = random_sample(100)
    y_data = random_integers(10,50,(100))
    
    #GOOD PLOT:
    fig2 = plt.figure()
    ax2 = fig2.add_subplot(111)
    ax2.bar(x_data, y_data,width=0.00001)
    #set the bbox for the text. Increase txt_width for wider text.
    txt_height = 0.04*(plt.ylim()[1] - plt.ylim()[0])
    txt_width = 0.02*(plt.xlim()[1] - plt.xlim()[0])
    #Get the corrected text positions, then write the text.
    text_positions = get_text_positions(x_data, y_data, txt_width, txt_height)
    text_plotter(x_data, y_data, text_positions, ax2, txt_width, txt_height)
    
    plt.ylim(0,max(text_positions)+2*txt_height)
    plt.xlim(-0.1,1.1)
    
    #BAD PLOT:
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.bar(x_data, y_data, width=0.0001)
    #write the text:
    for x,y in zip(x_data, y_data):
        ax.text(x - txt_width, 1.01*y, '%d'%int(y),rotation=0)
    plt.ylim(0,max(text_positions)+2*txt_height)
    plt.xlim(-0.1,1.1)
    
    plt.show()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class and i want to annotate it as a service. What
I'm looking to add(and remove, dynamically) text annotations to the PlotChart. Specifically I want
I'm trying to annotate some text onto a base image with a dropshadow. I
I want to annotate some of the fields of a given bean class with
I want to annotate a list of objects (which each have a set of
I want to know what actually happens when you annotate a method with @Transactional
I have a bunch of annotated, interrelated POJOs that I want to create/modify/search over
want to have a Hyperlink-Button in a gridView in which I can display a
Want the function to sort the table by HP but if duplicate HPs then
want to ask user to input something but not want to wait forever. There

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.