views:

48

answers:

1

Dear All,

I want to keep the width of the bars the same no matter the number of bars compared is high or low. I am using Matplotlib stacked bar chart. the width of the bars is relative to the number of the bars. Here is my sample code.

How can I make the width the same no matter the number of bars I compare from 1 to 10

import numpy as np
import matplotlib.pyplot as plt




N =1  
ind = np.arange(N)    # the x locations for the groups
width = 0.35       # the width of the bars: can also be len(x) sequence




design = []
arch = []
code = []

fig = plt.figure()



b   = [70]
a= np.array([73])
c = [66]




p1 = plt.bar(ind, a,width, color='#263F6A')
p2 = plt.bar(ind, b, width, color='#3F9AC9', bottom=a)
p3 = plt.bar(ind, c, width, color='#76787A', bottom=a+b)


plt.ylabel('Scores')
plt.title('CQI Index')


plt.xticks(ind+width/2., ('P1'))#dynamic - fed

plt.yticks(np.arange(0,300,15))


plt.legend( (p1[0], p2[0], p3[0]), ('A','B','C') )
plt.grid(True)

plt.show()

Thank you

+1  A: 

The width of the bars doesn't change, the scale of your image changes. If you want the scale to stay the same you have to manually specify what range you want to show, whether your plot is 10x10, 100x100, or 1,000,000,000 x 10

Edit:

If I understand correctly, what you want is something like this:

Graph 1 - 2 bars:

10
+---------------------------+
|                           |
|                           |
|                           |
|                           |
|                           |
|       4_                  |
|       | |                 |
|  2_   | |                 |
|  | |  | |                 |
|  | |  | |                 |
+---------------------------+ 10

Graph 2 - add 2 more bars

10
+---------------------------+
|                           |
|                           |
|                 7_        |
|                 | |       |
|                 | |       |
|       4_        | |       |
|       | |  3_   | |       |
|  2_   | |  | |  | |       |
|  | |  | |  | |  | |       |
|  | |  | |  | |  | |       |
+---------------------------+ 10

Where the apparent width of the bars hasn't changed from Graph 1 to Graph 2. If this is what you want to do then you'll need to set the scale of your plot

You can do that with

import matplotlib
matplotlib.use('GTKAgg')

import matplotlib.pyplot as plt
import gobject

fig = plt.figure()
ax = fig.add_subplot(111)

def draw1():
    plt.bar(0,2)
    plt.bar(2,4)
    ax.set_xlim((0,10))
    ax.set_ylim((0,10))
    fig.canvas.draw()
    return False

def draw2():
    plt.bar(4,3)
    plt.bar(6,7)

    ax.set_xlim((0,10))
    ax.set_ylim((0,10))
    fig.canvas.draw()
    return False

draw1()
gobject.timeout_add(1000, draw2)
plt.show()
Wayne Werner
I agree, the width is always as specified at the beginning.What I actually meant is that I want the scale to change automatically so as the bars dont get squeezed to fit the plot area.I mean the plotting area should be dynamic. So instead of dividing the plotting area over the number of bars we could just add one to another till 10 bars.
Atlas
I added an edit... is that what you're looking for?
Wayne Werner
Sorry if I did not express myself clearly. Adding graphs is not the functionality I am looking for. It was rather an analogy.I dont want to add bars. I just wanted to explain that the width should look the same no matter is the number of bars in the figure. They should not divide the figure otherwise the more bars we need, the smaller the graphs we well get.They should rather force the figure to expand a little bit to allow that they would look the same. Otherwise, if we plot one bar it should not take the whole space as it does with my example above.Thank you
Atlas
Here is my question answered but I could not figure out what did the guy meanhttp://www.mail-archive.com/[email protected]/msg04225.html
Atlas
That's exactly the point I was trying to make - you have to set the scale using `.set_xlim((min, max))` and `.set_ylim((min, max))`. You'll have to decide what the min and max need to be based on what the size of your dataset is.
Wayne Werner
Oh Gosh, that is so cool...Thank you very much. Now I will need to center them. Thanks a million
Atlas
You're welcome. Another way of saying thank you is marking answers as "accepted" by clicking the green check mark (as well as up-voting the useful ones).
Wayne Werner