无论我们在图中比较的条形数量有多少,如何保持条形的宽度相同?

发布于 2024-09-13 20:27:12 字数 807 浏览 6 评论 0原文

我想保持条形的宽度相同,无论比较的条形数量是高还是低。 我正在使用 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()

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅浅淡淡 2024-09-20 20:27:12

条形的宽度不会改变,图像的比例会改变。如果您希望比例保持不变,则必须手动指定要显示的范围,无论您的绘图是 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()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文