Reputation:
How do I make the bars side by side? So I do not have the white gaps.
years = [2010,2015,2020,2021]
photovoltaics = [3549,4351,5000,7200]
hydropower = [1190,2930,3200, 4500]
fig, ax = plt.subplots()
ax.bar(years, photovoltaics, bottom=hydropower, width=1)
ax.bar(years, hydropower, width=1)
ax.set_xticks(years)
fig.tight_layout()
Upvotes: 0
Views: 81
Reputation: 46
A quick fix, not necessarily the best.
You can plot the bar plots with equally spaced x-axis array and then replace the ticks with the years
import matplotlib.pyplot as plt
years = [2010,2015,2020,2021]
photovoltaics = [3549,4351,5000,7200]
hydropower = [1190,2930,3200, 4500]
fig, ax = plt.subplots()
ax.bar(range(len(years)), photovoltaics, bottom=hydropower, width=1)
ax.bar(range(len(years)), hydropower, width=1)
# ax.set_xticks(years)
plt.xticks(range(len(years)),years)
fig.tight_layout()
plt.show()
This is how it will look like, without the white spaces
Upvotes: 1