无论我们在图中比较的条数是多少,如何保持条的宽度相同? [英] How to keep the width of the bars the same no matter the number of bars we compare in the figure?

查看:33
本文介绍了无论我们在图中比较的条数是多少,如何保持条的宽度相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论要比较的条数是高还是低,我都希望条的宽度保持相同.我正在使用Matplotlib堆积条形图.条的宽度是相对于条的数量.这是我的示例代码.

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.

无论我比较的条数是多少(从1到10),如何使宽度相同?

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()

谢谢

推荐答案

条形的宽度不会改变,图像的比例也会改变.如果您希望比例尺保持不变,则必须手动指定要显示的范围,无论您的绘图是10x10、100x100还是1,000,000,000 x 10

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

如果我理解正确,那么您想要的是这样的东西:

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

图表1-2条:

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

图2-再添加2个条形图

Graph 2 - add 2 more bars

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

从图1到图2的条形表观宽度没有变化的情况.如果这是您要执行的操作,则需要设置图的比例尺

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

您可以使用

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()

这篇关于无论我们在图中比较的条数是多少,如何保持条的宽度相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