##### How matplotlib automatically set x-axis and y-axis limits for bar graph ?
 How matplotlib automatically set x-axis and y-axis limits for bar graph ? ift38375 Silly Frenchman Posts: 29 Threads: 18 Joined: Apr 2019 Reputation: Jul-03-2019, 11:08 AM Hi, I am confused in math/logic behind automatically by-default setting of x-axis and y-axis limits in bar graph ? Here i am sharing some examples 1. ```import matplotlib.pyplot as plt x = [1,2,3,4,5] y = [1000, 1002, 1001, 1003, 1005] plt.bar(x,y) plt.show()```In this bar graph, X axis limits are 0,1,2,3,4,5 and Y axis limits are 0,200,400,600,800,1000. How is happened?? 2. ```import matplotlib.pyplot as plt x=[20,30,90,70, 50, 60, 80, 70] y=[3,2,5,10, 3, 9, 7, 6] plt.bar(x,y) plt.show()```In this bar graph, X axis limits are 0,20,30,40,50,60,70,80,90 and Y axis limits are 0,2,4,6,8,10. How is happened?? Reply Posts: 818 Threads: 1 Joined: Mar 2018 Reputation: Jul-03-2019, 11:37 AM If you look at `plt.bar` function docs, you find keyword `bottom`; its default value 0, that means all bars will start at 0 and have height as defined in the second argument passed to `bar` (`y`). By default, matplotlib chooses axes limits to cover provided range of values. Note, bar function plots filled rectangles of specified heights, so, by default matplotlib chooses axes to cover range from 0 (bottom value) to (bottom + y). Ticks and tick labels are placed in regular manner. Check out `axes.set_xticks`, `axes.set_xticklabels`, `axes.set_xlim` (you can get current axes instance using `plt.gca()`) to change this behavior. Reply ift38375 Silly Frenchman Posts: 29 Threads: 18 Joined: Apr 2019 Reputation: Jul-03-2019, 02:26 PM (This post was last modified: Jul-03-2019, 02:26 PM by ift38375.) (Jul-03-2019, 11:37 AM)scidam Wrote: If you look at `plt.bar` function docs, you find keyword `bottom`; its default value 0, that means all bars will start at 0 and have height as defined in the second argument passed to `bar` (`y`). By default, matplotlib chooses axes limits to cover provided range of values. Note, bar function plots filled rectangles of specified heights, so, by default matplotlib chooses axes to cover range from 0 (bottom value) to (bottom + y). Ticks and tick labels are placed in regular manner. Check out `axes.set_xticks`, `axes.set_xticklabels`, `axes.set_xlim` (you can get current axes instance using `plt.gca()`) to change this behavior. please explain first example where Y axis limits are 0,200,400,600,800,1000. How these limit/range comes in graph ? Reply Posts: 818 Threads: 1 Joined: Mar 2018 Reputation: Jul-04-2019, 08:23 AM I didn't explore source code of matplotlib in detail, but in general, computation of values where ytick labels will be shown is performed by a `matplotlib.ticker` instance. By default, it is `ticker.AutoLocator`: ```from matplotlib.ticker import AutoLocator aul = AutoLocator() aul.tick_values(1, 3)`````Output:array([1. , 1.25, 1.5 , 1.75, 2. , 2.25, 2.5 , 2.75, 3. ])`````from pylab import * plt.plot([1,2,3]) plt.show() # yields a graph with ticks placed in above positions```This default behavior is made for convenience, and it could be easily overridden using `axes.set_yticks` etc. Note, there are minor and major ticks (we are talking about major one here), so underlying machinery is quite tricky. Reply

 Possibly Related Threads… Thread Author Replies Views Last Post IndexError: index 0 is out of bounds for axis 0 with size 0 atomxkai 2 847 Mar-03-2021, 08:26 AM Last Post: atomxkai Bar Chart axis Blyzz 0 301 Dec-01-2020, 02:36 AM Last Post: Blyzz IndexError: index 0 is out of bounds for axis 0 with size 0 tmhsa 0 2,000 Apr-24-2020, 10:00 AM Last Post: tmhsa how to get x values based on y-axis values from curvefit function python_newbie09 1 1,144 Sep-19-2019, 02:09 AM Last Post: scidam Automatically plot each variable on the same graph for each "ID" AdWill97 0 898 Apr-15-2019, 09:29 AM Last Post: AdWill97 How to prevent tick labels overlapping with axis SriRajesh 2 17,842 May-12-2018, 06:13 PM Last Post: SriRajesh Current time on x axis in matplolib Stumpy_L 2 2,310 Feb-07-2018, 02:07 PM Last Post: Stumpy_L Automatically updated graph in jupyter notebook using qt5 capponero 0 2,485 May-16-2017, 03:08 PM Last Post: capponero

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020