![](/img/trans.png)
[英]How can I remove borders/frames of subplots while plotting xarray dataset using imshow of matplotlib in Python?
[英]How can I remove space between bars while plotting bar plot using pandas and matplotlib in Python?
我有一個熊貓數據框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")
根據本文檔,條形圖的默認寬度即 0.8 使條形圖看起來太窄。 我可以在繪圖時手動增加寬度。 例如,當我在上面的代碼中使用 width = 20 時如下
fig = df.loc[:,"Current level":].plot(kind = "bar",
bottom = df["Base"],
width = 20,
color = colors)
但是,現在刻度和標簽的位置被扭曲了。 如何設置條形的寬度,使條形看起來很寬,條形之間的空間沒有或很小,並且 x 軸上的刻度和標簽仍與條形處於相同位置?
問題是 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()
結果:
請注意,補丁索引也發生了變化,因為我們不再擁有所有空的索引。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.