Home > Software engineering >  How to remove distance between two bars in matplotlib?
How to remove distance between two bars in matplotlib?

Time:11-07

I am trying to plot bar graphs in matplotlib. I have set frequency of X-axis manually in matplotlib by using plt.xticks. But i can not remove the distance between two bars (first and second). What should i do to remove the distance between two horizontal bars?

import matplotlib.pyplot as plt
import numpy as np

x = np.array([0, 15, 20, 25])
y = np.array([3500, 4239, 5239, 6239])

plt.bar(x, y, width = 2.5, edgecolor='black', color=['olive', 'seagreen', 'green', 'darkgreen'] )

plt.xticks([0, 15, 20, 25])

plt.legend()
plt.grid(color = 'slategrey', linestyle = '--', linewidth = 0.15)

plt.show()

As a newbie what should I do? Can anyone please write the whole code for me?

Here is the graph picture

CodePudding user response:

As a quick by-hand solution, you could use your x-values just as labels and equidistant ones for the plotting:

x = np.array([0, 15, 20, 25])
y = np.array([3500, 4239, 5239, 6239])

x_new = np.arange(len(x))

plt.bar(x_new, y, width = 0.5, edgecolor='black', color=['olive', 'seagreen', 'green', 'darkgreen'] )

plt.xticks(x_new, labels =x)

plt.legend()
plt.grid(color = 'slategrey', linestyle = '--', linewidth = 0.15)

plt.show()
  • Related