Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6135605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:29:59+00:00 2026-05-23T17:29:59+00:00

I have a barchart code snippet as below..When you run this,you get 4 bars

  • 0

I have a barchart code snippet as below..When you run this,you get 4 bars ,the first of which lies against the y axis.Is it possible to put some gap between y axis and the first bar?

def plot_graph1():
    xvals = range(4)
    xnames=["one","two","three","four"]
    yvals = [10,30,40,20]
    width = 0.25
    yinterval = 10
    figure = plt.figure()
    plt.grid(True)
    plt.xlabel('x vals')
    plt.ylabel('y vals')
    plt.bar(xvals, yvals, width=width)
    plt.xticks([ x+(width/2) for x in  xvals],[x for x in xnames])
    plt.yticks(range(0,max(yvals),yinterval))
    figure.savefig("barchart.png",format="png")
    plt.show()
if __name__=='__main__':    
    plot_graph1()

The output is as below

barchart as produced by above code

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:29:59+00:00Added an answer on May 23, 2026 at 5:29 pm

    It’s easiest to use plt.margins and plt.ylim(ymin=0). margins will act like axis('tight'), but leave the specified percentage of "padding", instead of scaling to the exact limits of the data.

    Also, plt.bar has an align="center" option that simplifies your example somewhat.

    Here’s a slightly simplified version of your example above:

    import matplotlib.pyplot as plt
    
    def plot_graph1():
        xvals = range(4)
        xnames=["one","two","three","four"]
        yvals = [10,30,40,20]
        width = 0.25
        yinterval = 10
    
        figure = plt.figure()
        plt.grid(True)
        plt.xlabel('x vals')
        plt.ylabel('y vals')
    
        plt.bar(xvals, yvals, width=width, align='center')
        plt.xticks(xvals, xnames)
        plt.yticks(range(0,max(yvals),yinterval))
        plt.xlim([min(xvals) - 0.5, max(xvals) + 0.5])
    
        figure.savefig("barchart.png",format="png")
        plt.show()
    
    if __name__=='__main__':    
        plot_graph1()
    

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

first question here. I have a BarChart showing several normal bars and a BarSeries,
Have you managed to get Aptana Studio debugging to work? I tried following this,
I have this code: $previous = 0; while($row = mysql_fetch_array($result)){ $difference = $row['steam'] -
I have a bar chart with horizontal bars. I used this article to setup
Have you guys had any experiences (positive or negative) by placing your source code/solution
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Does anyone know if its possible to have multiple tool tips displayed when you
I have a basic bar chart I'm presenting in flot (5 bars, displaying the
I have an issue with an achartengine graph - as in this screenshot: It
I'm running the code below and getting an empty chart using Flot/jQuery. Ideally what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.