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 回答