简体   繁体   English

如何在 Python 中使用 pandas 和 matplotlib 绘制条形图时删除条形之间的空间?

[英]How can I remove space between bars while plotting bar plot using pandas and matplotlib in Python?

I have a pandas dataframe df which looks as follows:我有一个熊猫数据框df ,如下所示:

Base    Current level   New fan New refrigerator    Unplug unused appliances    Run washing machine with full load  Fix leakages    After three months  Install smart thermostat    Replace light bulbs with LED lights Replace desktop with laptop After six months
0   0   150.0   0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
1   150 0.0 10.0    0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
2   160 0.0 0.0 15.0    0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
3   160 0.0 0.0 0.0 15.0    0.0 0.0 0.0 0.0 0.0 0.0 0.0
4   145 0.0 0.0 0.0 0.0 15.0    0.0 0.0 0.0 0.0 0.0 0.0
5   140 0.0 0.0 0.0 0.0 0.0 5.0 0.0 0.0 0.0 0.0 0.0
6   0   0.0 0.0 0.0 0.0 0.0 0.0 140.0   0.0 0.0 0.0 0.0
7   115 0.0 0.0 0.0 0.0 0.0 0.0 0.0 25.0    0.0 0.0 0.0
8   105 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 10.0    0.0 0.0
9   95  0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 10.0    0.0
10  0   0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 95.0

df.to_dict() is given for reference: df.to_dict()供参考:

{'Base': {0: 0,
  1: 150,
  2: 160,
  3: 160,
  4: 145,
  5: 140,
  6: 0,
  7: 115,
  8: 105,
  9: 95,
  10: 0},
 'Current level': {0: 150.0,
  1: 0.0,
  2: 0.0,
  3: 0.0,
  4: 0.0,
  5: 0.0,
  6: 0.0,
  7: 0.0,
  8: 0.0,
  9: 0.0,
  10: 0.0},
 'New fan': {0: 0.0,
  1: 10.0,
  2: 0.0,
  3: 0.0,
  4: 0.0,
  5: 0.0,
  6: 0.0,
  7: 0.0,
  8: 0.0,
  9: 0.0,
  10: 0.0},
 'New refrigerator': {0: 0.0,
  1: 0.0,
  2: 15.0,
  3: 0.0,
  4: 0.0,
  5: 0.0,
  6: 0.0,
  7: 0.0,
  8: 0.0,
  9: 0.0,
  10: 0.0},
 'Unplug unused appliances': {0: 0.0,
  1: 0.0,
  2: 0.0,
  3: 15.0,
  4: 0.0,
  5: 0.0,
  6: 0.0,
  7: 0.0,
  8: 0.0,
  9: 0.0,
  10: 0.0},
 'Run washing machine with full load': {0: 0.0,
  1: 0.0,
  2: 0.0,
  3: 0.0,
  4: 15.0,
  5: 0.0,
  6: 0.0,
  7: 0.0,
  8: 0.0,
  9: 0.0,
  10: 0.0},
 'Fix leakages': {0: 0.0,
  1: 0.0,
  2: 0.0,
  3: 0.0,
  4: 0.0,
  5: 5.0,
  6: 0.0,
  7: 0.0,
  8: 0.0,
  9: 0.0,
  10: 0.0},
 'After three months': {0: 0.0,
  1: 0.0,
  2: 0.0,
  3: 0.0,
  4: 0.0,
  5: 0.0,
  6: 140.0,
  7: 0.0,
  8: 0.0,
  9: 0.0,
  10: 0.0},
 'Install smart thermostat': {0: 0.0,
  1: 0.0,
  2: 0.0,
  3: 0.0,
  4: 0.0,
  5: 0.0,
  6: 0.0,
  7: 25.0,
  8: 0.0,
  9: 0.0,
  10: 0.0},
 'Replace light bulbs with LED lights': {0: 0.0,
  1: 0.0,
  2: 0.0,
  3: 0.0,
  4: 0.0,
  5: 0.0,
  6: 0.0,
  7: 0.0,
  8: 10.0,
  9: 0.0,
  10: 0.0},
 'Replace desktop with laptop': {0: 0.0,
  1: 0.0,
  2: 0.0,
  3: 0.0,
  4: 0.0,
  5: 0.0,
  6: 0.0,
  7: 0.0,
  8: 0.0,
  9: 10.0,
  10: 0.0},
 'After six months': {0: 0.0,
  1: 0.0,
  2: 0.0,
  3: 0.0,
  4: 0.0,
  5: 0.0,
  6: 0.0,
  7: 0.0,
  8: 0.0,
  9: 0.0,
  10: 95.0}}

