2

比較するバーの数が多いか少ないかに関係なく、バーの幅を同じに保ちたいです。Matplotlib 積み上げ棒グラフを使用しています。バーの幅は、バーの数に比例します。これが私のサンプルコードです。

1から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()

ありがとうございました

4

1 に答える 1

2

バーの幅は変わらず、画像のスケールが変わります。スケールを同じに保ちたい場合は、プロットが 10x10、100x100、または 1,000,000,000 x 10 のいずれであっても、表示する範囲を手動で指定する必要があります

編集:

私が正しく理解していれば、あなたが望むのは次のようなものです:

グラフ 1 - 2 バー:

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

グラフ 2 - さらに 2 本のバーを追加

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

バーの見かけの幅がグラフ 1 からグラフ 2 まで変化していない場合。これが目的の場合は、プロットのスケールを設定する必要があります。

あなたはそれを行うことができます

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()
于 2010-08-10T11:29:43.323 に答える