I'm creating a bar plot with matplotlib in Python, and I'm having a bit of a problem with the overlapping bars:
import numpy as np
import matplotlib.pyplot as plt
a = range(1,10)
b = range(4,13)
ind = np.arange(len(a))
width = 0.65
fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar(ind+width, a, width, color='#b0c4de')
ax2 = ax.twinx()
ax2.bar(ind+width+0.35, b, 0.45, color='#deb0b0')
ax.set_xticks(ind+width+(width/2))
ax.set_xticklabels(a)
plt.tight_layout()
I want the blue bars to be in front, not the red ones. The only way I have managed to do so so far was to switch ax and ax2, but then the ylabels are going to be reversed as well, which I don't want. Isn't there a simple way to tell matplotlib to render ax2 before ax?
In addition, the ylabels on the right are being cut off by plt.tight_layout(). Is there a way to avoid this while still using tight_layout?
To avoid overlapping of labels and autopct in a matplotlib pie chart, we can follow label as a legend, using legend() method.
Overlapping Chart In Excel To create the overlapping bar chart, follow the following steps: Step 1: Select the cell containing the data. Step 2: Select the 'Insert' Tab from the top and select the bar chart. Step 3: Right-click on one bar and choose the “Change series chart type” option.
To sum up, you might use a clustered bar graph when you want to make direct comparisons across parts of a whole. On the other hand, overlapped bar graphs enable to do excellent comparisons between two closely related numerical variables.
To remove gaps between bars, we can change the align value to center in the argument of bar() method.
Perhaps there is a better way that I do not know about; however, you can swap ax
and ax2
and also swap the location of the corresponding y
-ticks with
ax.yaxis.set_ticks_position("right")
ax2.yaxis.set_ticks_position("left")
import numpy as np
import matplotlib.pyplot as plt
a = range(1,10)
b = range(4,13)
ind = np.arange(len(a))
width = 0.65
fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar(ind+width+0.35, b, 0.45, color='#deb0b0')
ax2 = ax.twinx()
ax2.bar(ind+width, a, width, color='#b0c4de')
ax.set_xticks(ind+width+(width/2))
ax.set_xticklabels(a)
ax.yaxis.set_ticks_position("right")
ax2.yaxis.set_ticks_position("left")
plt.tight_layout()
plt.show()
By the way, instead of doing the math yourself, you can center the bars using the align='center'
parameter:
import numpy as np
import matplotlib.pyplot as plt
a = range(1,10)
b = range(4,13)
ind = np.arange(len(a))
fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar(ind+0.25, b, 0.45, color='#deb0b0', align='center')
ax2 = ax.twinx()
ax2.bar(ind, a, 0.65, color='#b0c4de', align='center')
plt.xticks(ind, a)
ax.yaxis.set_ticks_position("right")
ax2.yaxis.set_ticks_position("left")
plt.tight_layout()
plt.show()
(The result is essentially the same as the above.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With