I want to plot a waterfall chart using this data.我想使用这些数据绘制瀑布图。 For this, I plotted a stacked bar plot using the code below and using column Base as bottom.为此,我使用下面的代码并使用Base列作为底部绘制了堆积条形图。

colors = ["royalblue","green","green","red","red","red","royalblue",
        "red","red","red","royalblue"]

fig = df.loc[:,"Current level":].plot(kind = "bar",
                               bottom = df["Base"],
                     
                               color = colors)


selected_patches = fig.patches[0], fig.patches[20], fig.patches[40]


plt.legend(selected_patches, ["Base", "Rise", "Fall"], loc = "upper right")

plt.xticks(ticks = np.arange(0, len(df)), labels = df.columns[1:], rotation = 90)

plt.title("My electricity saving plan")

plt.ylabel("kWh consumption")

This returned me the following plot:这给了我以下情节: 在此处输入图像描述

The default width of the bar plot ie 0.8 as per this documentation makes the bar look too narrow.根据本文档,条形图的默认宽度即 0.8 使条形图看起来太窄。 I can increase the width manually while plotting.我可以在绘图时手动增加宽度。 For example, when I use width = 20 in the above code as follows例如,当我在上面的代码中使用 width = 20 时如下

fig = df.loc[:,"Current level":].plot(kind = "bar",
                               bottom = df["Base"],
                                      width = 20,
                               color = colors)

I get wider bars as shown:如图所示,我得到了更宽的条: 在此处输入图像描述

However, now the position of ticks and labels is distorted.但是,现在刻度和标签的位置被扭曲了。 How can I set the width of the bars such that the bars look wide, the space between bars is none or small, and the ticks and labels in the x-axis are still in the same position as bars?如何设置条形的宽度,使条形看起来很宽,条形之间的空间没有或很小,并且 x 轴上的刻度和标签仍与条形处于相同位置?

The problem is that each point in the x axis is allocating space for 10 different bars.问题是 x 轴上的每个点都为 10 个不同的条分配空间。 So any size you put, you will always have 9 empty spaces.所以你放任何尺寸,你总是有 9 个空格。 Instead, you should restructure the df before plotting:相反,您应该在绘图之前重组 df :

import matplotlib.pyplot as plt

colors = ["royalblue","green","green","red","red","red","royalblue",
        "red","red","red","royalblue"]

fig = df.loc[:,"Current level":].T.max(axis=1).plot(kind='bar', bottom=df['Base'], width=1, color=colors)

selected_patches = fig.patches[0], fig.patches[2], fig.patches[4]
plt.legend(selected_patches, ["Base", "Rise", "Fall"], loc = "upper right")

plt.xticks(ticks = np.arange(0, len(df)), labels = df.columns[1:], rotation = 90)

plt.title("My electricity saving plan")

plt.ylabel("kWh consumption")
plt.show()

Result:结果:

在此处输入图像描述

Notice that the patches indices also changed, since we don't have all the empty ones anymore.请注意,补丁索引也发生了变化,因为我们不再拥有所有空的索引。

暂无
暂无

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

相关问题 如何在 Python 中使用 matplotlib 的 imshow 绘制 xarray 数据集时删除子图的边框/框架? - How can I remove borders/frames of subplots while plotting xarray dataset using imshow of matplotlib in Python? 在 python 中绘制条形 plot 时,如何在 3 列的 pandas 数据框中将其堆叠为 2 列而不是为一列堆叠? - While plotting bar plot in python, how can I make it stacked for 2 columns and not stacked for one column in a pandas data frame of 3 columns? Matplotlib条形图,条形图相互重叠,如何创建空间 - Matplotlib bar plot, bars is on top of each other, how to create space how to remove empty space from bars for a specific group, that was plotted as seaborn bar plot on data in pandas dataframe - how to remove empty space from bars for a specific group, that was plotted as seaborn bar plot on data in pandas dataframe 如何使用 python 制作带有误差线的 plot 条形图? - How do I plot bar graphs with error bars using python? 如何在 matplotlib 中绘制 5 个分组的条形图? - How can a plot a 5 grouped bars bar chart in matplotlib? 如何在Matplotlib条形图中删除宽度不等的条之间的间隙 - How to remove gaps between bars with unequal widths in Matplotlib bar chart 如何消除 Matplotlib 条形图中条形之间的间隙 - How to remove gaps between bars in Matplotlib bar chart 如何在使用 Python 中的 matplotlib 在同一子图中绘制多个 geopandas 数据帧时添加图例? - How can I add legend while plotting multiple geopandas dataframes in the same subplot using matplotlib in Python? 如何减少 python plot 中不同条形组之间的空间 - how to reduce space between different groups of bars in python plot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM