问题描述
有时,数据集具有许多变量,并且选择了其他有助于它们的事物".显示对这些不同事物"的变量的贡献(例如,%)可能会很有用.但是,有时并非所有的事物"都对所有变量有所贡献.当绘制为条形图时,如果特定变量没有来自事物"的贡献,则会导致空格.如果事物"的贡献为零,是否有一种方法可以不只在条形图中绘制变量的特定条形?
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 (e.g. %) 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?
下面的示例显示了变量(a-j)的选择,这些变量具有可能有助于变量(1-5)的各种因素.注意:当事物"(1-5)对变量(a-j)的贡献为零时的间隙.
An example below shows a selection of variables (a-j) 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 (a-j) 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()
输出:
这篇关于您如何删除条形图中条形图之间零值的地方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!