简体   繁体   中英

How do you remove spaces between bars in bar charts for where plotted values are zero?

Sometimes datasets have a number of variables with a selection of other 'things' that contribute to them. It can be useful to show the contribution (eg %) to a variable of these different 'things'. However, sometimes not all of the 'things' contribute to all of the variables. When plotting as a bar chart, this leads to spaces when a specific variable does not have a contribution from a 'thing'. Is there a way to just not plot the specific bar for a variable in a bar chart if the contribution of the 'thing' is zero?

An example below shows a selection of variables (aj) that have various things that could contribute to them (1-5). NOTE: the gaps when the contribution of a 'thing' (1-5) to a variable (aj) is zero.

from random import randrange 
# Make the dataset of data for variables (a-j)
columns = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
data = np.array([np.random.randn(5)**2 for i in range(10)])
df = pd.DataFrame(data.T, columns=columns)
for col in df.columns:  
    # Set 3 of the 5 'things' to be np.NaN per column
    for n in np.arange(3):
        idx = randrange(5) 
        df.loc[list(df.index)[idx], col] = np.NaN
    # Normalise the data to 100% of values
    df.loc[:,col] = df[col].values / df[col].sum()*100

# Setup plot
figsize = matplotlib.figure.figaspect(.33)
fig = plt.figure(figsize=figsize)
ax = plt.gca()
df.T.plot.bar(rot=0, ax=ax)     
# Add a legend and show
plt.legend(ncol=len(columns))
plt.show()

结果图

As commented, there's no inbuilt function for this. Here's an approach that you can explore:

# we will use this to shift the bars
shifted = df.notnull().cumsum()

# the width for each bar
width = 1 / len(df.columns)

fig = plt.figure(figsize=(10,3))
ax = plt.gca()
colors = [f'C{i}' for i in range(df.shape[1])]
for i,idx in enumerate(df.index):
    offsets = shifted.loc[idx]
    values = df.loc[idx]
    ax.bar(np.arange(df.shape[1]) + offsets*width, values, 
           color=colors[i], width=width, label=idx)
    
ax.set_xticks(np.arange(df.shape[1]))
ax.set_xticklabels(df.columns);
ax.legend()

Output:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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