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 520949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:11:56+00:00 2026-05-13T08:11:56+00:00

I want to plot a confusion matrix using Pylab. The class labels along the

  • 0

I want to plot a confusion matrix using Pylab. The class labels along the horizontal axis are long, so I want to plot them rotated vertically. However, I also want to plot them on top of the axis, not below.

This command can plot vertical labels on bottom:

pylab.imshow(confusion_matrix)
pylab.xticks(..., rotation='vertical')

and this command can plot horizontal labels on top without rotation:

pylab.matshow(confusion_matrix)

but I cannot find anything that does both. The following command does not work.

pylab.matshow(confusion_matrix)
pylab.xticks(..., rotation='vertical')

Can you suggest a way to plot a confusion matrix with xticks on top of the axis with vertical rotation? Thank you.

EDIT

Thank you, Mark, for your help. It got me on the right track by inspecting the tick properties more closely. The only difference with your answer and my desired answer is applying that idea to an AxesImage, not a plot. After investigation, here is the answer:

im = pylab.matshow(confusion_matrix)
for label in im.axes.xaxis.get_ticklabels():
    label.set_rotation(90)
im.figure.show()

To those reading… don’t forget about show()! I forgot that I needed to refresh the figure. See output below.

Confusion matrix with vertical labels. http://up.stevetjoa.com/rotate_ticklabels.png

  • 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-05-13T08:11:56+00:00Added an answer on May 13, 2026 at 8:11 am

    If I understand you correctly, this will get you close. You might have to ‘pad’ your labels out with spaces to move them off the xaxis line.

    from matplotlib import pylab 
    pylab.plot([0, 6], [0, 6])
    pylab.xticks([1,2,3,4,5,6],('one','two','three','four','five','six'),rotation='vertical',verticalalignment='bottom')
    

    EDIT IN RESPONSE TO COMMENT

    If you want them rotated vertical on the top x-axis, try this:

    pylab.plot([0, 6], [0, 6])
    pylab.xticks([1,2,3,4,5,6],('one','two','three','four','five','six'))
    for tick in pylab.gca().xaxis.iter_ticks():
        tick[0].label2On = True
        tick[0].label1On = False
        tick[0].label2.set_rotation('vertical')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently have a nx3 matrix array. I want plot the three columns
I want to plot a 2d rect (using the rectangle function is good enough
I want to create a Java application bundle for Mac without using Mac. According
I want to log onto Stack Overflow using OpenID, but I thought I'd set
I am plotting a confusion matrix with matplotlib with the following code: from numpy
I want to plot some confidence interval graphs in MATLAB but I don't have
I want to plot a line from one well-defined point to another and then
I want to plot stacked histograms in R; i.e. stack individual histograms in the
I want to plot some data. The first column contains the x-data. But matplotlib
I want to plot a 3d graph in matlab By graph I mean in

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.