user17066714
user17066714

Reputation:

How do you change the gaps between x ticks?

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()

bargraph

Upvotes: 0

Views: 81

Answers (1)

Dushyant
Dushyant

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

Bar plot without spaces between bars

Upvotes: 1

Related Questions