4

I want to annotate the axis of a plot with text like the example chart. To be specific, I would like to annotate regions of the axes with different captions (XYZ, ABC, MNO, etc. shown in red).

I generated the chart using this example (plotting barchart): http://matplotlib.org/examples/api/barchart_demo.html

Could anyone please help me draw such line as well as add text along axes? Any pointer to an example is also appreciated. I am not sure how else to articulate what I want to do here other than describing with a picture.

Example figure showing the text annotation along X and Y axes (shown using XYZ, MNO, etc.) in red

0

1 Answer 1

9

A quick read of the documentation will help, which can be found here. I have used the annotate function that is described in the documentation

Here is a piece of code that will do what you require for the x-axis. The most part of this code is taken from the example you gave a link to in your question.

N = 5
menMeans = (20, 35, 30, 35, 27)
menStd = (2, 3, 4, 1, 2)
ind = np.arange(N)  # the x locations for the groups
width = 0.35       # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(ind, menMeans, width, color='r', yerr=menStd)
womenMeans = (25, 32, 34, 20, 25)
womenStd = (3, 5, 2, 3, 3)
rects2 = ax.bar(ind + width, womenMeans, width, color='y', yerr=womenStd)

# add some text for labels, title and axes ticks
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind + width)
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5'))
ax.legend((rects1[0], rects2[0]), ('Men', 'Women'))

######### annotating the x axis   #########
ax.annotate('', xy=(0, -2),xytext=(3,-2.09),                     #draws an arrow from one set of coordinates to the other
            arrowprops=dict(arrowstyle='<->',facecolor='red'),   #sets style of arrow and colour
            annotation_clip=False)                               #This enables the arrow to be outside of the plot

ax.annotate('xyz',xy=(1.1,-3.8),xytext=(1.3,-3.8),               #Adds another annotation for the text that you want
            annotation_clip=False)


ax.annotate('', xy=(3.1, -2),xytext=(5,-2.09),                   #Repeating for however many arrows you want under the axes
            arrowprops=dict(arrowstyle='<->',facecolor='red'),
            annotation_clip=False)

ax.annotate('abc',xy=(3.6,-3.8),xytext=(3.9,-3.8),
            annotation_clip=False)

######## Can add further annotations for the y-axis here similar to the above ########



# by changing the coorinates of the above you can repeat this for the y axis too
def autolabel(rects):
    # attach some text labels
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
                '%d' % int(height),
                ha='center', va='bottom')
autolabel(rects1)
autolabel(rects2)
plt.show()

This gives the image below:

enter image description here

You will need to reproduce this in order to do the same for the y-axis

Sign up to request clarification or add additional context in comments.

2 Comments

Hm, trying this, it works for text, but I can't see the arrows under the horizontal axis. Do I need the autolabel function? What's the facecolor='red' for? I don't see any red there.
@CGFoX, in newer versions of pyplot, the arrowstyles are specified with words, not tokens. See matplotlib.org/3.2.2/tutorials/text/…. Setting arrowstyle="simple" worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.