Kevin
Kevin

Reputation: 836

Bottom border on matplotlib bar plot

So I'm trying to make a bar plot in using matplotlib, without the "box" or axis ticks, as follows:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.bar([1, 2], [150, 250], edgecolor = 'black', linewidth = 1, \
       color = '#EEFFCC')

ax.set_xticks([1, 2])
plt.tick_params(bottom = False, left = False)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)

Particularly, I don't want the "box" but I want the bars to have a black border. However, when I make the plot, it looks like this:

enter image description here

And the bottom border isn't there (ostensibly since I got rid of the bottom "box" spine). If I try to set linewidth = 2 or higher, the border is there but is a lot thinner than the top, left, or right borders.

Is there anyway that I can have the bottom border display at the same length as the top/left/right borders for the bar? Thanks!

Upvotes: 2

Views: 3579

Answers (1)

Robbie
Robbie

Reputation: 4882

If lines (or anything else) go beyond the figure extent in matplotlib they are clipped (which is happening to your boxes). If you force then to not be clipped then it will work.

Changing the relevant line:

ax.bar([1, 2], [150, 250], edgecolor = 'black', linewidth = 1, \
       color = '#EEFFCC', clip_on=False)

Gives: enter image description here

Upvotes: 4

Related Questions