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

发布于 2025-01-29 00:15:01 字数 4184 浏览 2 评论 0原文

我有一个pandas dataframe 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()供参考:

{'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}}

我想使用此数据绘制瀑布图。为此,我使用下面的代码绘制了一个堆叠的条形图,并使用列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")

这给了我以下情节:

bar图的默认宽度IE 0.8 根据此文档使栏看起来太狭窄。 我可以在绘图时手动增加宽度。例如,当我在上述代码中使用宽度= 20时,如下所示,

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

我会得到更宽的条形:如图所示:

但是,现在tick和标签的位置被扭曲了。我如何设置条形的宽度,以使条形看起来很宽,杆之间的空间不小或小,X轴中的tick和标签仍然与条形位置相同?

I have a pandas dataframe df which looks as follows:

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:

{'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.

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:
enter image description here

The default width of the bar plot i.e. 0.8 as per this documentation makes the bar look too narrow.
I can increase the width manually while plotting. For example, when I use width = 20 in the above code as follows

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

I get wider bars as shown:
enter image description here

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赢得她心 2025-02-05 00:15:01

问题在于,X轴中的每个点都在为10个不同条的分配空间。因此,您放置的任何尺寸,您将始终有9个空的空间。相反,您应该在绘制之前重组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()

结果:

”在此处输入图像说明“

请注意,贴片索引也更改了,因为我们没有所有空的空白。

The problem is that each point in the x axis is allocating space for 10 different bars. So any size you put, you will always have 9 empty spaces. Instead, you should restructure the df before plotting:

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:

enter image description here

Notice that the patches indices also changed, since we don't have all the empty ones anymore.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文