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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:47:28+00:00 2026-06-12T20:47:28+00:00

I’m generating a stacked bar chart in matplotlib (using Python 2.7 on Windows 7).

  • 0

I’m generating a stacked bar chart in matplotlib (using Python 2.7 on Windows 7).

Since I want to use it to pairwise compare data from 2 data-sets, I would like to use different colors for every 2nd bar. Can anyone tell me how to achieve this?

My bar chart looks basically like this:

import numpy
import matplotlib.pyplot as plt


fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)

IDs = ["1","A","2","B","3","C","4","D","5","E"]
N = len(IDs)

property1 = numpy.array([1,3,4,2,3,5,6,7,3,2])
property2 = numpy.array(range(10))
property3 = numpy.array(range(10,0,-1))

ind = numpy.arange(N)
width = 0.8

p1 = ax1.bar(ind, property1, width, color='red')
p2 = ax1.bar(ind, property2, width, color='blue', bottom=property1)
p3 = ax1.bar(ind, property3, width, color='green', bottom=property1 + property2)
plt.xticks(ind+width/2., IDs )

plt.show()
plt.close()

So I want to use one color scheme for the bars labelled with letters, another for those labelled with numbers (since e.g., “1” and “A” form a pair – they represent the same sample under 2 different experimental conditions, which is why I want them next to each other).

Ideally, if width could be adapted to leave no gap between the bars of a pair (but a gap between pairs), that would be really nice.

But right now, I have no idea how to go about this, so any advice would be great!

(I can use both “sets” of data individually, of that is easier? Maybe do two plots with wide gaps, intersected with each other?)

  • 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-12T20:47:30+00:00Added an answer on June 12, 2026 at 8:47 pm

    You can do this more elegantly with slicing. A possibly better solution would be to de-interlace your data.

    IDs = ["1","A","2","B","3","C","4","D","5","E"]
    
    N = len(IDs)
    fig = plt.figure()
    ax1 = fig.add_subplot(1,1,1)
    
    set_count = 2
    set_label = np.vstack([j*ones(len(IDs)//set_count) for j in range(set_count)]).T.ravel()
    
    property1 = numpy.array([1,3,4,2,3,5,6,7,3,2])
    property2 = numpy.array(range(10))
    property3 = numpy.array(range(10,0,-1))
    
    props = [property1,property2,property3]
    
    ind = numpy.arange(N/set_count)
    width = 0.9
    b_width = 0.9/set_count
    
    color_sets = [['red','green','blue'],['black','magenta','yellow']]
    
    for j in range(set_count):
        tmp_accum = np.zeros(len(props[0]))
        for k in range(len(props)):
            ax1.bar(ind +j*b_width, props[k][set_label==j], width=b_width, color=color_sets[j][k], bottom=tmp_accum[set_label==j])
            tmp_accum += props[k]
    
    
    lab_ind = []
    for j in range(set_count):
    
        lab_ind.append(ind + (j+.5)*b_width)
    
    plt.xticks(np.vstack(lab_ind).T.ravel(), IDs )
    

    It needs some sanity checks, but it works and should scale to more than 2 classes and more than 3 properties easily (you just need to make sure that all of your lengths sync up, using itertools.cycle for the colors might help).

    image of results

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.