1

I have a matplotlib.bar plot and I can't figure out how to space the bars further apart so the labels are readable.

Here is my code

import matplotlib  
matplotlib.use('Agg')  
import matplotlib.pyplot as plt  
def makePlot(self, data, labels, title, fileName):  
    plt.bar(range(1,len(data)+1), data, align="center")  
    plt.title(title)  
    plt.xticks(range(1, len(labels)+1), labels)  
    plt.savefig(fileName)  
    plt.clf()
4
  • 1
    Why not swapping the x- and y-axis (to facilitate reading of the x-labels)? Moreover, if the X's have no particular order, you can reorder them by increasing or decreasing values of Y. Commented Jun 3, 2011 at 20:19
  • The categories on the x-axis represent sort of a trend over time, so it makes more sense for them to stay on the x-axis. I'm mostly bothered by the large amount of space on the right side of the plot- why can't I just increase the spacing of the bars to fill in that space, or why isn't that happening automatically? Commented Jun 3, 2011 at 20:45
  • 2
    You can use the plt.xlim() function to manually set the limits of the x axis. Commented Jun 3, 2011 at 21:22
  • That fixed it. Thank you. Would you mind editing that into your answer so it's more clear that it answered my question? Commented Jun 3, 2011 at 21:34

1 Answer 1

3

You can change the size of the figure by calling plt.figure(figsize=(x,y)) where x and y are the width and height in inches. That line must be before you call plt.bar.

Alternatively, you can make the label font smaller. You would do that by changing your call to xticks to plt.xticks(range(1, len(labels)+1), labels, size='small').

One more thing to try is to use the plt.xlim() function to manually set the limits of the x axis.

Sign up to request clarification or add additional context in comments.

1 Comment

This helps, but is it possible to set the spacing of the bars manually?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.