修改matplotlib条形图中的条宽和条形位置(在容器上循环) [英] Modifying bar-width and bar-position in matplotlib bar-plot (looping over containers)

查看:216
本文介绍了修改matplotlib条形图中的条宽和条形位置(在容器上循环)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试采用以下策略(摘自此处)以调整matplotlib barplots中条形的大小

I'm trying to adapt the following strategy (taken from here) to adjust the sizes of bars in matplotlib barplots

# Iterate over bars
for container in ax.containers:
    # Each bar has a Rectangle element as child
    for i,child in enumerate(container.get_children()):
        # Reset the lower left point of each bar so that bar is centered
        child.set_y(child.get_y()- 0.125 + 0.5-hs[i]/2)
        # Attribute height to each Recatangle according to country's size
        plt.setp(child, height=hs[i])

,但是在基于两列DataFrame的绘图上使用它时遇到了奇怪的行为.代码的相关部分几乎相同:

but have encountered a strange behaviour when using this on a plot based on a two-columns DataFrame. The relevant part of the code is almost identical:

for container in axes.containers:
        for size, child in zip(sizes, container.get_children()):
            child.set_x(child.get_x()- 0.50 + 0.5-size/2)
            plt.setp(child, width=size)

我得到的效果是条形宽度的大小(我在条形图中使用;不是hbar)按预期的方式进行了更改,但是重新居中仅适用于对应于DataFrame的第二列(我将它们交换来检查),对应于下图中的浅蓝色.

The effect I get is that the size of the width of the bars (I'm using in in bar-chart; not an hbar) is changed as intended, but that the re-centering is only applied to the bars that correspond to the second column of the DataFrame (I've swapped them to check), which corresponds to the lighter blue in the figure below.

我不太清楚这是怎么发生的,因为这两个更改似乎都应用在同一循环中.我还发现很难进行故障排除,因为在我的情况下,外循环通过两个容器,而内循环通过的子对象与栏的数量相同(每个容器对应一个子对象).

I don't quite see how this could happen, since both changes seem to be applied as part of the same loop. I also find it difficult to trouble-shoot since in my case the outer-loop goes through two containers, and the inner-loop goes through as many children as there are bars (and this for each container).

如何开始对此进行故障排除?我怎么才能知道我到底在遍历什么呢? (我知道每个孩子都是一个矩形对象,但这还没有告诉我两个容器中的矩形之间的区别)

How could I start troubleshooting this? And how could I find out what I'm actually looping through? (I know each child is a rectangle-object, but this doesn't yet tell me the difference between the rectangles in both containers)

推荐答案

显然,以下方法在修改垂直条形图时效果更好:

Apparently the following approach works better when modifying vertical bar-plots:

for container in axes.containers:
        for i, child in enumerate(container.get_children()):
            child.set_x(df.index[i] - sizes[i]/2)
            plt.setp(child, width=sizes[i])

因此,与我采用的原始方法的主要区别在于,我没有获得容器的当前x_position,而是重新使用DataFrame的索引来将容器的x_position设置为索引减去容器的一半新宽度.

So the main difference with the original approach I was adapting is that I do not get the current x_position of the container, but re-use the index of the DataFrame to set the x_position of the container at the index minus half of its new width.

这篇关于修改matplotlib条形图中的条宽和条形位置(在容器上循环)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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