简体   繁体   English

Matplotlip plot 分组数据条形图增加x轴空间

[英]Matplotlip plot barchart of grouped data increase space of x-axis

I want to make a barchart out of gourped data, but I cannot get a space between the columns on x-axis.我想用 gourped 数据制作一个条形图,但我无法在 x 轴上的列之间获得空格。 I tried to set different figure size and width set to 0.8, but this does not help.我尝试将不同的图形大小和宽度设置为 0.8,但这无济于事。

My code looks like this:我的代码如下所示:

data = [['Tom', 'a'], ['Tom', 'a'],['nick', 'a'], ['juli', 'a'],['juli', 'a'],['juli', 'b'],['Simon', 'b'],['Tom', 'b'],['Sandra', 'a'],['Stefan', 'b'],['Johnny', 'a'],['Johnny', 'b'],['Johnny', 'b'],['Johnny', 'b']]
df = pd.DataFrame(data, columns=['name', 'category'])

ax= df.groupby('name')['category'].value_counts().unstack(0).plot(kind ="bar", width=1.0,figsize = (20, 8), align = 'center', color=['#F9986C', '#C9B265', 'blue', 'red', '#6ABFD0','#C4A5F6','yellow','#6BBCE5',
                                                                                                                               '#E791F6','maroon','green','indigo','teal','lime','darkslategrey','#B6B965','orange'])
plt.ylabel('Count',fontsize=23)
plt.xlabel('Category', fontsize=23)
plt.legend(bbox_to_anchor=(0.99, 0.99), loc='upper right', borderaxespad=0)
plt.show()

And outcome is:结果是: 阴谋

Any suggestions?有什么建议么?

Changing the width of the individual bars in a group isn't currently supported.当前不支持更改组中各个条的宽度。 It also could be quite confusing to know which bars belong to the same group, especially with empty bars involved.知道哪些柱属于同一组也可能会很混乱,尤其是涉及空柱。

A simple workaround could be to set the edgecolor to ec='white' (and lw=1 ).一个简单的解决方法是将 edgecolor 设置为ec='white' (和lw=1 )。

Or you could loop through the generated bars and update their width:或者您可以遍历生成的条并更新它们的宽度:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

data = [['Tom', 'a'], ['Tom', 'a'],['nick', 'a'], ['juli', 'a'],['juli', 'a'],['juli', 'b'],['Simon', 'b'],['Tom', 'b'],['Sandra', 'a'],['Stefan', 'b'],['Johnny', 'a'],['Johnny', 'b'],['Johnny', 'b'],['Johnny', 'b']]
df = pd.DataFrame(data, columns=['name', 'category'])

ax = df.groupby('name')['category'].value_counts().unstack(0).plot(kind ="bar", width=1.0,figsize = (20, 8), align = 'center', color=['#F9986C', '#C9B265', 'blue', 'red', '#6ABFD0','#C4A5F6','yellow','#6BBCE5',

                                                                                                                              '#E791F6','maroon','green','indigo','teal','lime','darkslategrey','#B6B965','orange'])
ax.set_ylabel('Count',fontsize=23)
ax.set_xlabel('Category', fontsize=23)
ax.legend(bbox_to_anchor=(0.99, 0.99), loc='upper right', borderaxespad=0)

factor = 0.9  # reduce the width to 90% of their given width
for bars in ax.containers:
    for bar in bars:
        x, y = bar.get_xy()
        w = bar.get_width()
        bar.set_width(w * factor)
        bar.set_xy((x+w * (1-factor)/2, y))

plt.tight_layout()
plt.show()

分组条形图,改变单个图的宽度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM